简体   繁体   中英

Unable to send E-mails using Swift Mailer

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")

->setUsername('username')
->setPassword('password');

$username = $_SESSION['username'];  
$from = $_POST['from'];
$to =  $_POST['to'];
$subject = $_POST['subject'];
$body = $_POST['message'];

$message = Swift_Message::newInstance($subject)

->setFrom(array($from => $username))
->setTo(array($to))
->setBody($body);

$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

Error returned on web browser:

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #157919304]' in C:\\Program Files\\xampp\\htdocs\\healthcare system\\swift\\lib\\classes\\Swift\\Transport\\StreamBuffer.php:259 Stack trace: #0 C:\\Program Files\\xampp\\htdocs\\healthcare system\\swift\\lib\\classes\\Swift\\Transport\\StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 C:\\Program Files\\xampp\\htdocs\\healthcare system\\swift\\lib\\classes\\Swift\\Transport\\AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 C:\\Program Files\\xampp\\htdocs\\healthcare system\\swift\\lib\\classes\\Swift\\Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 C:\\Program Files\\xampp\\htdocs\\healthcare system\\mail_process.php(32): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in C:\\Program Files\\xampp\\htdocs\\healthcare system\\swift\\lib\\classes\\Swift\\Transport\\StreamBuffer.php on line 259

Replace the below code:

$message = Swift_Message::newInstance('$subject')

->setFrom(array('$from' => '$username'))
->setTo(array('$to'))
->setBody('$body');

With

$message = Swift_Message::newInstance($subject)

->setFrom(array($from => $username))
->setTo(array($to))
->setBody($body);

As here you are using the single quotation mark that is the problem in your code.

replace this

->setUsername('username')

->setPassword('password');

with

$transport->setUsername('username'); /* note: user name is your gmail username(eg
asaa@gmail.com) */

$transport->setPassword('password'); / password is your gmail password /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM