Question

I asked a similar question earlier, but the context has changed a bit. I want to use swiftmailer to send an email in Symfony 2. The problem is I'm using the gmail SMTP server, and so when my message arrives, my email client shows it from gmail.com, rather than mydomain.com. How can I fix this?

$message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom(array('digest@mydomain.com' => "Digest"))
        ->setSender(array('digest@mydomain.com' => "Digest"))
        ->setCharset('iso-8859-1')
        ->setContentType('text/html')
        ->setTo('myemail@gmail.com')
        ->setBody($this->renderView('email.html.twig', array()));

$this->get('mailer')->send($message);
Was it helpful?

Solution

The Gmail SMTP server only allows you to send mail using the email address you're aithenticating with. So if you configured Symfony with

mailer_transport: gmail
mailer_user: myemail@gmail.com
mailer_password: ******

All your messages we'll look like they're from myemail@gmail.com

What you can do maybe is use another smtp server that is more flexible in the types of From addresses they will allow. If you are on a local development server, you could use your ISP's SMTP server, or use one of the various email as a service provider, such as Sendgrid, Mailjet or postmarkapp

They all allow you to send messages from any address you can prove ownership of, after some configuration steps.

One thing that may be worth trying also if you really want to use the gmail smtp server, is to define your from address in your "Send mail as" Gmail configuration (in Settings -> Accounts). This will let your app send email using the configured adresses in the From: field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top