Jump to content

Recommended Posts

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);
  }
}
}
  ?>

Link to comment
https://forums.phpfreaks.com/topic/217240-help-with-my-php-form-mail/
Share on other sites

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 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.

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.

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.

$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.