Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to swiftmailer in laravel

I have an issue with using swiftmailer with the client's SMTP microsoft exchange server.

To troubleshoot, I am thinking of changing out the swiftmailer with something else.

My entire setup is on an internal network. So no mailgun or sendgrid at all.

How do I use an alternative library for swiftmailer in Laravel in order to troubleshoot?

Also this is the log I get when I tried to use swiftmailer on Laravel

<< 250-xxx.internal.abc.com Hello [xxx.xx.xxx.xx]
250-SIZE 36700160
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XSHADOW

>> STARTTLS

<< 500 5.3.3 Unrecognized command

!! Expected response code 220 but got code "500", with message "500 5.3.3 Unrecognized command

What I tried part (I)

Somebody asked me to try MAIL_ENCRYPTION to null and dd(config('mail.encryption')), so the below is the answer:

Code:

    try { 
        Mail::send('emails.test', [], function ($m) {
        $m->from('[email protected]', 'Your Application')->to('[email protected]');
        dd(config('mail.encryption'));
        var_dump('executed');
    });
     } catch (Exception $e) {
        var_dump($e->getMessage()); 
        var_dump($e->getTraceAsString());
    }  

Result: null was printed out The string executed was not.

What I tried part (II):

explicitly use swiftmailer transport with encryption set to null

  try {
    // Create the Transport
    $transport = \Swift_SmtpTransport::newInstance();

    $transport->setUsername('[email protected]')
    ->setPort(25)
    ->setHost('smtp.internal.client.com')
    ->setEncryption(null);

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

    $logger = new \Swift_Plugins_Loggers_ArrayLogger();
    $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));


    $message = new \Swift_Message('Wonderful Subject');
    $message->setFrom(['[email protected]' => 'John Doe']);
    $message->setTo(['[email protected]'])->setBody('Here is the message itself');

    // // Send the message
    $numSent = $mailer->send($message);
    } catch(\Swift_TransportException $e) {
        var_dump('test');
        var_dump($e->getMessage()); 
        var_dump($e->getTraceAsString());
    }

Result:

www-data@server:~/html/laravel-app$ php artisan email:send
string(4) "test"
string(127) "Failed to authenticate on SMTP server with username "[email protected]" using 0 possible authenticators"
string(2503) "#0 /var/www/html/a3qp/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(332): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport))

What I tried Part (III):

Same as part II but set encryption to 'tls' explicitly. I get the same error message as the original one at the very top.

What I tried Part (IV):

Someone asked me to take out the dd line in Part I This is what I got

string(8) "executed"
[Swift_TransportException]
Failed to authenticate on SMTP server with username "[email protected]" using 0 possible authenticators

Update

  1. I am using SMTP to a MS Exchange server that is in my client's infrastructure. I have no access to that server's SMTP log, so it's hard to troubleshoot. The code is totally intranet. There is no access to outside internet service and I am not supposed to use mailgun or any other smtp cloud services to send client's transactional emails meant for internal consumption.
  2. I have another code using a legacy cakephp 2 system that can work with the same SMTP mail server with no issues.
  3. I have tested my laravel SMTP settings on a google mail. And it works fine.
  4. My next step for troubleshooting is to see if it's swiftmailer or the rest of laravel where the issue lies. Hence this post.
  5. Yes, I am trying to get hold of the IT team responsible for the Exchange server, but they are in another country. I am still trying though.
  6. I know I am repeating point 2, but the comments suggest that this point is not clear enough. I am using the exact same SMTP settings on a legacy cakephp 2 system to send emails and there is no issue. Because I cannot get hold of the IT team responsible for the exchange server and plus the SMTP settings do work for another cakephp 2 system, I feel I should try to troubleshoot it on the laravel system to find out for sure.
  7. I have tried PHP mail and sendmail by changing the .env file. I did not trigger any error messages but i also appear not to receive any emails.
  8. Please see What I tried Part I, Part II, Part III, Part IV for details on code used to test the SMTP
like image 610
Kim Stacks Avatar asked Jun 01 '17 04:06

Kim Stacks


People also ask

How do you check mail is sent or not in laravel?

You can use the Mail::failures() function for that.


1 Answers

The SMTP server you're attempting to connect to does not understand the STARTTLS command. To prevent the STARTTLS command from being attempted, you must disable encryption by setting your MAIL_ENCRYPTION environment variable to null in your .env file.

The next issue is that, according to the EHLO response, the SMTP server does not accept any authentication methods. If it did, the line 250-AUTH would also include the methods it accepted (e.g. 250-AUTH PLAIN DIGEST-MD5).

To prevent an authentication attempt, you must set your MAIL_USERNAME environment variable to null in your .env file.

To sum up, your .env file should have the following lines:

MAIL_ENCRYPTION=null
MAIL_USERNAME=null

That will disable tls encryption and disable authentication.

like image 193
patricus Avatar answered Sep 24 '22 13:09

patricus