简体   繁体   English

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

[英]PHP MAILER sending multiple email to one recipient

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: 我知道这听起来像是重复的,但是我的问题有点不同,所以我从数据库使用循环中获取电子邮件以获取数据的问题是,即使所有收件人每次都在addaddress()函数中传递1封电子邮件,根据收件人数量多次发送相同的电子邮件示例,如果我的收件人是三个,每个收件人将分别收到三个电子邮件,这是我的代码:

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."}>\r\n" . 
            "Reply-To: {$adminemail}\r\n".
            "MIME-Version: 1.0\r\n".
            'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $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 真的不知道这是怎么了

Your code makes very little sense, and you're making it unnecessarily complicated. 您的代码毫无意义,并且使它不必要地变得复杂。 @RamRaider's comment is quite right. @RamRaider的评论是正确的。 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. 查看PHPMailer随附的邮件列表示例,以实现更好的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM