0

想知道你们是否可以帮我一点,因为我正在搞乱 PHP 邮件程序,我修改了这段代码,但是从这里http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html

通过 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;
   }
 }



 ?>
4

2 回答 2

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 = "from@someone.com"; 
$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();
}

?>
于 2012-07-06T00:20:23.087 回答
0

您永远不会将用户的地址设置为邮件程序。

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

之前send()被调用。因为您在循环中发送邮件,所以每个用户都会收到一封邮件,并且不会看到其他地址。

(您链接的文档示例不完整,因为下面的评论显示)

你也可以只放一个

$mail->AddBCC($row->email);

在循环和循环之后的其余邮件程序代码中,向所有人发送一封邮件,但是除了密件抄送地址之外,您还需要一个(虚拟)地址作为收件人才能发送邮件。

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

您没有在查询中选择 MailType。您是否启用了错误报告?

于 2012-07-06T00:38:12.987 回答