All writing

SwiftMailer - Sending Mail in PHP With Ease

The other day someone was looking for help with sending emails in PHP. I’ll be honest, the methodology for sending mail in PHP is haphazard to say the least. At least with the built in functionality. The documentation is lacking and there are a lot of undocumented pitfalls.

This is where SwiftMailer comes in.

SwiftMailer is an OOP based PHP5 library for sending email. It supports multiple SMTP servers, batch sending mail, etc. It doesn’t require any php.ini configurations, and its very straight forward. If there are errors, it throws exceptions instead of returning weird functionality. I highly recommend it for sending email in PHP. There are other mailer libraries, however I really like SwiftMailer the best. It has great documentation, and every PHP developer should check it out.

Here is an example on sending an email:

setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

//Mail
$transport = Swift_MailTransport::newInstance();
*/

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

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$result = $mailer->send($message);

/*
You can alternatively use batchSend() to send the message

$result = $mailer->batchSend($message);
*/

?>

There you go! Its pretty darn slick. Hope it helps anyone out there.

Sheet
A-3 · Detail
Drawn
Utah
Scale
1:1
Rev
2026