Swiftmailer only sends 999 emails from BCC

感情迁移 提交于 2019-12-10 22:12:23

问题


I am using Swiftmailer PHP Version 4.3.0 and I want to send one email with 1500 email addresses in the BCC.

The limits on my server allow that I can send up to 10000 emails within 24h. I have not exceeded this limit.

However, when I try to send the email with Swiftmailer it returns the following error message (see below for details)

"421 too many messages in this connection "'

and only 999 emails from the 1500 email addresses in the BCC receive the email. I executed this experiment several times, also changing the number of emails in the BBC to 1000 or 2000 - each time exactly 999 emails go through. Is it possible that there is a limit somewhere in Swiftmailer which does not allow to set more then 999 email addresses in the BCC? If so, is it possible to change this limit? If not, what else could cause this problem?


Here is the complete error message from Swiftmailer:

exception 'Swift_TransportException' with message 'Expected response code 250 but got code "421", with message "421 too many messages in this connection "' in /is/htdocs/www/swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php:386 Stack trace:

#0 /is/htdocs/www/swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('421 too many me...', Array)

#1 /is/htdocs/www/swift/lib/classes/Swift/Transport/EsmtpTransport.php(245): Swift_Transport_AbstractSmtpTransport->executeCommand('MAIL FROM: executeCommand('MAIL FROM: _doMailFromCommand('me@mydomain.n...')

#4 /is/htdocs/www/swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(466): Swift_Transport_AbstractSmtpTransport->_doMailTransaction(Object(Swift_Message), 'me@mydomain.n...', Array, Array)

#5 /is/htdocs/www/swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(178): Swift_Transport_AbstractSmtpTransport->_sendBcc(Object(Swift_Message), 'me@mydomain.n...', Array, Array)

#6 /is/htdocs/www/swift/lib/classes/Swift/Mailer.php(86): Swift_Transport_AbstractSmtpTransport->send(Object(Swift_Message), Array)

#7 /is/htdocs/www/swift/lib/swift_required.php(133): Swift_Mailer->send(Object(Swift_Message))

#8 /is/htdocs/www/mydomain.net/test.php(21): sendMessage()

#9 {main}

when executing this code:

/*
 * Autoloader and dependency injection initialization for Swift Mailer.
 */

if (defined('SWIFT_REQUIRED_LOADED')) {
    return;
}

define('SWIFT_REQUIRED_LOADED', true);

//Load Swift utility class
require dirname(__FILE__) . '/classes/Swift.php';

if (!function_exists('_swiftmailer_init')) {
    function _swiftmailer_init()
    {
        require dirname(__FILE__) . '/swift_init.php';
    }
}

//Start the autoloader and lazy-load the init script to set up dependency injection
Swift::registerAutoload('_swiftmailer_init');

sendMessage();

function sendMessage(){

try{

  // LARGE ARRAY with 1500 entries
  $bcc = array("test1@mydomain.net","test2@mydomain.net", ... , "test1500@mydomain.net")

  // Create the Transport
  $transport = Swift_SmtpTransport::newInstance('***', 25);
  $transport->setUsername('***');
  $transport->setPassword('***');

  // Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  // Create the message
  $message = Swift_Message::newInstance();

  // Give the message a subject
  $message->setSubject("This is a test mail");

  // Set the From address with an associative array
  $message->setFrom(array('me@mydomain.net' => 'My Name'));

  $message->setTo("specialMail@mydomain.net");

  $message->setBcc($bcc);

  $message->setBody("Hi, this is a test mail. Enjoy.");

  $result = $mailer->send($message);

  echo "Success";
}
catch(Exception $exception)
{
  echo $exception;
}
}

回答1:


That message comes from the remote SMTP server. Your mail hosting account may accept 10000 messages per day but that is totally unrelated to other limits you might have (total recipients, recipients per message, messages per connection, message size, allowed senders...).

Bcc works internally by sending individual messages, one per recipient, so it's sensible to hit some limits doing so.

It shouldn't be difficult to split your recipient list in chunks and send several copies of the message:

$all_bcc = array("test1@mydomain.net","test2@mydomain.net", ... , "test1500@mydomain.net");

foreach (array_chunk($all_bcc, 999) as $bcc) {
    $transport = Swift_SmtpTransport::newInstance('***', 25);
    $transport->setUsername('***');
    // ...
}



回答2:


I built a system that had to send emails to multiple people but without the recipients knowing who else the email was sent to, and the number of recipients could be anything from 10 through to 50,000.

The end product you should be aiming for is to send the email within a loop, so that it sends the email directly to each of the 1,500 recipients you have. This really is the only way around your predicament.

With this method however you may find yourself falling foul of the rules set by bulk mail senders (e.g. SendGrid, Mailgun etc) who limit the number of emails you send per hour.

In my case I created a record in a database with all of the email information and recipient information (so you would create 1,500 individual records) and then create a cronjob to run every minute. The cronjob would run the email sending script, which would send about 15 emails to start with. But you could increase this figure over time until you hit the sweet spot.



来源:https://stackoverflow.com/questions/37697008/swiftmailer-only-sends-999-emails-from-bcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!