Kausheel Kausheel - 1 year ago 57
PHP Question

Contact form in PHP: Allowing the user to send me an email

On my website I have a contact form, where the user can report bugs or submit feature requests. The form contains a subject and message.

I want the form to email me directly, either with PHPMailer or with PHP's built-in mail function.

The issue here is when generating the email, the sender has to be an address I own, not the actual user's email address. The reason for this is obvious, since we shouldn't be allowed to impersonate other people. However I want to be able to email my users back directly from my inbox, which I can't do since I can't use their address to email myself in the script.

What is the best way to construct the contact form then?
The only workaround I can think of is to insert the message + sender address into a database, and then read it manually from there....an epic hassle.

I would be grateful for any suggestions.

Answer Source

Set the Reply-To e-mail header, so you'll get something like this:

$headers = 'From: you@example.com' . "\r\n";
// Set the sender address as Reply-to below
$headers .= 'Reply-To: sender@example.com' . "\r\n";

mail('you@example.com', 'Subject here', 'The message goes here', $headers);

This way you will be the sender, but when you hit the reply button in your mail client, it will default to reply to the address listed in the Reply-To header.