doujiufutaog59220 2015-09-06 07:30
浏览 209

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

How am i able to send multiple email with different body?

I have this loop for sending email:

UPDATED:

                // $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";
                }

Let's assume I have 3 different emails - email1@gmail.com, email2@gmail.com, email3@gmail.com.

Now what happens here is that the value of the $email of this link: <a href = 'project/welcome/signin.php?email=$email'> is equivalent to email3@gmail.com which is the last recipient of the message and both email1@gmail.com and email2@gmail.com receives that email too which is email3@gmail.com.

So i concluded that the program finishes the loops first before sending email. Now what I want is that each loop sends their corresponding email and not that of the email of the last recipient only. Could this be possible? How?

  • 写回答

1条回答 默认 最新

  • dsfdsf21312 2018-09-12 12:28
    关注

    You can add mail sending in your for loop. This sends in order. But this can be slow. Try this change:

    $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";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?