KDM Posted October 29, 2010 Share Posted October 29, 2010 I have a pretty good php mailer script, but how do I get the senders email? When I test the form, the users email doesn't appear in the "From" field. Anyone know what I need to add to my code? Thanks. <?php if (array_key_exists('send', $_POST)) { // mail processing script // remove escape characters from POST array if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } $from = 'Emailer Form'; $to = 'myEmail.com'; $subject = 'Interested in Urban Style Ballroom Dancing'; // list expected fields $expected = array('name', 'phone', 'email', 'address', 'city', 'state', 'zip', 'emailerMessage'); // set required fields $required = array('name', 'phone', 'email', 'address', 'city', 'state', 'zip', 'emailerMessage'); // create empty array for any missing fields $missing = array(); // process the $_POST variables foreach ($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } // otherwise, assign to a variable of the same name as $key elseif (in_array($key, $expected)) { ${$key} = $temp; } } // go ahead only if all required fields OK if (empty($missing)) { // build the message $message = "name: $name\n\n"; $message .= "phone: $phone\n\n"; $message .= "email: $email\n\n"; $message .= "address: $address\n\n"; $message .= "city: $city\n\n"; $message .= "state: $state\n\n"; $message .= "zip: $zip\n\n"; $message .= "emailerMessage: $emailerMessage\n\n"; // limit line length to 70 characters $message = wordwrap($message, 70); // send it $mailSent = mail($to, $subject, $message); if ($mailSent) { // $missing is no longer needed if the email is sent, so unset it unset($missing); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/ Share on other sites More sharing options...
Pikachu2000 Posted October 29, 2010 Share Posted October 29, 2010 You don't use it as the from: header. You'd set it as the reply-to: header if needed. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128148 Share on other sites More sharing options...
KDM Posted October 29, 2010 Author Share Posted October 29, 2010 You don't use it as the from: header. You'd set it as the reply-to: header if needed. so I would write $reply-to='"; ? what would go into the quotation marks? Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128160 Share on other sites More sharing options...
Ninjakreborn Posted October 29, 2010 Share Posted October 29, 2010 I normally just put the from email (the person sending) as the from address. There is no reason, not to do this. If you do not want to, then just put their email address in the message. I normally just make it the "From" address. $headers = 'From: ' . $from_email; Then just pass that as extra_headers into the mail command. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128174 Share on other sites More sharing options...
jcbones Posted October 30, 2010 Share Posted October 30, 2010 I normally just put the from email (the person sending) as the from address. There is no reason, not to do this. If you do not want to, then just put their email address in the message. I normally just make it the "From" address. $headers = 'From: ' . $from_email; Then just pass that as extra_headers into the mail command. I have found that a lot of host will not send emails unless the From: header is a valid email address hosted on their server. So you would need to set their email address as the reply-to: header, so that a valid address could be placed in the From: header. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128307 Share on other sites More sharing options...
Ninjakreborn Posted October 30, 2010 Share Posted October 30, 2010 That is a possibility. I have personally never encountered that. I have worked on a variety of webhosts (godaddy, 1and1, bluehost, and many I don't recall). I have also worked on dedicated, and virtual servers. There is a chance that some hosts will not let it send that way. However, I have not encountered this problem personally before. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128385 Share on other sites More sharing options...
KDM Posted October 31, 2010 Author Share Posted October 31, 2010 Thanks for the help guys but I'm pretty new to php so I need to know how to write the code to solve my problem. Can you all please tell me how I should re-write my code? This is what have above the DOCTYPE. <?php if (array_key_exists('send', $_POST)) { // mail processing script // remove escape characters from POST array if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } $from= '$email'; $to = 'my email address'; $subject = 'form mail'; // list expected fields $expected = array('name', 'phone', 'email', 'address', 'city', 'state', 'zip', 'emailerMessage'); // set required fields $required = array('name', 'phone', 'email', 'address', 'city', 'state', 'zip', 'emailerMessage'); // create empty array for any missing fields $missing = array(); // process the $_POST variables foreach ($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } // otherwise, assign to a variable of the same name as $key elseif (in_array($key, $expected)) { ${$key} = $temp; } } // go ahead only if all required fields OK if (empty($missing)) { // build the message $message = "name: $name\n\n"; $message .= "phone: $phone\n\n"; $message .= "email: $email\n\n"; $message .= "address: $address\n\n"; $message .= "city: $city\n\n"; $message .= "state: $state\n\n"; $message .= "zip: $zip\n\n"; $message .= "emailerMessage: $emailerMessage\n\n"; // limit line length to 70 characters $message = wordwrap($message, 70); // send it $mailSent = mail($to, $subject, $message); if ($mailSent) { // $missing is no longer needed if the email is sent, so unset it unset($missing); } } } ?> When I test the form i get this when I hit the reply button eminentwebllc <eminentwebllc@p3nlhg124.shr.prod.phx3.secureserver.net> I want the reply address to by the email address entered into the form. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128778 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2010 Share Posted October 31, 2010 $headers = "Reply-To: $email\r\n"; // send it $mailSent = mail($to, $subject, $message, $headers); Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1128785 Share on other sites More sharing options...
KDM Posted November 2, 2010 Author Share Posted November 2, 2010 $headers = "Reply-To: $email\r\n"; // send it $mailSent = mail($to, $subject, $message, $headers); Thanks that worked! When I hit reply, it has their email address followed by this stuff @p3nlhg124.shr.prod.phx3.secureserver.net>. I tested it and it still sends the reply to their address. Thanks again, this is a good board. This is second time I learned something here. Quote Link to comment https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/#findComment-1129489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.