简体   繁体   English

SwiftMailer没有在Symfony 2.5中发送电子邮件

[英]SwiftMailer not sending emails in Symfony 2.5

After spending 2 days trying to get it to work, I finally decided to ask here. 在花了两天时间使它工作之后,我终于决定在这里询问。 I am trying to generate an email once the user submits the contact form, upon reading the documentation on Symfony website and countless other articles on stackoverflow and google I just cant get it to work. 一旦用户提交联系表格,我就试图生成一封电子邮件,在阅读Symfony 网站上的文档以及关于stackoverflow和google的无数其他文章时,我无法正常工作。 Looking at the instruction it looks like it should be pretty straight forward but none of the solutions are working. 看一下指令,看起来应该很简单,但是所有解决方案都没有用。 I tried sending emails via Gmail and via our company SMTP but both did not work 我尝试通过Gmail和我们公司的SMTP发送电子邮件,但都无法正常工作

This is my current parameters.yml 这是我当前的parameters.yml

swiftmailer:
   transport:            smtp
   username:             username (confirmed that i am entering it right)
   password:             password (confirmed that i am entering it right)
   host:                 mail.domain.com
   port:                 26
   auth_mode:            login 

The above setting does not give any error, it just submits the form and displays success message but no mails are recieved 上面的设置没有给出任何错误,它只是提交表单并显示成功消息,但是没有收到邮件

I even tried several other combination like the one below using Gmail but this returns user authentication error, I made sure over and over and over that I am entering the right login details and 100% sure they are correct because via browser i can login. 我什至尝试了其他几种组合,例如以下使用Gmail的组合,但这会返回用户身份验证错误,我一遍又一遍地确保输入正确的登录详细信息,并确保100%正确,因为我可以通过浏览器登录。

config.yml: config.yml:

swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode:  %mailer_auth_mode%
    host:      %mailer_host%
    username:  %mailer_username%
    password:  %mailer_password%
    spool:     { type: memory }

parameters.yml: parameters.yml:

mailer_transport:  smtp
mailer_encryption: ssl
mailer_auth_mode:  login
mailer_host:       smtp.gmail.com
mailer_username: myusername@gmail.com
mailer_password: mypassword

This is what my controller looks like which is generating email 这就是我的控制器生成电子邮件的样子

public function indexAction(Request $request)
    {
        $enquiry = new Enquiry();
        $form = $this->createForm(new EnquiryType(), $enquiry);

        $form->handleRequest($request);

        if ($form->isValid()) {
            $message = \Swift_Message::newInstance()
                ->setSubject('Contact enquiry')
                ->setFrom('username@domain.com')
                ->setTo('username@domain.com')
                ->setBody(
                    $this->renderView('ContactBundle:Default:contactEmail.txt.twig', array('enquiry' => $enquiry))
                );
            //print_r($message);
            $this->get('mailer')->send($message);
            $this->get('session')->getFlashBag()->add(
                'notice',
                'Your contact enquiry was successfully sent. Thank you!'
            );

            return $this->redirect($this->generateUrl('contact_form'));
        }

        return $this->render(
            'ContactBundle:Default:contact.html.twig',
            array(
                'form' => $form->createView()
            )
        );

    }

I will really appreciate if I can get some help here as I am completely clueless right now. 如果能在这里获得帮助,我将不胜感激,因为我现在一无所知。

I got it to work on both company based SMTP and Gmail 我可以同时在基于公司的SMTP和Gmail上使用

For company SMTP 对于公司SMTP

config.yml: config.yml:

swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode:  %mailer_auth_mode%
    host:      %mailer_host%
    username:  %mailer_username%
    password:  %mailer_password%
    spool:     { type: memory }

parameters.yml: parameters.yml:

    mailer_transport: smtp
    mailer_host: mail.domain.com
    mailer_user: username@domain.com
    mailer_password: password

For Gmail config.yml settings remain the same but in parameters.yml make the following change 对于Gmail config.yml设置保持不变,但在parameters.yml进行以下更改

    mailer_transport: gmail
    mailer_host: smtp.gmail.com
    mailer_user: username@gmail.com
    mailer_password: password

Then visit this link of google to allow apps to login into your account to send mails. 然后访问Google的此链接 ,以允许应用登录您的帐户以发送邮件。 You can also view any suspecious activity of login using this link 您还可以使用此链接查看任何可疑的登录活动

Note: 注意:

In your controller where you are telling the function to send email ->setFrom('username@gmail.com') should match the email id which is generating the email otherwise you will not get an email, I over looked this and wasted 2 days on it. 在您告诉控制器该函数发送电子邮件的控制器中->setFrom('username@gmail.com')应该与正在生成电子邮件的电子邮件ID相匹配,否则您将不会收到电子邮件,我仔细检查了一下并浪费了2天在上面。

 $message = \Swift_Message::newInstance()
                ->setSubject('Contact enquiry')
                ->setFrom('username@gmail.com') \\match this to mailer_user in parameters.yml
                ->setTo('username@gmail.com')
                ->setBody(
                    $this->renderView('ContactBundle:Default:contactEmail.txt.twig', array('enquiry' => $enquiry))
                );

This is how I got it work :) finally, hopefully this will help someone out there stuck. 这就是我的工作方式:)最后,希望这可以帮助某个卡住的人。 I can confirm that this solution works for me on both development and production server. 我可以确认该解决方案在开发和生产服务器上均适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM