1

I have a problem sending a lot of emails from my Laravel app.

Before explain the issues, this is the server config:

Server: Google Compute Engine, VM Instance (Ubuntu Server) - Standar 1

This is my configuration:

MAIL_DRIVER=smtp  
MAIL_HOST=smtp-relay.gmail.com  
MAIL_PORT=587  
MAIL_USERNAME=my_email@mydomain.com  
MAIL_PASSWORD=********  
MAIL_ENCRYPTION=tls  

So, I use Google App work Account for this purpose and all works very well when i send emails for one recipient!

The problem is when i try to send email to all my customers:

foreach ($users as $key => $user) {
// content construction
    if ($have_one){
    Mail::queue('emails.contact', $body, function($message) use($email)
        {
           $message->from('info@mydomain.com', 'My Name Team');
           $message->to($email)->subject('Fake Subject!');
        });
    }
}

It work well the very firsts 100-200 emails sent, but in some point it just crash it!!!

I use a php artisan command to send this emails, and this is the output:
output sending email

Does anyone know how to fix it?

Thanks in advance!

uTombou
  • 41
  • 1
  • 6
  • Is it usually failing at a specific user or does it seem random? – user1669496 Nov 04 '16 at 14:23
  • Hi, thanks for your reply! It seems to be a **random behaviour**. Sometimes it stops at 100, 120 or any other user between 90 -150. – uTombou Nov 04 '16 at 14:36
  • You may read this. This is a not a same but may be close. Please read the link. http://stackoverflow.com/questions/37469770/laravel-swiftmailer-expected-response-code-250-but-got-code-530-with-messag – Manish Nov 04 '16 at 14:42
  • @Manish Thank you for the suggestion! But sadly the other Question is for an Authentication issue... In this case i have already authenticate my Google App Work account and indeed i sent transactional emails to user (also the very firsts 172 email are sent when i run my php artisan command) – uTombou Nov 04 '16 at 14:51
  • Did you check that for particular domain these mails send (like only works for gmail and gmail attached web domain) and doesn't work for outside domain. Please check and response me. – Manish Nov 04 '16 at 15:06
  • @Manish I think this is not a Domain problem 'cause it works very well with transactional mailing. We have user with gmail.com, hotmail.com, yahoo.com, custom_domain.com... I mean, it works with normally! – uTombou Nov 04 '16 at 15:24
  • @uTombou Did you check for any errors on the API level? were you running the following command after every change? "php artisan config:cache" – George Nov 30 '16 at 21:54

3 Answers3

1

Some person have same as issue you are getting. Try to do the following may be below procedure will solve your issue.

A.) login from gmail and visited the link https://www.google.com/settings/security/lesssecureapps and turned on less secure apps. B.) Edit .env file as like below:

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=username //i.e. username@gmail.com

MAIL_PASSWORD=password //Gmail accounts password

MAIL_ENCRYPTION=ssl

C.) In you Controller, Write down as following:

$rawData = request::all();

Mail::queue('program.meeting.emailInvite', $rawData, function($message) use ($rawData)

{

$message->from('info@echosofts.com', 'Echosofts')->to(array_map('trim', explode(',', $rawData['all_email_id'])))->subject($rawData['mail_title']);

});

Then email was working fine except the sender email ID was my google account (username@gmail.com) instead of info@cgindians.com.

D.) To overcome the sender email changing problem, I visited my google account and do the following: "Setting icon"-> Settings -> Accounts and Import->Send mail as->Add another email address your own.

Manish
  • 3,443
  • 1
  • 21
  • 24
  • Hi @Manish Thanks for take a time to research! well.. I already made the "lesssecureapps" thing... (that's how i can send email from my laravel app in first place). Let me try the others recommendations! The only thing i am concern about is: sending email that way all recipients will be visibles for others, right? `to(array_map('trim', explode(',', $rawData['all_email_id'])))` – uTombou Nov 04 '16 at 15:46
0

I think sometimes this stuff just happens and are usually network related issues. The best you can do is drop your code into a try/catch block, log when it happens, and attempt the email again.

Sometimes it helps to drop a sleep($n) and let it sleep for a second before continuing.

Also keep an eye on the logs to see if you can spot any patterns in the future that might give you some idea of what's going wrong. If it is a network issue, there isn't a lot besides this that can be done.

user1669496
  • 32,176
  • 9
  • 73
  • 65
0

Please try first Go to

vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mailer.php

And add $this->_transport->stop();

just before return $sent;.

The final code looks like this.

public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $failedRecipients = (array) $failedRecipients;

        if (!$this->_transport->isStarted()) {
            $this->_transport->start();
        }

        $sent = 0;

        try {
            $sent = $this->_transport->send($message, $failedRecipients);
        } catch (Swift_RfcComplianceException $e) {
            foreach ($message->getTo() as $address => $name) {
                $failedRecipients[] = $address;
            }
        }
        $this->_transport->stop();
        return $sent;
    }

Also check this link https://github.com/mustafaileri/swiftmailer/commit/d289295235488cdc79473260e04e3dabd2dac3ef

Manish
  • 3,443
  • 1
  • 21
  • 24