Search code examples
phpemailsslzend-framework2smtp

zend smtp mail crashes after 100+ mails


While sending a newsletter-kind-of mail I came across this weird problem.

In a for loop, I loop through all the users in a database and try to send them all an HTML mail with some basic information. Now the thing is that the first 200 or so mails run fine, but then the script crashes and gives the following error:

Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 263 Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 263 Fatal error: Uncaught exception 'Zend\Mail\Protocol\Exception\RuntimeException' with message 'Could not read from smtp.gmail.com' in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php:308 Stack trace: #0 /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php(339): Zend\Mail\Protocol\AbstractProtocol->_receive(300) #1 /opt/zendframework2/library/Zend/Mail/Protocol/Smtp.php(358): Zend\Mail\Protocol\AbstractProtocol->_expect(221, 300) #2 /opt/zendframework2/library/Zend/Mail/Protocol/Smtp.php(394): Zend\Mail\Protocol\Smtp->quit() #3 /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php(115): Zend\Mail\Protocol\Smtp->_disconnect() #4 [internal function]: Zend\Mail\Protocol\AbstractProtocol->__destruct() #5 {main} thrown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 308

Now, I'm not to familiar with smtp, ssl & tls but I believe the most important line of the error is: 'Could not read from smtp.gmail.com'. Which doesn't make any sense to me.

We send emails all the time ( lost passwords, registration mails, etc ) and this always (to my knowledge) works fine. The script just crashes after it has sent too many mails in a short period of time.

Ok, that was the problem, now let me explain the setup :)

I'm running Zend 2.2.6 on a standard LAMP server ( PHP 5.3.10 ) and using standard SMTP mail scripts provided by Zend. We're using Google business apps as a mail client. The following is the first couple of lines of the mail script:

  <?PHP
    namespace Mail\Mails;
    use Zend\Mail;
    use Zend\Mail\Message;
    use Zend\Mime\Message as MimeMessage;
    use Zend\Mime\Part as MimePart;
    use Zend\Mail\Transport\Smtp as SmtpTransport;
    use Zend\Mail\Transport\SmtpOptions;
    use Mail\Config\Config;

    class Base
    {
        private $transport, $text, $html, $to, $subject;

        public function __construct()
        {
            $config     = new Config();
            $transport  = new SmtpTransport();
            $options    = new SmtpOptions(array(
                'name'  => 'mydomain.com',
                'host'  => 'smtp.gmail.com',
                'port'  => 587,
                'connection_class'  => 'login',
                'connection_config' => array(
                    'username'  => $config->username,
                    'password'  => $config->password,
                    'ssl'       => 'tls'
                ),
            ));

            $transport->setOptions($options);
            $this->transport = $transport;

            // This happens in different parts of the code.
            $this->subject( $subject );
            $this->to( $address );
            $this->html( $html );
            $this->text( $text );
            $this->send();
        }

    ?>

What I've tried so far:

  1. Running the script again
    • This has different outcomes: sometimes it breaks earlier, sometimes it breaks further on, but this proves the script doesn't break on a particular address. I get the feeling that the server has some kind of cooling down: the first run it processes like 200 addresses but when I rerun the script directly after, it can break after 20 addresses. When I try it like an hour later the script breaks after about 200 addresses again.
  2. I've tried to change the ssl option to 'ssl' and the port option to '465' but this has the exact same result.

Is anybody familiar with this problem? I'm not really sure where to look for problems, maybe someone can give me a push in the right direction?

Thanks in advance!


Solution

  • Older site but probably your problem

    http://www.serversmtp.com/en/limits-of-gmail-smtp-server

    That's because Google places all sorts of limits on the use of its SMTP service by marketers. Those limits begin with a restriction on the number of recipients who can receive the same message. If Gmail's SMTP server detects that your message is going to more than 500 people, it disables your account.