Using PHP'S Swiftmailer With Gmail

Email not being sent with Swift Mailer using Gmail SMTP


How to use Swift Mailer Using Gmail SMTP

Try this code like this:

<?php
require_once 'lib/swift_required.php';
try
{
echo '<pre>';
//Generating the Email Content
$message = Swift_Message::newInstance()
->setFrom(array('myemail@gmail.com' => 'No Reply'))
->setTo(array('recipient@gmail.com' => 'Recipient'))
->setSubject('Test Email')
->setBody("This is a Test Email to check SwiftMailer.");

// Create the Mail Transport Configuration
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
->setUsername('myemail@gmail.com')
->setPassword('appPassword')
->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' => true,
'verify_peer' => false)));

//local domain sending
$transport->setLocalDomain('[127.0.0.1]');

$mailer = Swift_Mailer::newInstance($transport);

//Send the email
$sentFlag = $mailer->send($message);
}
catch (Exception $e)
{
echo $e->getMessage();
}

?>

Hope it helps

Swiftmailer config : send mail using gmail

Looks like your live server is missing OpenSSL, so you need to enable it in order to get secure connections working (i.e. enable the php_openssl module). Also check this question.



Related Topics



Leave a reply



Submit