Sending email using Google App and PHP Swift Mailer

Not very long ago I converted one of my site to use Google App email service. Using a third party email service can reduce the load on your own server and eliminate the responsibilities of configuring and maintaining a mail server. Since it’s essentially Gmail, SMTP is supported. I paired it with Swift Mailer, the free PHP mail client and the solution has been quite stable and satisfactory.

Until out of blue I checked the mailbox of the default mailing account: noreply@mydomain.com.

The mailbox is filled with undeliverable emails (which is normal), and surprisingly, a quite amount of user emails. Here is the scenario: my website has an online form that supports one to send message to an email address. So if abc@domain1.com uses the form to send message to xyx@domain2.com, an mail will be delivered to xyz@domain2.com, through Gmail using noreply@mydomain.com. Also to achieve better user experience, when Swift Mailer message is constructed, I also set the “from” address to abc@domain1.com, so when xyz@domain2.com receives the email, the message appears to be from abc@domain1.com directly. The idea is also that when xyz replies, the message goes back to abc.

The problem is in the replying part. You might have guessed or figured out, all the replies went back to noreply@mydomain.com. So basically all responses were lost since no one care about the noreply mailbox.

I looked more carefully into the email header and found the problem: the from address looks up like this: abc@domain1.com<noreply@mydomain.com>.

As you can see abc@domain1.com is only treated as a “display name” and actual email address is still noreply@mydomain.com. So even the from address appears to be correct in a email client, replying to the message sends the response to a black hole.

After identifying the problem I started to tweak the Swift Mailer message but no matter what I try Gmail will always append the noreply address in the from header. This left me in a despair mood. Other options are not good. I can write a program to automatically check the mailbox and forward those emails, or modify the message with some warning to notify the receiver not to reply directly, or just switch to a new email service. All these options are either too complicated (without a good cause), or will negatively affect user experience.

After some more poking around I found this email header option: “Reply-To”. If it does what its name indicates my problem can be solved by adding this header in mail messages. You might be laughing at me right now for not knowing this but before this I had never really studied email headers and I did get a load of information by trying to solve this.

Adding a header field is pretty easy in Swift Mailer and it worked. With the “Reply-To” header the mail client (and webmails that I tested) correctly put the right email address when replying.

It’s always fun to be able to solve a problem in a simple way, before going too far down the other paths. And hopefully this post can help someone else in the same boat.

This entry was posted in my 2 cents. Bookmark the permalink.