Archive
Posts Tagged ‘swift mailer’
Send e-mails from PHP via Gmail using Swift Mailer [PHP]
November 21, 2010
Leave a comment
Problem
You have a PHP script from which you want to send e-mails. You want an easy solution, and it’d be nice to send the e-mails via a Gmail account.
Solution
There are several ways to send e-mails from PHP. A sophisticated (and easy) solution is using Swift Mailer, which is an open-source library for sending e-mails from PHP 5 applications. See the docs here.
For my application I created a dedicated Gmail account, I refer to this as me@gmail.com. I put Swift Mailer in the following directory: ~/public_html/Swift. Here is the PHP code:
setUsername('me@gmail.com')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('me@gmail.com' => 'MY NAME'))
->setTo(array('to@destination.com' => 'YOU'))
->setBody('This is the text of the mail sent by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d message(s)\n", $numSent);
?>
Credits
This post is based on this thread.
Links
Some related links, I didn’t test them:
Categories: php
email, gmail, smtp, swift mailer