Search the blog

SwiftMailer is a superb library that handles all the nuances of sending emails with PHP. There is more than one way to send emails and the easiest way used to be to use Swift_MailTransport. However, you may now see this message:

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

Like it or not, Swift_MailTransport is now deemed insecure and the authors of SwiftMailer are making us use something else, namely Sendmail or SMTP.

You may not want to (or can’t) add SMTP credentials to your application. So how do you use Sendmail instead? I was surprised to see the documentation was quite poor. Here is how I got it working.

The first thing you need is the path to Sendmail. This can differ from server to server so it’s best to use ini_get('sendmail_path') as the starting point and default to SwiftMailer’s recommended /usr/sbin/sendmail -bs if this isn’t set.

$sendmailPath = ini_get('sendmail_path');

// If not set or available default to what Swift recommend
if ($sendmailPath === false || $sendmailPath === '') {

    $sendmailPath = '/usr/sbin/sendmail -bs';

}

Now simply set the transport type:

$transport = new \Swift_SendmailTransport($sendmailPath);
$mailer = \Swift_Mailer::newInstance($transport);
// Now compose your message and send as normal

I also found when using Yii 2 the recommened way to use Sendmail did not work – but this did:

// Get $sendmailPath as above
Yii::$app->mailer->setTransport(new \Swift_SendmailTransport($sendmailPath));
Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.