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('xxx@client.com', 'Your Application')->to('me@me.com');
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('xxx@client.com')
->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(['xxx@client.com' => 'John Doe']);
$message->setTo(['me@me.com'])->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 "xxx@client.com" 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 "xxx@client.com" using 0 possible authenticators
Update
- 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.
- I have another code using a legacy cakephp 2 system that can work with the same SMTP mail server with no issues.
- I have tested my laravel SMTP settings on a google mail. And it works fine.
- My next step for troubleshooting is to see if it's swiftmailer or the rest of laravel where the issue lies. Hence this post.
- 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.
- 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.
- 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.
- Please see What I tried Part I, Part II, Part III, Part IV for details on code used to test the SMTP