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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3536836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:08:18  来源:igfitidea点击:

trying to send mail using swift mailer, gmail smtp, php

phpsmtpswiftmailer

提问by champ

Here is my code:

这是我的代码:

<?php
require_once 'Swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
  ->setUsername('[email protected]')
  ->setPassword('pass');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'MY NAME'))
  ->setTo(array('[email protected]' => '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);
?>

AFter RUNNING GOT THIS ERROR...

运行后出现这个错误......

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

致命错误:未捕获的异常 'Swift_TransportException' 与消息 '预期响应代码 220 但在 /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

回答by Maerlyn

GMail's SMTP requires encryption. Use:

GMail 的 SMTP 需要加密。用:

Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl");

回答by osos

there is missing the ssl parameter, it should be something like that

缺少 ssl 参数,它应该是这样的

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

Tested and work fine

经测试并正常工作

回答by Fernando

Swift SmtpTransport - Code (send a email)

Swift SmtpTransport - 代码(发送电子邮件)

The SMTP of GMAIL is: smtp.googlemail.com

GMAIL 的 SMTP 是:smtp.googlemail.com

The Full Code:

完整代码:

<?php
$pEmailGmail = '[email protected]';
$pPasswordGmail = '********';
$pFromName = 'MundialSYS.com'; //display name

$pTo = '[email protected]'; //destination email
$pSubjetc = "Hello MundialSYS"; //the subjetc 
$pBody = '<html><body><p>Hello MundialSYS</p></html></body>'; //body html

$transport = Swift_SmtpTransport::newInstance('smtp.googlemail.com', 465, 'ssl')
            ->setUsername($pEmailGmail)
            ->setPassword($pPasswordGmail);

$mMailer = Swift_Mailer::newInstance($transport);

$mEmail = Swift_Message::newInstance();
$mEmail->setSubject($pSubjetc);
$mEmail->setTo($pTo);
$mEmail->setFrom(array($pEmailGmail => $pFromName));
$mEmail->setBody($pBody, 'text/html'); //body html

if($mMailer->send($mEmail) == 1){
    echo 'send ok';
}
else {
    echo 'send error';
}
?>

回答by Neo

I have managed to get this working without the SSL, here is how:

我已经设法在没有 SSL 的情况下使其正常工作,方法如下:

$transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
            ->setUsername('[email protected]')
            ->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance($subject)
            ->setFrom(array($emailTo=>$name))
            ->setTo(array($emailTo=>'Neo Nosrati'))
            ->addPart($body,'text/plain')
            ->setReturnPath('[email protected]');

回答by David Conde

I cannot be sure, but I think that Gmail's port is 587 using TLS, which is not SSL, but a newer version of it. You should check into that, because I think you are placing the wrong construction code.

我不能确定,但​​我认为 Gmail 的端口是 587 使用 TLS,这不是 SSL,而是它的更新版本。您应该检查一下,因为我认为您放置了错误的构造代码。

Best of luck!

祝你好运!

回答by darksoulsong

I'm using the "Messages Swift Mailer" bundle in Laravel 3 and having the same issue. After some testing, in my case, the solution was to set the same email address that I used in the SMTP authentication on the "from" parameter.

我在 Laravel 3 中使用了“Messages Swift Mailer”包并且遇到了同样的问题。经过一些测试,就我而言,解决方案是在“from”参数上设置与我在 SMTP 身份验证中使用的电子邮件地址相同的电子邮件地址。

I was trying to use a different address and that was triggering the "swiftmailer expected response code 220 but got code with message"error.

我试图使用不同的地址,这触发了“swiftmailer 预期响应代码 220 但收到带有消息的代码”错误。

Hope that helps.

希望有帮助。

回答by matz

I got same error before and i added "ssl" parameter in Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") like osos said.

我之前遇到过同样的错误,我像 osos 所说的那样在 Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") 中添加了“ssl”参数。

IT WORKS!! thanks..:D

有用!!感谢:D

this is my code:

这是我的代码:

<?php
require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('[email protected]')
  ->setPassword('XXXXXXX');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('THIS IS THE SUBJECT')
  ->setFrom(array('[email protected]' => 'YOUR NAME'))
  ->setTo(array('[email protected]' => '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);
?>

回答by squarecandy

For google apps, in addition to setting to port 465 and ssl as recommended in the accepted answer, you may have to enable allow less secure appssetting, as per https://stackoverflow.com/a/25238515/947370

对于谷歌应用程序,除了按照已接受的答案中的建议设置为端口 465 和 ssl 外,您可能还必须按照https://stackoverflow.com/a/25238515/947370启用允许安全性较低的应用程序设置