在Php Mailer中发送多个具有不同正文的电子邮件

时间:2015-09-06 07:30:12

标签: php phpmailer

我如何能够发送多个不同身份的电子邮件?

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

更新:

                // $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.com email2@gmail.com email3@gmail.com

现在,这里发生的是此链接$email的值:<a href = 'project/welcome/signin.php?email=$email'>等同于email3@gmail.com,它是邮件的最后一个收件人,email1@gmail.com并且email2@gmail.com也会收到email3@gmail.com的电子邮件。

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

1 个答案:

答案 0 :(得分: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";
    }
}