php - 如何将电子邮件发送到从数据库中获取的包含其他数据的电子邮件数组

标签 php mysql phpmailer

我在使用 phpmailer 发送批量电子邮件时遇到问题,但发送一封电子邮件没有问题。

这是我的代码:

$result = mysql_query("select * from $to",$conn) or die("list 
            selected  ($to) does not exist ".mysql_error());

while ($row = mysql_fetch_array($result))
{           
    $email[] = $row['email'];           
    $student[] = $row['name'];
}

foreach ($email as $val => $uemail) {
    $email = $uemail;
    $students= $student[$val] ;

    require("class.phpmailer.php");
        $mail = new PHPMailer(true); 

    try {
           $mail->AddReplyTo('info@bratim.com', 'My Name');
           $mail->AddAddress("$email", "$student");
           $mail->SetFrom('info@me.com', 'MyName');
           $mail->AddReplyTo('info@me.com', 'My nameg');
           $mail->Subject = "$sub";

           $mail->MsgHTML("Dear $student<br> $msg <br>
             <img src=\"$path\"> <p>

             $host_upper
            ______________________________________________________
            THIS IS AN AUTOMATED RESPONSE. 
            ***DO NOT RESPOND TO THIS EMAIL****

             ");
            $mail->AddAttachment("$path2");      // attachment

          $mail->Send();
          echo "Message Sent OK to $email  </p>\n";
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
    } 
}

任何帮助、建议将不胜感激。

最佳答案

您可以在 while 循环内发送邮件:

<?php
require_once("class.phpmailer.php");

$result = mysql_query('SELECT `email`, `student`, `data1` FROM `students` ORDER BY `email` ASC;');

while ( $row = mysql_fetch_assoc($result) )
{
    $current_mail = new PHPMailer(true);

    $current_mail->AddReplyTo('info@bratim.com', 'My Name');
    // ....
    $current_mail->Send();
    unset($current_mail);
}
?>

关于php - 如何将电子邮件发送到从数据库中获取的包含其他数据的电子邮件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11326733/

相关文章:

php - 将表值更新到数据库

php - 如何将多个查询更新为一个

php - 数据库中缺少换行符

php - 如何使用 PHPmailer 使用 Postfix 配置和发送邮件?

php - 汉字PHPMailer

php - 在 PHP 中创建简单的 Google 日历事件

php - PHP 中有没有更优雅的方法来解析这个 xpath 对象?

javascript - javascript 是否具有在给定时间内向收件人发送警报/通知电子邮件的功能?

php - Symfony + Doctrine Oracle DateTime 格式问题

MySQL:按 JSON 属性搜索返回错误