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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵