I'm using the SwiftMailer class to send mail using the php mail() function or SMTP depending on my app's configuration (development or production). My code looks like this :

// Default mailer: php mail() function
$this->transport = \Swift_MailTransport::newInstance();

// If a SMTP host is defined
if (isset($_SITE['site_smtp_host'])) {
    $this->transport = \Swift_SmtpTransport::newInstance($_SITE["site_smtp_host"], 587)
                    ->setUsername($_SITE["site_smtp_user"])
                    ->setPassword($_SITE["site_smtp_pass"]);
    }

Since SwiftMailer 5.4.5 I'm getting this deprecation notice :

Exception: UNKNOWN ERROR (16384): The Swift_Transport_MailTransport class is deprecated since version 5.4.5 and will be removed in 6.0. Use the Sendmail or SMTP transport instead.

Should I use Swift_SendmailTransport as I was using Swift_MailTransport ? Will it work it the same environnements ? Does it also use the php mail() functions ? If not, is it not possible to use the php mail() function with SwiftMailer anymore ?

2

There are 2 best solutions below

0
On BEST ANSWER

First of all, about deprecation from swift mailer site:

It is advised that users do not use this transport if at all possible since a number of plugin features cannot be used in conjunction with this transport due to the internal interface in PHP itself.

The level of error reporting with this transport is incredibly weak, again due to limitations of PHP's internal mail() function. You'll get an all-or-nothing result from sending.

If you need 100% compatible solution, you need to check php.ini settings and OS platform http://php.net/manual/en/mail.configuration.php

For unix platform will be enough to call ->setCommand with ini_get("sendmail_path") value. For windows platform support need to check smtp option.

0
On

This is how it’s done. Note that the Sendmail path is not the same on all servers.

// Get the Sendmail path
$sendmailPath = ini_get('sendmail_path');
$sendmailPath = ($sendmailPath === false || $sendmailPath === '') ? '/usr/sbin/sendmail -bs' : $sendmailPath;

// Create the transport method
$transport = new \Swift_SendmailTransport($sendmailPath);
$mailer = \Swift_Mailer::newInstance($transport); // 5.6 or...
$mailer = new \Swift_Mailer($transport); // ...6.2

// Now compose and send your email