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: firstname.lastname@example.org.
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 email@example.com uses the form to send message to firstname.lastname@example.org, an mail will be delivered to email@example.com, through Gmail using firstname.lastname@example.org. Also to achieve better user experience, when Swift Mailer message is constructed, I also set the “from” address to email@example.com, so when firstname.lastname@example.org receives the email, the message appears to be from email@example.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 firstname.lastname@example.org. 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: email@example.com<firstname.lastname@example.org>.
As you can see email@example.com is only treated as a “display name” and actual email address is still firstname.lastname@example.org. 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.