【问题标题】:Email not being sent with Swift Mailer using Gmail SMTP使用 Gmail SMTP 的 Swift Mailer 未发送电子邮件
【发布时间】:2018-02-11 17:40:04
【问题描述】:

我正在尝试使用 Gmail SMTP 从 Swift Mailer 发送电子邮件。它会在一段时间内方便地发送电子邮件,但随后完全停止发送,尤其是当我在一天左右后恢复工作时。它显示以下错误:

无法与主机 smtp.gmail.com [#0] 建立连接

以下是我用来发送电子邮件的示例代码:

<?php
    require_once 'lib/swift_required.php';
    try
    {
        echo '<pre>';
        //Generating the Email Content
        $message = Swift_Message::newInstance()
                    ->setFrom(array('myemail@gmail.com' => 'No Reply'))
                    ->setTo(array('recipient@gmail.com' => 'Recipient'))
                    ->setSubject('Test Email')
                    ->setBody("This is a Test Email to check SwiftMailer.");

        // Create the Mail Transport Configuration
        $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
                    ->setUsername('myemail@gmail.com')
                    ->setPassword('appPassword');

        //local domain sending
        $transport->setLocalDomain('[127.0.0.1]');

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

        //Send the email
        $sentFlag = $mailer->send($message);
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }

?>

我正在使用应用密码,并且我在我的 Google 帐户设置中启用了两步验证。我一直在寻找这个问题的解决方案一段时间,我已经浏览了许多其他相关的帖子,但没有找到解决方案。有人请提出一个永久的解决方案。

提前致谢。

【问题讨论】:

  • 尝试将 ('smtp.gmail.com', 465, 'ssl') 更改为 ('smtp.gmail.com', 587) 。让我们看看发生了什么
  • 收到此错误:预期响应代码为 250,但收到代码“530”,消息为“530 5.7.0 必须先发出 STARTTLS 命令。j62sm11357963wmf.4 - gsmtp”

标签: php email gmail swiftmailer


【解决方案1】:

如何通过 Gmail SMTP 使用 Swift Mailer

试试这个代码:

<?php
    require_once 'lib/swift_required.php';
    try
    {
        echo '<pre>';
        //Generating the Email Content
        $message = Swift_Message::newInstance()
                    ->setFrom(array('myemail@gmail.com' => 'No Reply'))
                    ->setTo(array('recipient@gmail.com' => 'Recipient'))
                    ->setSubject('Test Email')
                    ->setBody("This is a Test Email to check SwiftMailer.");

        // Create the Mail Transport Configuration
        $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
                    ->setUsername('myemail@gmail.com')
                    ->setPassword('appPassword')
                    ->setStreamOptions(array(
                     'ssl' => array(
                     'allow_self_signed' => true, 
                     'verify_peer' => false)));

        //local domain sending
        $transport->setLocalDomain('[127.0.0.1]');

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

        //Send the email
        $sentFlag = $mailer->send($message);
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }

?>

希望对你有帮助

【讨论】:

  • 抛出错误:警告:stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证在 C:\wamp64\www\domain 中失败.com\lib\classes\Swift\Transport\StreamBuffer.php 第 94 行
  • @JunaidFarooq 我编辑了代码以允许关闭 ssl 证书。希望对你有帮助
  • 谢谢,@LasVegasCoder。它确实有效,但我想知道在没有认证/对等验证的情况下允许连接是否安全。我目前正在本地主机上尝试它,但我很快就会将它迁移到实时服务器上。我不想有任何安全风险。
  • 迁移到实时服务器后,您可以删除该选项并允许证书验证。不要忘记将此答案标记为已接受以帮助他人。
猜你喜欢
  • 2011-04-01
  • 2021-06-17
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多