通过G-Suite发送群发电子邮件PHPMAILER

时间:2017-04-26 12:32:16

标签: php email phpmailer gsuite

所以我有以下代码:

  while(list($i, $data_subscriber) = each($subscriber)) {

     $email_counter = $i + 1;
     echo ($email_counter).') '.$data_subscriber->field_1.' '.$data_subscriber->field_2.' - '.$data_subscriber->email.''."\n";
     flush();

     $language = $data_subscriber->lang;

     $body_mail = include($template_send_newsletter);

     $first_name_idx = $data_subscriber->field_1;
     $last_name_idx = $data_subscriber->field_2;

     if (mail_send(strtolower($data_subscriber->email),
                   ( IsSet($first_name_idx) && IsSet($last_name_idx) ? $first_name_idx.' '.$last_name_idx : $data_subscriber->email ),
                   $site_admin,
                   $site_name,
                   'Newsletter ['.date('d/m/Y', time()).']',
                   true,
                   $body_mail,
                   NULL,
                   false)) {

       reset($data_subscriber->newsletter_item);
       unset($log_detail);
       $log_detail = "";
       while(list($j, $list_of_news_to_send) = each($data_subscriber->newsletter_item)) {
         $log_detail = ( IsSet($list_of_news_to_send->title) ? $list_of_news_to_send->title."\n" : '');
         $log_detail = ( IsSet($list_of_news_to_send->subtitle) ? $list_of_news_to_send->subtitle."\n" : '');
         $log_detail = ( IsSet($list_of_news_to_send->creation_date) ? $list_of_news_to_send->creation_date."\n" : '');
         $log_detail = "\n";
       } /* end while */

       write_newsletter_detail($data_subscriber->id_subscription,
                               $data_subscriber->email,
                               'Y',
                               $log_detail);
     }
     else {
       write_newsletter_detail($data_subscriber->id_subscription,
                               $data_subscriber->email,
                               'N',
                               NULL);
     } /* end if mail_send */

   } /* end while */

以下功能

function mail_send($rcpt_to,           
                   $to_name,                 
                   $mail_from,                
                   $from_name,                
                   $subject,          
                   $isHTML = true,            
                   $body,                     
                   $attachment = NULL,        
                   $direct_delivery = false,
                   $reply_to = NULL){

    $mail = new PHPmailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 3;
    $mail->Debugoutput = 'html';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = "mail@domain.com";
    $mail->Password = "password";
    $mail->Timeout = 3600;
    $mail->addAddress(trim($rcpt_to), trim($to_name));
    $mail->setFrom($mail_from,$from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;

    if ($isHTML) {
      $mail->IsHTML(true);
      $mail->AltBody = strip_tags($mail->Body);
    } 
    if (IsSet($attachment)) {
      $mail->AddAttachment($attachment['tmp_name'], $attachment['name']);
    }
    if (IsSet($reply_to)) {
      $mail->AddReplyTo($reply_to);
    }
    if (!$mail->send()) {
        return false;
    } else {
        return true;
    }
  }

我正在使用PHPMailer使用gmail SMTP服务器发送多封电子邮件。 我有4个文件包含不同但几乎相同的代码来发送简报。 我的一个文件是将所有电子邮件发送给(~60个收件人) 其他3个文件,每个文件需要向~600个收件人发送一封电子邮件。 这里出现问题,一半的电子邮件是发送给另一个不是。 如果我运行脚本,但这次是因为没有收到第一次收到电子邮件的收件人,它会发送。

我在自己的域名中使用GMAIL。所以我没有超过任何限制。

可能导致此问题的原因是什么?有任何想法吗 ?谢谢。

UPDATE **

SERVER -> CLIENT: 454 4.7.0 Too many login attempts, please try again later. e11sm950198edd.68 - gsmtp

发送一些电子邮件后,我收到此错误。有什么想法吗?

UPDATE ***

  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 3;
  $mail->Host = 'smtp.gmail.com';
  $mail->SMTPAuth = true;
  $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
  $mail->Port = 465;
  $mail->SMTPSecure = 'ssl';
  $mail->Username = "domain@domain.com";
  $mail->Password = "password";
  $mail->setFrom($site_noreply, $site_name);
  $subjectnewsletter = 'Newsletter ['.date('d/m/Y', time()).']';
  $mail->Subject = $subjectnewsletter;
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

  reset($subscriber);
  while(list($i, $data_subscriber) = each($subscriber)) {
   $email_counter = $i + 1;
   echo ($email_counter).') '.$data_subscriber->field_1.' '.$data_subscriber->field_2.' - '.$data_subscriber->email.''."\n";
   flush();
   $language = $data_subscriber->lang;
   $first_name_idx = $data_subscriber->field_1;
   $last_name_idx = $data_subscriber->field_2;
   $fullname = $first_name_idx.' '.$last_name_idx;

   $bodymail = include($template_send_newsletter);
   $mail->msgHTML($bodymail);
   $mail->addAddress($data_subscriber->email, $fullname);
   reset($data_subscriber->newsletter_item);
   unset($log_detail);
   $log_detail = "";
   while(list($j, $list_of_news_to_send) = each($data_subscriber->newsletter_item)) {
     $log_detail = ( IsSet($list_of_news_to_send->title) ? $list_of_news_to_send->title."\n" : '');
     $log_detail = ( IsSet($list_of_news_to_send->subtitle) ? $list_of_news_to_send->subtitle."\n" : '');
     $log_detail = ( IsSet($list_of_news_to_send->creation_date) ? $list_of_news_to_send->creation_date."\n" : '');
     $log_detail = "\n";
   } /* end while */
   if (!$mail->send()) {
     write_newsletter_detail($data_subscriber->id_subscription,
                             $data_subscriber->email,
                             'N',
                             NULL);
       echo $mail->ErrorInfo;
       break; //Abandon sending
   } else {

     write_newsletter_detail($data_subscriber->id_subscription,
                             $data_subscriber->email,
                             'Y',
                             $log_detail);
       echo "Message sent";
   }
   $mail->clearAddresses();
   $mail->clearAttachments();
  } /* end while */

我设法更改了我的代码,因此它不会在每封电子邮件中连接到smtp服务器。 但现在它发送了93封电子邮件后,我得到了以下错误:

SMTP server error: MAIL FROM command failed

2 个答案:

答案 0 :(得分:0)

您需要使用一个使用多个连接的MTA,并且它已经设置为尊重您要连接的服务器的限制。此时,您试图超越您要连接的服务器的限制,以及Google等公司对太多连接或使用其邮件服务器进行广播的限制。

如果您的号码数量适中,请查找mail sending service,或者如果您发送大量电子邮件,请考虑购买和托管MTA。

答案 1 :(得分:0)

您可以使用SMTP relay配置,该配置使您可以使用任何电子邮件域地址从服务器发送电子邮件。

相关问题