Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP Mailer中发送多封邮件_Php_Oop_Email_Phpmailer - Fatal编程技术网

在PHP Mailer中发送多封邮件

在PHP Mailer中发送多封邮件,php,oop,email,phpmailer,Php,Oop,Email,Phpmailer,我一直在编写一个触发器,它使用PHP mailer发送电子邮件。代码的问题在于它向单个收件人发送多封邮件。我甚至尝试使用singleTo函数,但即使这样似乎也不起作用 $mail = new PHPMailer(); for($i = 0; $i <= sizeof($emailid); $i++) { $mail->WordWrap = 50; $mail->IsHTML(true);

我一直在编写一个触发器,它使用PHP mailer发送电子邮件。代码的问题在于它向单个收件人发送多封邮件。我甚至尝试使用singleTo函数,但即使这样似乎也不起作用

$mail = new PHPMailer();

for($i = 0; $i <= sizeof($emailid); $i++)   {
    $mail->WordWrap = 50;                              
    $mail->IsHTML(true);                                 
    $mail->SingleTo = true;
    $mail->AddAddress($emailid[$i],$name[$i]);
    $mail->Subject = 'Some Subject';
    $mail->Body    = "Some Body";
    $mail->AltBody = "Some Body";

    $errornumber[$i] = 1;

    if(!$mail->Send()) {
       $errorinfo[$i] = $mail->ErrorInfo;
       $errornumber[$i] = 0;
    }
}
AddAddress只是在地址数组的末尾添加一个新地址。如果你想发送每个地址的单独电子邮件,你必须在每次迭代中清除该列表,例如

for(...) {
   $mail->AddAddress(...);
   $mail->send();
   $mail->ClearAddresses();  // <---you need this line.
}

我使用的脚本几乎相同,但我也设置了以下内容:

while(...)
{
  $mail = new PHPMailer(); //reset class instance to new one
  .....
  $mail->From = $fromAddress;   
  $mail->FromName = $fromAddress;
  $mail->AddAddress($toAdress);
  ....
}
与您的代码相同的代码示例如下


$emailid[$i]和$name[$i]的值是多少?$emailid的大小是多少?这些是邮件ID及其名称@jterryIt将是我必须发送邮件给的人数。每次都不同@我喜欢这个答案,因为如果你将同一封电子邮件发送给不同的收件人,那么真的没有必要重建电子邮件。。。您只需更改收件人即可+1谢谢!
while(...)
{
  $mail = new PHPMailer(); //reset class instance to new one
  .....
  $mail->From = $fromAddress;   
  $mail->FromName = $fromAddress;
  $mail->AddAddress($toAdress);
  ....
}
$mail = new PHPMailer();

$body = file_get_contents('contents.html');

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 25;
$mail->Username = 'yourname@example.com';
$mail->Password = 'yourpassword';
$mail->setFrom('list@example.com', 'List manager');
$mail->addReplyTo('list@example.com', 'List manager');

$mail->Subject = "PHPMailer Simple database mailing list test";

//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop
$mail->msgHTML($body);
//msgHTML also sets AltBody, so if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You'll need to alter this to match your database
$mysql = mysql_connect('localhost', 'username', 'password');
mysql_select_db('mydb', $mysql);
$result = mysql_query("SELECT full_name, email, photo FROM mailinglist WHERE sent = false",   $mysql);
while ($row = mysql_fetch_array($result)) {
      $mail->addAddress($row['email'], $row['full_name']);
      $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB

      if (!$mail->send()) {
         echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
         break; //Abandon sending
      } else {
            echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
            //Mark it as sent in the DB
            mysql_query(
             "UPDATE mailinglist SET sent = true WHERE email = '" . mysql_real_escape_string($row['email'], $mysql) . "'"
            );
      }
      // Clear all addresses and attachments for next loop
      $mail->clearAddresses();
      $mail->clearAttachments();
}