Adding Email ‘Reply To’ Functionality to Solspace’s Freeform Module
Smart ExpressionEngine developers leverage the powerful add-ons for ExpressionEngine that are out there for purchase or free. One of the best free add-on modules is Solspace‘s Freeform. From the Solspace Freeform product page:
The Freeform module allows developers of ExpressionEngine websites to easily create forms on the fly and post submissions into the Freeform database. Results can be sorted, filtered, emailed and exported. Attachments can be submitted and stored. Any number of fields can be collected and validated.
That’s a pretty good summary. For me, the module has completely replaced the default ExpressionEngine contact form and I use it for many, many other forms on most of my sites.
One small piece of functionality that it doesn’t have but I regularly wanted to have was the ability, after Freeform delivers an email to your specified recipient(s), to be able to simply click the reply button in my email client and have the reply directed to the person who filled out the form. Natively, Freeform will reply to the sender of the email, which is a static value that you define in the Freeform Templates creation page.
As an example, the contact page on this site sends me an email when you fill it out. In the Freeform template, I have the sender as something@webinception.com. This email address isn’t even real, its just a placeholder since the email actually was sent by my website. So if I click reply when I get an email, the address that the reply will go to is something@webinception.com. Not very productive.
To remedy this, I hacked around in the module and with two simple changes, you can have the same reply to functionality I now use. As a word of caution, if you make this change, future updates by Solspace to the Freeform module will more than likely break this functionality. However, the hack is so simple, I am hoping that Mitchell and crew just make it a feature of the next release. I’d love to just specify replyto=“yes” in the form parameters to make this happen.
With Freeform already installed and working, go to your system folder and then modules/freeform and open the file mod.freeform.php.
Scroll to about line 869 and look for this code:
else
{
$data[$key] = $REGX->xss_clean($val);
}
}
In between those last two closing brackets, insert 4 lines of code so that it looks like this. I use //@Chad for mods like this so that I can quickly find all hacks I have injected into the default code… so you may want to change that to //@boyink or //@casey or //@gorilla or whatever suits you. Here is the finished code:
else
{
$data[$key] = $REGX->xss_clean($val);
}
//@Chad begin replyto hack
if ($key == 'email' ) {
$replyto = $val;
} //end replyto hack
}
Then scroll down to about line 1117. You should see this:
foreach ($recipients as $val)
{
$email->initialize();
$email->from($msg['from_email'], $msg['from_name']);
$email->to($val);
$email->subject($msg['subject']);
$email->message($REGX->entities_to_ascii($msg['msg']));
$email->Send();
}
Just after the $email->to($val); line, you’ll add 2 lines so that it looks like this:
foreach ($recipients as $val)
{
$email->initialize();
$email->from($msg['from_email'], $msg['from_name']);
$email->to($val);
//@Chad begin replyto hack
$email->reply_to($replyto); //End replyto hack
$email->subject($msg['subject']);
$email->message($REGX->entities_to_ascii($msg['msg']));
$email->Send();
}
Again, //@masuga or //@shrimpy may apply better to you.
Save the file and you are done. Now send yourself a test email through the form, and when it arrives in your email box click reply and everything should turn out just peachy.

6 Comments
posted on August 26, 2009 at 3:35pm by Hambo:
Not sure if I’m missing something here but in Freeform, why don’t you just use {email} in the From Email field?
It will populate with whatever the user fills in.
posted on August 26, 2009 at 3:42pm by Chad Crowell:
Somebody tweeted that as well. Never knew it was an option. Now I do… oh well. Might as well just remove this article I suppose.
posted on August 26, 2009 at 3:46pm by Hambo:
Glad to help Chad!
posted on August 30, 2009 at 8:17pm by Chad Crowell:
Always a pleasure hearing from you friend!
So, for those of you reading this, within the Freeform template setup, you can specify {email} to be the From email address, and the user who filled out the form’s email address will then be the reply to. Much much simpler, and something I never knew.
Lesson learned here? There are many ways to do things in EE that work. Sometimes you just end up taking the long road
posted on September 09, 2009 at 4:55pm by Travis:
Don’t delete it, please—there are some rare occasions when you want the Reply-To address not to match the From email address.
Mostly it’s for mailing lists, or for auto-replies, which is exactly what this is. So it’s useful, though not usually necessary.
posted on October 04, 2009 at 6:11am by Arne:
Actually.. this is very useful. As for example my server has spoofing settings disabled. So I need to set @mydomain as “from” address, and “replyto” address as the {email} one. I’m bookmarking this.
Leave a Comment