2
$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);

网页浏览器返回错误:

致命错误:未捕获的异常 'Swift_TransportException' 带有消息 '无法与主机 smtp.gmail.com 建立连接 [无法找到套接字传输“ssl” - 您在配置 PHP 时是否忘记启用它?#157919304]' 在 C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Transport\StreamBuffer.php:259 堆栈跟踪:#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->

4

2 回答 2

0

替换这个

->setUsername('用户名')

->setPassword('密码');

$transport->setUsername('用户名'); /* 注意:用户名是您的 gmail 用户名(例如
asaa@gmail.com)*/

$transport->setPassword('密码'); /密码是你的gmail密码/

于 2014-03-27T11:51:57.393 回答
0

替换以下代码:

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

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

$message = Swift_Message::newInstance($subject)

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

在这里,您使用的是代码中存在问题的单引号。

于 2013-05-08T05:22:39.397 回答