【问题标题】:Swift mailer mail not sent via SMTP when used gmail account使用 gmail 帐户时未通过 SMTP 发送 Swift 邮件程序邮件
【发布时间】:2017-05-03 14:23:19
【问题描述】:

我有以下代码使用Swift mailer 通过 PHP 发送邮件

<?php
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
            ->setUsername('my.email@mydomain.com')
            ->setPassword('emailpassword');


$message = Swift_Message::newInstance()
      ->setSubject("This is test subject")
      ->setFrom(array("sender.email@gmail.com"))
      ->setTo(array("recipient.email@gmail.com"))
      ->setBody("This is test mail body","text/html");

$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

这很好用,收件人会收到一封电子邮件。但是,如果我像这样将传输中的用户名和密码更改为 gmail 的用户名和密码 -

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
            ->setUsername('sender.email@gmail.com') //this is the change, using gmail
            ->setPassword('senderpassword'); //this is the change

那么这不起作用。

谁能告诉我哪里做错了?我缺少什么配置?

附:我也检查了垃圾邮件文件夹

【问题讨论】:

    标签: php swiftmailer


    【解决方案1】:

    您是否在您的 gmail 帐户中检查过两步验证?关了吗?在开启两步验证之前,我也遇到了同样的问题。我把它关掉然后它工作。也许你可以试一试。

    【讨论】:

    • 我应该在 Gmail 的哪个位置检查两步验证??
    • 打开你的 gmail。然后在右上角的网站上,单击您的个人资料照片,然后单击我的帐户。去登录和安全然后登录谷歌。您可以在右侧看到它。内盒。希望对您有所帮助。
    • 或尝试将 smtp 端口更改为 587
    • less than secure apps也有一个gmail设置。可能值得一试。我将 PHP Mailer 与 Gmail 一起使用,本地和远程都没有问题,我也没有针对该设置进行调整,但它可能会有所帮助。祝你好运...Google - Lees Than Secure Apps
    • @dalelandry,还允许打开不太安全的应用程序
    【解决方案2】:

    唯一有效的方法是解锁验证码

    https://accounts.google.com/UnlockCaptcha

    我从This SO Question 得到了这个答案。查看获得最高点赞的回复。

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 2017-05-12
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多