代码之家  ›  专栏  ›  技术社区  ›  Kaley36

在开发模式下以Symfony 3发送电子邮件

  •  0
  • Kaley36  · 技术社区  · 7 年前

    这是我的config_dev.yml文件:

    swiftmailer:
    transport: '%mailer_transport%'
    host: '%mailer_host%'
    username: '%mailer_user%'
    password: '%mailer_password%'
    spool: { type: memory }
    

    和我的配置。yml文件:

    传输:'%mailer\u transport%'
    主机:'%mailer\u host%'
    假脱机:{type:memory}
    

    ....
    mailer_transport: gmail
    mailer_host: 127.0.0.1
    mailer_user: 'my_gamil_here'
    mailer_password: 'my_password_here'
    

    然后在我的控制器中,我有以下内容:

    public function indexAction(\Swift_Mailer $mailer)
    {
        $message = (new \Swift_Message('Hello Email'))
            ->setFrom('westtexascentral555@gmail.com')
            ->setTo('kaleyaw@yahoo.com')
            ->setBody(
                $this->renderView(
                    // app/Resources/views/Emails/registration.html.twig
                    'Emails/registration.html.twig',
                    array('name' => 'James')
                ),
                'text/html'
            );
    
        $mailer->send($message);
    
        return new Response('Email sent');
    }
    

    谁能告诉我出了什么问题吗。没有向电子邮件帐户发送任何内容。

    3 回复  |  直到 5 年前
        1
  •  1
  •   ArtOsi    7 年前

    http://symfony.com/doc/current/email/gmail.html . 请注意这一部分:

    gmail传输只是使用smtp传输的快捷方式 并设置以下选项:

    encryption  ssl
    
    auth_mode   login
    
    host    smtp.gmail.com
    
        2
  •  0
  •   gintko    7 年前
    1. mailer_user 仅设置为您的gmail用户名(不含主机部分)。假设我有电子邮件 myemail@gmail.com ,我的用户名是 myemail .

    2. 你对你的gmail帐号使用两步验证吗?如果是这样,您还需要为您的帐户生成应用程序密码,如中所述 docs :

    generate an App password mailer_password 参数您还必须确保 allow less secure apps to access your Gmail account .

    很快,您可以跟随 here ,登录,选择应用程序(在本例中 Mail )要使用mailer的设备,请按 Generate snub udpz xcvt jrdp

    如果这没有帮助,还可以检查日志,通常如果smtp服务器出现问题,swiftmailer会将错误打印到日志中。

        3
  •  0
  •   YaatSuka    7 年前


    尝试使用“smtp”而不是“gmail”: