2012-07-06 166 views
0

知道你是否你們幫我一點點,因爲我用PHP郵件瞎搞,我已經修改這個代碼,但是從這裏http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.htmlPHP梅勒發送簡訊

發送單封電子郵件是通過PHP郵件工作正常(與一個不同的腳本),但現在試圖發送到多個電子郵件與數據庫下面的腳本目前不工作..你能發現它有什麼問題嗎?雖然我想知道它是否真的用數據庫中的電子郵件做任何事情..我有點困惑。

腳本不會成功並打印的名字,但不發送任何電子郵件了!至少沒有收到..(不是垃圾郵件)任何幫助?對不起,如果這是非常明顯的!

<?php 
// Grab our config settings 
require_once($_SERVER['DOCUMENT_ROOT'].'/mail/config.php'); 

// Grab the FreakMailer class 
require_once($_SERVER['DOCUMENT_ROOT'].'/mail/lib/MailClass.inc'); 

//set execution time limit to 5 minutes 

$safeMode = (@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1) ? TRUE : FALSE; 
if ($safeMode === FALSE) { 
    set_time_limit(300); // Sets maximum execution time to 5 minutes (300 seconds) 
    // ini_set("max_execution_time", "300"); // this does the same as "set_time_limit(300)" 
} 

echo "max_execution_time " . ini_get('max_execution_time') . "<br>"; 


     //db connection 
    $con = mysql_connect("xx","xx","xx"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("xx", $con); 

// Setup body 
$textBody = "Dear {MEMBER_NAME},\n\nTEST"; 
$htmlBody = "Dear {MEMBER_NAME},<br /><br />TEST"; 

// instantiate the class 
$mailer = new FreakMailer(); 

// Get the user's Email 
$sql = mysql_query("SELECT displayname,email FROM engine4_users2")or die(mysql_error()); 


//lets reset the time limit of the server everytime an email is sent to bypass maximum 
    while (1==1) { 
    set_time_limit(30); // sets (or resets) maximum execution time to 30 seconds) 
    // .... put code to process in here 


    while($row = mysql_fetch_object($sql)) 
    { 
    // Send the emails in this loop. 
    $member_name = $row->displayname; 

    if($row->MailType == 'html') 
    { 
    $mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody); 
    $mailer->IsHTML(true); 
    $mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody); 
    } 
    else 
    { 
    $mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $textBody); 
    $mailer->isHTML(false); 
    } 
    $mailer->Send(); 
    $mailer->ClearAddresses(); 
    $mailer->ClearAttachments(); 
    $mailer->IsHTML(false); 
    echo "Mail sent to: " . $member_name . "<br />"; 
    } 




    usleep(1000000); // sleep for 1 million micro seconds - will not work with Windows servers/PHP4 
    // sleep(1); // sleep for 1 seconds (use with Windows servers/PHP4 
     if (1!=1) { 
    break; 
    } 
} 



?> 
+1

的目的是什麼,以你上次'if'命令? 'if(1!= 1)'?你是否重新定義了其他地方的號碼? – 2012-07-06 00:08:30

回答

0

您從未將用戶的地址設置爲郵件程序。

$mailer->AddAddress($row->email); 

之前send()被調用。因爲您要在循環中發送郵件,所以每個用戶都會收到一封郵件,並且不會看到其他地址。

(你鏈接的實況的例子是不完整的,如下意見披露)

你可以或者只放一個

$mail->AddBCC($row->email); 
在循環

和郵寄者的代碼循環之後的其餘部分發送一封郵件給所有人,但除了密送抄送地址以外,您還需要一個(虛擬)地址作爲收件人才能發送郵件。

if($row->MailType == 'html') 

您沒有在查詢中選擇MailType。你是否啓用了錯誤報告?

+0

謝謝,得到它的工作:)我意識到像你說的,它在一個循環,所以只需添加地址!沒有搞砸 – user1250526 2012-07-06 12:49:31

1

是否有原因,你不使用phpmailer?爲phpmailer下載.php文件並使用此代碼,就像一個魅力:

<?php 
$sql = mysql_query("SELECT displayname,email FROM engine4_users2")or die(mysql_error()); 

// 
// 

require_once('../../mailer/class.phpgmailer.php'); 
require_once ('../../mailer/class.smtp.php'); 
require_once('../../mailer/phpmailer.lang-en.php'); 

$debug = new SMTP(); 
$debug->do_debug = 2; 

while ($record = mysql_fetch_array ($sql)) { 
$mail = new PHPGMailer(); 
$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host = "mail.zzxxyyhost.com"; // SMTP server 
$mail->Port = 26; //designated port, could be different, check your host 
$mail->SMTPAuth = TRUE; //smtp authentication may be false, check your host 
$mail->Username = "username"; //username 
$mail->Password = "password"; //password 

$mail->From = "[email protected]"; 
$mail->FromName = "fromsomeone"; 

$mail->AddBCC($record['email'], $record["displayname"]); //use bcc for hidden emails  
$mail->Subject = "$record["displayname"]"; 
$mail->Body = "Your body"; 


      if(!$mail->Send()) 
      { 
       echo 'Message was not sent.'; 
       echo 'Mailer error: ' . $mail->ErrorInfo; 
      } 
    // Clear all addresses and attachments for next loop 
    $mail->ClearAddresses(); 
    $mail->ClearAttachments(); 
} 

?> 
+0

我很困惑,我以爲我在使用PHPmailer?你的代碼不需要我使用SMTP? – user1250526 2012-07-06 00:28:40

+0

僅供參考,我使用上面的腳本發送一個電子郵件到多個地址,它的工作原理。 – 2012-07-06 00:30:24

+0

是的,它確實需要你使用smtp。我注意到你正在使用郵件的擴展版本。如果您將我引用的文件包含在郵件程序目錄中,它將會起作用。 – 2012-07-06 00:31:55