问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26018224/swiftmailer-not-sending-emails-in-symfony-2-5