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.

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

报告相同问题?