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.

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行