【问题标题】:Sending an email using Gmail from localhost in Symfony 3在 Symfony 3 中使用 Gmail 从本地主机发送电子邮件
【发布时间】:2017-04-26 08:06:25
【问题描述】:

我最近遇到了以下问题,我无法在 Symfony 3 中使用 Gmail 和 Swiftmailer 从本地主机发送任何邮件

这是我在parameters.yml中的设置

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: xxxx@gmail.com
mailer_password: xxxxx
mailer_encryption: tls
mailer_port: 587

这里是 config_dev.yml 设置

swiftmailer:
    transport: gmail
    username:  '%mailer_user%'
    password:  '%mailer_password%'

以及示例代码

/**
 * @Route("/mail/{name}")
 */
public function mailAction($name)
{

    echo 'tete';

    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('konrad@mydomain.de')
        ->setTo('info@mydomain.de')
        ->setBody(
            $this->renderView(
            // app/Resources/views/Emails/registration.html.twig
                'email/registration.html.twig',
                array('name' => $name)
            ),
            'text/html'
        )
        /*
         * If you also want to include a plaintext version of the message
        ->addPart(
            $this->renderView(
                'Emails/registration.txt.twig',
                array('name' => $name)
            ),
            'text/plain'
        )
        */
    ;
    $this->get('mailer')->send($message);

    return $this->render('main/homepage.html.twig');
}

在我尝试发送一些测试邮件后,邮件只是假脱机,我没有看到任何错误或类似情况。

此外,我已经使用 PHPMailer 使用上面的设置在本地对其进行了测试,并且在该库中发送按预期工作。

当我在本地工作时,是否需要设置更多内容?

【问题讨论】:

  • 主机 IP 应该是 Gmail SMTP
  • 请添加您的发送代码
  • @Namoz 代码取自 symfony 文档,因此没有添加我这边的额外代码。我会将代码放在第二个答案中

标签: php symfony phpmailer swiftmailer


【解决方案1】:

Gmail 传输只是使用 SMTP 传输的快捷方式 并设置这些选项:

Option   Value 
 encryption  ssl
 auth_mode login 
 host   smtp.gmail.com

你理解错了,因为你定义了传输 SMTP 参数,但它实际上并没有被使用,因为配置会寻找 Gmail 传输,Symfony 使用 Gmail SMTP 的默认值自动配置,你只需要提供用户名和密码在参数中。

# app/config/config_dev.yml 
swiftmailer: 
     transport: gmail 
     username: '%mailer_user%' 
     password: '%mailer_password%'

%mailer_user%%mailer_password% 预计将在

中定义
# app/config/parameters.yml 
parameters:
# ...
mailer_user: your_gmail_username
mailer_password: your_gmail_password

我假设你没有正确配置,因为parameters.yml 没有mailer_usermailer_password,如果有,你已经覆盖了 Symfony 使用的 Gmail 默认值或者不正确。 无论哪种方式,请查看配置并仅填写适当的值。

phpmailer 也无关紧要,Symfony 使用 Swift 邮件程序。它可以与 phpmailer 一起使用,因为您可能正确设置了参数,但是 Swift 邮件程序会读取配置 .yml 以生成传输对象,如果配置错误,则传输对象的值将是错误的。

【讨论】:

  • 嗯,但它似乎仍然不起作用。我不能不在参数中使用 mailer_transport 或 mailer_host,因为没有这两个我得到"[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException] You have requested a non-existent parameter "mailer_transport". Did you mean this: "mailer_password"?"
  • 非常好!谢谢!
【解决方案2】:

(代表 OP 发布)

上面的代码按预期工作。即使没有指定加密或端口甚至主机。邮件到达我的邮箱,但被我的 SpamAssassin 设置过滤。问题解决了。很抱歉从无到有:)

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2021-02-12
    • 2016-07-22
    相关资源
    最近更新 更多