Send e-mails from PHP via Gmail using Swift Mailer [PHP]
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.
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 email@example.com. I put Swift Mailer in the following directory:
~/public_html/Swift. Here is the PHP code:
setUsername('firstname.lastname@example.org') ->setPassword('pass'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('Wonderful Subject') ->setFrom(array('email@example.com' => 'MY NAME')) ->setTo(array('firstname.lastname@example.org' => '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); ?>
This post is based on this thread.
Some related links, I didn’t test them: