Question

I have a form on my website which will send me an email using PHP. It will be sent to an email address from my hosting service, which I never check, so I set it up to forward emails sent to that address to my iCloud email address. This works great, since it preserves the "From" address so when I reply, it is sent to the person who filled out the form. The problem is, when I reply to the email sent from the form, the person who gets that reply will see it came from my iCloud account, and replies to that email will go directly to my iCloud account rather than to my hosting service email.

My question is, is there a way to specify in my PHP code what email should appear in the "From" field upon replying to the email (but of course, this is different from the "From" address in the first email sent to me (the person filling out the form)?

Essentially, I want it to work like this:
-Upon form submission, an email is sent to example@myhostingservice.com with the "From" address set to the email inputted in the form
-example@myhostingservice.com forwards the email to my iCloud account (but preserves the email address of person who filled out form in the "From" address)
-I reply to that email using my iCloud account, and the receiver sees it came from example@myhostingservice.com (not iCloud), and when they reply it goes to example@myhostingservice.com

Here is a snippet of my code to see what I'm working with:

$name = $_POST['name']; 
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "$subject";
    $email_body = "Name: $name \n \n $message"; 

    $headers = "From: $email_address";

    mail($to,$email_subject,$email_body,$headers);
} 

Thanks!

Was it helpful?

Solution 2

I don't think this is possible using PHP - the email client you use on your iCloud account sets the new "Reply-To". However, try to add the hosting email account to your iCloud email client -(Using POP3 or IMAP)- then you will be able to "Send as" your hosting account and you won't need to forward everything to iCloud.

OTHER TIPS

From docs (http://php.net/manual/en/function.mail.php):

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Although I must warn you, doing such may mark your email as spam since the email address it is being sent from does not match the From header.

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