doutang1884 2015-08-27 09:18
浏览 50
已采纳

PHP MAILER向一个收件人发送多封电子邮件

I know it may sound like duplicate but my problems a bit different so i'm fetching email from my database use loop to get the data the problem was even if passing 1 email at the addaddress() function at a time all the recipient receive the same email multiple times based on the number of recipient example if my recipient is three each recipient will receive three email each this is my code:

for ($i = 0; $i <= (6) - 1; $i++) {
    $q = Yii::app()->db->createcommand("select user_other_email from user_account LIMIT 6")->queryall();
    $ty = '';
    for ($b = 0; $b < count($q); $b++) {
        $ty = $email = $q[$b]['user_other_email'];
        $msg = "Test this!";
        $adminemail = "email@gmail.com";
        $name=' = ?UTF-8?B?' . base64_encode($adminemail) . '?=';
        $subject = 'TEST';
        $headers = "From: $name <{".$adminemail."}>
" . 
            "Reply-To: {$adminemail}
".
            "MIME-Version: 1.0
".
            'Content-type: text/html; charset=iso-8859-1' . "
";
        $this->mailsend2($ty,$adminemail,$subject,$msg);
    }
}

AND this is my mailer function:

public function mailsend2($to, $from, $subject, $message) {
    $mail = Yii::app()->Smtpmail;
    $mail->SetFrom($from, 'Test');
    $mail->Subject = $subject;
    $mail->MsgHTML($message);
    $mail->AddAddress($to);
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
}

really dont know what is wrong with this

  • 写回答

1条回答 默认 最新

  • dqwh0109 2015-08-27 10:23
    关注

    Your code makes very little sense, and you're making it unnecessarily complicated. @RamRaider's comment is quite right. You're also doing lots of unnecessary work building strings you never use. Here's a more elegant implementation that should work as expected:

    $q = Yii::app()->db->createcommand("select user_other_email from user_account LIMIT 6")->queryall();
    foreach ($q as $to) {
        $this->mailsend2(
            $to['user_other_email'],
            'email@gmail.com',
            'TEST',
            'Test this!'
        );
    }
    

    This is still quite an inefficient way of sending. Look at the mailing list example provided with PHPMailer for a better implementation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?