简体   繁体   English

PHPMailer 没有将所有电子邮件发送到最后一个电子邮件地址

[英]PHPMailer not sending all emails to last email address

I am using PHPMailer to send emails.我正在使用 PHPMailer 发送电子邮件。 I have created a function that sends 3 emails to 3 different email addresses (sending 9 emails in total).我创建了一个函数,可以向 3 个不同的电子邮件地址发送 3 封电子邮件(总共发送 9 封电子邮件)。

  • The first email address is receiving all the 3 emails.第一个电子邮件地址正在接收所有 3 封电子邮件。
  • The second email address is receiving 2 emails.第二个电子邮件地址正在接收 2 封电子邮件。
  • The third email address is receiving only 1 email.第三个电子邮件地址仅收到 1 封电子邮件。

Why this happening?为什么会发生这种情况?

Here is my code:这是我的代码:

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception;

require 'lib/phpmailer/vendor/autoload.php'; 

$mail = new PHPMailer(true);

$mail1 = phpmaileremail($reciever1, $usertype1, $file, $subject1, $body1);
$mail2 = phpmaileremail($reciever2, $usertype2, $file, $subject2, $body2);
$mail3 = phpmaileremail($reciever3, $usertype3, $file, $subject3, $body3);

function phpmaileremail($reciever,$usertype, $file, $subject, $body)
{
    global $mail;
    $mail->SMTPDebug = 0;                                        
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'xxx@gmail.com';
    $mail->Password = 'xxx';
    $mail->SMTPSecure = 'tls';                               
    $mail->Port = 587;   

    $mail->setFrom('xxx', 'xxx');            
    $mail->addAddress($reciever); 
    $mail->addAddress($reciever, $usertype); 

    $mail->addAttachment($file);

    $mail->isHTML(true);                                   
    $mail->Subject = $subject; 
    $mail->Body = $body; 
    $mail->AltBody = 'NA'; 
    $mail->send();

    echo "Mail sent";
}

Because you're reusing the $mail object to addAddress() and send() .因为您将$mail对象重用于addAddress()send() So the first time you call phpmaileremail() the first address gets the email.因此,当您第一次调用phpmaileremail() ,第一个地址会收到电子邮件。 Then when you call it for the second time the second address is added and the first and second address get the email.然后,当您第二次调用它时,会添加第二个地址,并且第一个和第二个地址会收到电子邮件。 And so on.等等。

A simple solution would be to create the $mail object inside the phpmaileremail() function:一个简单的解决方案是在phpmaileremail()函数中创建$mail对象:

function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{

  $mail = new PHPMailer(true);

  $mail->SMTPDebug = 0;                                        
  $mail->isSMTP();                                             
  $mail->Host       = 'smtp.gmail.com;';                     
  $mail->SMTPAuth   = true;                              
  $mail->Username   = 'XXXXXXXX@gmail.com';                  
  $mail->Password   = 'XXXXXXXXXXXXXXXXXX';                         
  $mail->SMTPSecure = 'tls';                               
  $mail->Port       = 587;   

  $mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');            
  $mail->addAddress($reciever); 
  $mail->addAddress($reciever, $usertype); 
  // Attachments
  $mail->addAttachment($file);         // Add attachments

  $mail->isHTML(true);                                   
  $mail->Subject = $emailsubject; 
  $mail->Body    = $email_body; 
  $mail->AltBody = 'NA'; 
  $mail->send(); 

  echo "Mail sent"; 

}

PS: Not that it matters, but reciever is written receiver . PS:这不是问题,但reciever写入receiver I've made that mistake as well.我也犯过这样的错误。

Kiko's answer will work, however it's not the best way. Kiko 的答案会奏效,但这不是最好的方法。 As its name suggests, addAddress adds an address, it doesn't set absolutely or replace existing recipients you've already added.顾名思义, addAddress添加一个地址,它不会绝对设置或替换您已经添加的现有收件人。

PHPMailer has a standard function to clear the list of addresses you're ending to called clearAddresses , so the right approach is to call that after each message you send and add the new address before sending the next one, so the sequence will be roughly: PHPMailer 有一个标准函数来清除您要结束的地址列表,称为clearAddresses ,因此正确的方法是在您发送的每条消息之后调用它并在发送下一条消息之前添加新地址,因此序列大致如下:

addAddress();
send();
clearAddresses();
addAddress();
send();

and so on.等等。 This is most clearly demonstrated in the mailing list example provided with PHPMailer , which does its sending in a loop, calling clearAddresses each time around.这在PHPMailer 提供的邮件列表示例中得到最清楚的证明,它在循环中发送,每次都调用clearAddresses

You can achieve the same thing using a new instance of PHPMailer each time (which has the effect of clearing addresses, but also clears everything else too), but it's more efficient to re-use the instance.您可以每次使用 PHPMailer 的新实例来实现相同的目的(它具有清除地址的效果,但也清除其他所有内容),但重用该实例更有效。 This is especially true if you're sending over SMTP (which you are) because it will allow you to make use of keepalive, which dramatically reduces the overhead of making an SMTP connection.如果您通过 SMTP(您是)发送,则尤其如此,因为它允许您使用 keepalive,从而大大减少了建立 SMTP 连接的开销。 If you use a new instance, the connection is dropped and recreated each time.如果您使用新实例,则每次都会断开并重新创建连接。 You can achieve this inside your function by making the PHPMailer instance static:您可以通过将 PHPMailer 实例设为静态来在您的函数中实现这一点:

function phpmaileremail($reciever, $usertype, $file, $emailsubject, $email_body)
{
    static $mail;

    if ($mail === null) {
        //Set everything that remains the same all the time in here
        $mail = new PHPMailer();
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com;';
        $mail->SMTPAuth = true;
        $mail->Username = 'XXXXXXXX@gmail.com';
        $mail->Password = 'XXXXXXXXXXXXXXXXXX';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
        $mail->SMTPKeepAlive = true;
        $mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
    }
    $mail->addAddress($reciever, $usertype);
    // Attachments
    $mail->addAttachment($file);         // Add attachments

    $mail->isHTML(true);
    $mail->Subject = $emailsubject;
    $mail->Body = $email_body;
    $mail->AltBody = 'NA';
    $mail->send();
    $mail->clearAddresses();
    $mail->clearAttachments();
    echo "Mail sent";
}

This has the added benefit of not using a global.这具有不使用全局的额外好处。 Also note the use of clearAttachments , as that works the same way as addresses.还要注意clearAttachments的使用,因为它的工作方式与地址相同。

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

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