2015/04/11

How to send mail from PHP using swiftmailer library ?

Download swiftmailer library from here
require_once "swiftmailer/swiftmailer/lib/swift_required.php";
private function send_mail($total_google_num){
$subject = 'Subject';
$from = array('example_1@gmail.com' =>'Your Name');
$to = array('example_2@gmail.com' => 'Recipient1 Name');
$text = "text message";
$html = "html message";
$transport = \Swift_SmtpTransport::newInstance("smtp.gmail.com")
->setPort(465)
->setEncryption('ssl')
->setUsername(your_gmail_username)
->setPassword(your_gmail_password);
$swift = \Swift_Mailer::newInstance($transport);
$message = (new \Swift_Message($subject))
->setFrom($from)
->setBody($html, 'text/html')
->setTo($to)
->addPart($text, 'text/plain');
$failures = null;
if ($recipients = $swift->send($message, $failures))
{
echo 'Message successfully sent!';
} else {
echo "There was an error:\n";
print_r($failures);
}
}
view raw gistfile1.php hosted with ❤ by GitHub

No comments:

Post a Comment