$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一起建立连接[无法找到套接字传输“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-> start ()#3 C:\ Program Files \ xampp \ htdocs \ healthcare system \ mail_process.php(32):Swift_Mailer-> send(Object(Swift_Message))#4 {main}抛出C:\ Program Files \ xampp \第259行的htdocs \ healthcare system \ swift \ lib \ classes \ Swift \ Transport \ StreamBuffer.php
答案 0 :(得分: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);
在这里,您使用的是单引号,这是代码中的问题。
答案 1 :(得分:0)
替换此
- > setUsername('用户名&#39)
- > setPassword('密码&#39);
带
$ transport-> setUsername('用户名&#39); / *注意:用户名是你的gmail用户名(例如
asaa@gmail.com)* /
$ transport-> setPassword('密码&#39); / 密码是您的Gmail密码 /