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:
You could alternatively use a different transport such as Sendmail or Mail:
$transport = Swift_SendmailTransport::newInstance(‘/usr/sbin/sendmail -bs’);
$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(‘firstname.lastname@example.org’ => ‘John Doe’))
->setTo(array(‘email@example.com’, ‘firstname.lastname@example.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.