【问题标题】:Sending multiple email with different body in Php Mailer在 Php Mailer 中发送具有不同正文的多封电子邮件
【发布时间】:2015-09-06 07:30:11
【问题描述】:

我如何发送多封不同正文的电子邮件?

我有这个循环发送电子邮件:

更新:

                // $arr[] here contains email from database
                // $arrcount = length of $arr[]

                for ($x = 0; $x < $arrcount; $x++)
                {
                    $email = $arr[$x]; 

                $body = "
                    <div>   
                    <h3>Good Day!</h3>

                            You can now <b> <a href = 'project/welcome/signin.php?email=$email'> sign-in and accept this invitation </a> </b>
                        <br>
                     </div>
                "; 
                    $mail->addAddress($email, "You");
                    $query2 = "Insert into ws_invites(user_id,email,date_invited) values ('$myid','$email',now())";
                    $result2 = mysql_query($query2);
                }
                $mail->addReplyTo('myserver@gmail.com', 'Name');
                $mail->WordWrap = 50;
                $mail->Subject = 'You have been invited';
                $mail->Body    = $body;
                $mail->isHTML(true);
                if(!$mail->send()) { 
                   $fault = "true";
                }

假设我有 3 封不同的电子邮件 - email1@gmail.comemail2@gmail.comemail3@gmail.com

现在这里发生的情况是此链接的$email 的值:&lt;a href = 'project/welcome/signin.php?email=$email'&gt; 等同于email3@gmail.com,它是邮件的最后一个收件人,email1@gmail.comemail2@gmail.com 也收到该电子邮件这是email3@gmail.com

所以我得出结论,程序在发送电子邮件之前先完成循环。现在我想要的是每个循环都发送它们相应的电子邮件,而不是仅发送最后一个收件人的电子邮件。这可能吗?怎么样?

【问题讨论】:

  • 你说你用它来发送电子邮件,但我没有在代码中看到发送电子邮件的任何地方。我所看到的只是数据库中的一个新条目。 $arrcount 的值是多少?您想将该信息插入数据库多少次?您的插入语句在循环内。
  • 使用phpmailer查看examples文件夹中提供的邮件列表示例,并确保您使用的是最新版本。
  • 更新了问题。希望你能帮助我。
  • @Makudex,我也有同样的问题,请问解决方法是什么,我真的卡在这一点上。

标签: php phpmailer


【解决方案1】:

您可以在 for 循环中添加邮件发送。这按顺序发送。但这可能很慢。试试这个改变:

$mail->addReplyTo('myserver@gmail.com', 'Name');
$mail->WordWrap = 50;
$mail->Subject = 'You have been invited';
$mail->isHTML(true);

for ($x = 0; $x < $arrcount; $x++)
{
    $email = $arr[$x];

    $body = "
    <div>
        <h3>Good Day!</h3>
        You can now <b> <a href = 'project/welcome/signin.php?email=$email'> sign-in and accept this invitation </a> </b>
        <br>
    </div>
    ";
    $mail->addAddress($email, "You");
    $query2 = "Insert into ws_invites(user_id,email,date_invited) values ('$myid','$email',now())";
    $result2 = mysql_query($query2);
    $mail->Body = $body;
    if(!$mail->send()) {
       $fault = "true";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多