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

发布于 2024-09-15 21:28:29 字数 1984 浏览 11 评论 0原文

这是我的代码:

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

运行后出现此错误...

致命错误:未捕获异常“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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

聆听风音 2024-09-22 21:28:30

鉴于这个问题的年龄,我最近不得不经历这个过程,所以我想向仍然来到这里的人确认这一切仍然相关^^,但这里有一个关于如何启用两步验证的完整指南,获取您的应用程序密码并发送电子邮件。

composer require swiftmailer/swiftmailer

如前所述使用 Gmail 时,您需要在 Google 帐户上启用两步验证并创建应用程序密码。

如何进入您的 Google 帐户的两步验证

点击 9 个点 >安全>两步验证>开始使用
输入您的手机,然后验证设备。

启用后,您需要转到两步验证,然后滚动到底部并找到应用程序密码。

输入图片此处描述

创建一个新密码,然后在代码中使用它,如下所示:

在此处输入图像描述

<?php
require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
          ->setUsername('[email protected]')
          ->setPassword('yourapppassword');
$message_text = "Hello User,\n\This is the body of your email message";
$message = (new Swift_Message('Your Email Subject'))
->setFrom(['[email protected]' => 'Your From Name'])
->setBody($message_text);
$mailer = new Swift_Mailer($transport);
$message->setTo('[email protected]');
$result = $mailer->send($message);
?>

Given the age of the question I have just recently had to go through this process so I would like to confirm for anyone still coming here that it is all still relavent ^^ but here is a full guide on how to enable 2-Step Verification, get your App Password and send an email.

composer require swiftmailer/swiftmailer

When using gmail as mentioned previously you need to have 2-Step Verification enabled on your Google Account and create an App Password.

How to get to 2-Step Verification on your Google Account

Click on the 9 dots > Security > 2-Step Verification > Get Started
Enter your mobile and then verify the device.

Once you have enabled it you need to go to 2-Step Verification and then scroll to the bottom and find App Passwords.

enter image description here

Create a new password and then use it in your code as below:

enter image description here

<?php
require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
          ->setUsername('[email protected]')
          ->setPassword('yourapppassword');
$message_text = "Hello User,\n\This is the body of your email message";
$message = (new Swift_Message('Your Email Subject'))
->setFrom(['[email protected]' => 'Your From Name'])
->setBody($message_text);
$mailer = new Swift_Mailer($transport);
$message->setTo('[email protected]');
$result = $mailer->send($message);
?>
爱格式化 2024-09-22 21:28:29

GMail 的 SMTP 需要加密。使用:

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

GMail's SMTP requires encryption. Use:

Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl");
夏末染殇 2024-09-22 21:28:29

缺少 ssl 参数,应该是类似的内容经过

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

测试并且工作正常

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

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

Tested and work fine

ㄖ落Θ余辉 2024-09-22 21:28:29

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

GMAIL 的 SMTP 是:smtp.googlemail.com

完整代码:

<?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';
}
?>

Swift SmtpTransport - Code (send a email)

The SMTP of GMAIL is: 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';
}
?>
追星践月 2024-09-22 21:28:29

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

祝你好运!

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.

Best of luck!

土豪 2024-09-22 21:28:29

我已经设法在没有 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]');

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

$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]');
你是我的挚爱i 2024-09-22 21:28:29

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

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

希望有帮助。

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.

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

Hope that helps.

枯寂 2024-09-22 21:28:29

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

有用!!谢谢..:D

这是我的代码:

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

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

IT WORKS!! thanks..: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);
?>
丢了幸福的猪 2024-09-22 21:28:29

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

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://stackoverflow.com/a/25238515/947370

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文