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

时间:2022-11-15 12:21:56

Here is my code:

这是我的代码:

<?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);
?>

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

8 个解决方案

#1


50  

GMail's SMTP requires encryption. Use:

GMail的SMTP需要加密。使用:

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

#2


7  

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

经过测试,工作正常

#3


3  

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 = 'xxxx@gmail.com';
$pPasswordGmail = '********';
$pFromName = 'MundialSYS.com'; //display name

$pTo = 'xxxxxx@xxxx.xxx'; //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';
}
?>

#4


2  

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!

祝你好运!

#5


2  

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

我已经设法让这个工作没有SSL,这是如何:

$transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
            ->setUsername('contact@columbussoft.com')
            ->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('other@columbussoft.com');

#6


1  

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.

希望有所帮助。

#7


1  

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

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

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('XXXXXXX@gmail.com')
  ->setPassword('XXXXXXX');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('THIS IS THE SUBJECT')
  ->setFrom(array('XXXXXXX@gmail.com' => 'YOUR NAME'))
  ->setTo(array('XXXXXXX@gmail.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);
?>

#8


1  

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 apps setting, as per https://*.com/a/25238515/947370

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

#1


50  

GMail's SMTP requires encryption. Use:

GMail的SMTP需要加密。使用:

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

#2


7  

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

经过测试,工作正常

#3


3  

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 = 'xxxx@gmail.com';
$pPasswordGmail = '********';
$pFromName = 'MundialSYS.com'; //display name

$pTo = 'xxxxxx@xxxx.xxx'; //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';
}
?>

#4


2  

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!

祝你好运!

#5


2  

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

我已经设法让这个工作没有SSL,这是如何:

$transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
            ->setUsername('contact@columbussoft.com')
            ->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('other@columbussoft.com');

#6


1  

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.

希望有所帮助。

#7


1  

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

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

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('XXXXXXX@gmail.com')
  ->setPassword('XXXXXXX');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('THIS IS THE SUBJECT')
  ->setFrom(array('XXXXXXX@gmail.com' => 'YOUR NAME'))
  ->setTo(array('XXXXXXX@gmail.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);
?>

#8


1  

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 apps setting, as per https://*.com/a/25238515/947370

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