尝试使用swift mailer,gmail smtp,php发送邮件

前端之家收集整理的这篇文章主要介绍了尝试使用swift mailer,gmail smtp,php发送邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里是我的代码
<?PHP
require_once 'Swift/lib/swift_required.PHP';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465)
  ->setUsername('me@ff.com')
  ->setPassword('pass');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('me@ff.com' => 'MY NAME'))
  ->setTo(array('you@ss.com' => 'YOU'))
  ->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'),'logo.png');  
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n",$numSent);
?>

在运行时出现此错误

Fatal error: Uncaught exception ‘Swift_TransportException’ with message ‘Expected response code 220 but got code “”,with message “”‘ in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.PHP:406

Stack trace: 
#0 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.PHP(299): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('',Array) 
#1 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.PHP(107): Swift_Transport_AbstractSmtpTransport->_readGreeting() 
#2 /home/sitenyou/public_html/Swift/lib/classes/Swift/Mailer.PHP(74): Swift_Transport_AbstractSmtpTransport->start() 
#3 /home/sitenyou/public_html/sgmail.PHP(16): Swift_Mailer->send(Object(Swift_Message)) 
#4 {main} thrown in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.PHP on line 406
GMail的SMTP需要加密。使用:
Swift_SmtpTransport::newInstance('smtp.gmail.com',465,"ssl");

猜你在找的Swift相关文章