SwiftMailer在Symfony 2.5中不发送电子邮件 [英] SwiftMailer not sending emails in Symfony 2.5
问题描述
这是我目前的 parameters.yml
swiftmailer:
transport:smtp
username:username(确认我输入正确)
密码:密码(确认我正在输入)
主机:mail.domain.com
端口:26
auth_mode:login
上述设置不会发生任何错误,它只是提交表单并显示成功消息,但没有收到邮件
我甚至尝试使用Gmail的其他几种组合,如下所示,但这会返回用户身份验证错误,我一次又一次地确保我输入正确的登录详细信息,并确保他们是正确的因为通过浏览器我可以登录。
config.yml:
取值wiftmailer:
运输:%mailer_transport%
加密:%mailer_encryption%
auth_mode:%mailer_auth_mode%
主机:%mailer_host%
用户名:%mailer_username%
密码:%mailer_password%
spool:{type:memory}
参数.yml:
mailer_transport:smtp
mailer_encryption:ssl
mailer_auth_mode:login
mailer_host:smtp.gmail.com
mailer_username:myusername@gmail.com
mailer_password:mypassword
这是我的控制器看起来是生成电子邮件
public function indexAction(Request $ request)
{
$ inquiry = new Inquiry();
$ form = $ this-> createForm(new EnquiryType(),$ inquiry);
$ form-> handleRequest($ request);
if($ form-> isValid()){
$ message = \Swift_Message :: newInstance()
- > setSubject('联系询问')
- > setFrom('username@domain.com')
- > setTo('username@domain.com')
- > setBody(
$ this-> ; renderView('ContactBundle:Default:contactEmail.txt.twig',array('inquiry'=> $ queries))
);
// print_r($ message);
$ this-> get('mailer') - > send($ message);
$ this-> get('session') - > getFlashBag() - > add(
'notice',
'您的联系询问已成功发送,谢谢!
);
return $ this-> redirect($ this-> generateUrl('contact_form'));
}
return $ this-> render(
'ContactBundle:Default:contact.html.twig',
array(
'form' => $ form-> createView()
)
);
}
如果我能在这里获得一些帮助,我将非常感谢我现在完全没有意义。
我已经在两家公司的SMTP和Gmail上工作了
对于公司SMTP
config.yml:
swiftmailer:
transport:%mailer_transport%
加密:%mailer_encryption%
auth_mode:%mailer_auth_mode%
主机:%mailer_host%
用户名:%mailer_username%
密码:%mailer_password%
spool:{type:memory}
parameters.yml:
mailer_transport:smtp
mailer_host:mail.domain.com
mailer_user:username@domain.com
mailer_password:密码
对于Gmail config.yml
设置保持不变,但在 parameters.yml
进行以下更改
mailer_transport:gmail
mailer_host:smtp.gmail.com
mailer_user: username@gmail.com
mailer_password:密码
然后访问这个链接,以允许应用程序登录到您的帐户以发送邮件。
您还可以使用此链接查看任何可疑的登录活动
注意:
在您的控制器中,发送电子邮件 - > setFrom('username@gmail.com')
应该匹配生成电子邮件的电子邮件ID,否则您不会收到电子邮件,我看过这个并浪费了2天。
$ message = \Swift_Message :: newInstance()
- > setSubject ('联系询问')
- > setFrom('username@gmail.com')\\将其配置到parameters.yml中的mailer_user
- > setTo('username@gmail.com ')
- > setBody(
$ this-> renderView('ContactBundle:Default:contactEmail.txt.twig',array('inquiry'=> $ inquiry))
);
这是我如何得到它的工作:)终于,希望这将帮助一个人在那里卡住。我可以确认这个解决方案适用于开发和生产服务器。
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. 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
This is my current 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.
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:
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
For company SMTP
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:
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
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. 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.
$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.
这篇关于SwiftMailer在Symfony 2.5中不发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!