4

I would like to add a failed message re-direct to my mail script, so that if a user enters a wrong address in the email field it gets returned to me, and not to my hosting company's inbox, how do I do that? I've already added return-path but doesn't work, what else can i do to get this code to work.

Here is the code:

<?php if(isset($_POST['submit'])) { 
if(trim($_POST['first-name']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['first-name']);
}

//Check to make sure that the last name field is not empty
if(trim($_POST['last-name']) == '') {
    $hasError = true;
} else {
    $lname = trim($_POST['last-name']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
    $hasError = true;
    } else {
    $email = trim($_POST['email']);
}

//Check to make sure that the phone field is not empty
if(trim($_POST['tel']) == '') {
    $hasError = true;
} else {
    $phone = trim($_POST['tel']);
}

//Check to make sure that the phone field is not empty
if(trim($_POST['company']) == '') {
    $hasError = true;
} else {
    $company = trim($_POST['company']);
}



foreach (array($_POST['q1'])  as  $value)  {
 $q1 = $value[0];
 $q2 = $value[1];
 $q3 = $value[2];
}



//If there is no error, send the email
if(!isset($hasError)) {

    $to = 'me@host.com';
    $recipient = $email;


    $subject = 'Subject';

    $headers = "From: Me\n" . $to . "\r\n";
    $headers .= "Reply-To: ". $to . "\r\n";
    $headers .= "Return-Path: ". $to . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $msg1 = "First Message";


    $msg2 = "Second Message";

    //Send Email
    mail($recipient, $subject, $msg2, $headers);
    mail($to, $subject, $msg1, $headers);

    $emailSent = true;
    }
}
?>
Beto
  • 705
  • 2
  • 11
  • 26

1 Answers1

3

the return path is set by the mta based on the envelope sender, you can't just set that in the headers yourself. You can try to set the envelope sender using the $additional_parameters argument of the mail function. See Example #3 on http://www.php.net/manual/en/function.mail.php

In your case, that would be something like

mail($recipient, $subject, $msg2, $headers, "-f $to");

On some systems overriding the envelope sender using -f is restricted. In that case you'd probably have to switch to submitting the mail via SMTP instead of calling the sendmail binary via mail().

Gryphius
  • 68,120
  • 6
  • 44
  • 51
  • how do I make the switch to SMTP? – Beto Aug 03 '11 at 21:24
  • use any existing php smtp library like phpmailer – Gryphius Aug 03 '11 at 21:29
  • in that case, the SMTP approach is probably your best bet. have a look at http://phpmailer.worxware.com – Gryphius Aug 05 '11 at 05:46
  • Dumb question but what's the envelope or mail envelope? – Beto Aug 05 '11 at 21:09
  • the envelope sender/recipient address is specified in the smtp protocol ("MAIL FROM:" / "RCPT TO:"). the envelope doesn't have to match the sender / recipient in the headers. the sender/recipient in the headers is basically only used for display purposes, eg. what the target mail program shows as sender/recipient. but for the mail routing path / bounce adress the envelope is important. – Gryphius Aug 06 '11 at 07:27
  • How do I set the evalope, this is how I've done it, this should work: $headers = "From: ". $to . "\r\n"; $headers .= "Reply-To: ". $to . "\r\n"; $headers .= "Return-Path: ". $to . "\r\n"; – Beto Aug 08 '11 at 16:58
  • Thanks Gryphius, your solution helped, I did have to modify the fifth parameter do get it to work properly like so: mail($recipient, $subject, $msg2, $headers, "-f".$to); – Beto Aug 09 '11 at 23:12