2
votes
$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

2
Pls help.... I've been trying so many methods to send Mails by PHP but all has been failing me... This function is crucial for my Final Year Project to run well..... Help is needed! Thank you all in advance.... - Darsshan Nair
This error showed up after doing the syntax corrections given by MIss Poo. This is also the traditional error that I have been getting when I tried to send even test emails via Swift Mailer. Back to ground zero. What has to be configured in PHP? or is it a Swift Mailer config error.... Pls Help - Darsshan Nair

2 Answers

0
votes

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.

0
votes

replace this

->setUsername('username')

->setPassword('password');

with

$transport->setUsername('username'); /* note: user name is your gmail username(eg
[email protected]) */

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