- This topic is empty.
-
AuthorPosts
-
March 6, 2014 at 10:08 pm #164954CathykParticipant
I am a real novice at PHP, need some help with a form I’ve created. The form is functioning correctly. However, I’d like to have the email from the person filling in the form show in the from field when form is submitted. The form can be found at http:www.visualsgd.net/Testing/contact.php. And here’s my php:
'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);// Set form status
$form_complete = FALSE;// configure validation array
$validation = array();// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);// Update form switch
$form_complete = TRUE;
}
}function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}?>
Thanks for your time and help!
March 7, 2014 at 10:44 am #165001__ParticipantThe fourth param of mail is
$headers
, which is where you can (should!) be adding your email headers. For example:// headers are in the form // {Header-Name}: {header value}{CRLF} $headers = "From: [email protected]\r\n"; // if the From header is not an email on _your_ domain // (and the user's is likely not), // then you should add a Sender header. // otherwise, the email will probably be discarded as spam during delivery. $headers .= "Sender: [email protected]\r\n"; // then, add the $headers when you send the email: $success = mail( $email_to,$email_subject,$email_content,$headers );
March 7, 2014 at 11:13 am #165004CathykParticipantThanks, Traq!
Here’s my PHP now:
// simple email content foreach($_POST as $key => $value) { if($key != 'submit') $email_content .= $key . ': ' . $value . "\n"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-Type: text/html; charset=utf-8" . "\r\n"; $headers = "From: [email protected]\r\n"; $headers .= "Sender: [email protected]\r\n"; } // if validation passed ok then send the email mail($email_to, $email_subject, $email_content, $headers);
Filled in form and sent. Received email in Junk Mail with From Field as [email protected]. What I need is From Field to be filled in with the email sender fills in on the form.
March 7, 2014 at 11:36 am #165007__ParticipantReceived email in Junk Mail with From Field as [email protected].
I was using
example.com
as an example. You need to provide the actual email addresses to be used.As I explained above, the email probably ended up in your junk folder because the Sender domain (“my-web-site.example.com”) didn’t match your website domain.
What I need is From Field to be filled in with the email sender fills in on the form.
I have no idea what your form looks like, so I can’t answer that. You would need to add the name of that form field to your
$required_fields
array, and then make sure that the value submitted is a valid email address.edit
I would also recommend not using the
validate_email_address
function in your code; it is not reliable. The best option for validating an email address is PHP’s built-in filter_var function:$is_valid_email = filter_var( $email, FILTER_VALIDATE_EMAIL );
March 7, 2014 at 11:49 am #165008CathykParticipantThanks, again for your time and patience. I don’t know the email address, it will be filled each time the person completing the form sends. Here’s the URL for my form I am testing, http://www.visualsgd.net/Testing/contact.php. What I need is for the “Your Email” field on the form to populate the “From” field on the email I receive once the sender completes the form and submitsl.
March 7, 2014 at 12:59 pm #165015__ParticipantThe “Your Email” field is named
email
. The code you showed above already validates this field, so, you can simply use it.March 7, 2014 at 1:13 pm #165018CathykParticipantSo my code should be:
$headers = ‘From: “$email”‘ . “\r\n”;
March 7, 2014 at 1:24 pm #165019AlenParticipant$headers = "From: $email\r\n";
March 7, 2014 at 1:35 pm #165021CathykParticipantThanks, Alen.
My email now shows from: [email protected].
This is close but I want it to actually show the senders email address that they entered on the form so that when I hit reply their email shows in the to field.
March 7, 2014 at 3:52 pm #165042__ParticipantI want it to actually show the senders email address that they entered on the form so that when I hit reply their email shows in the to field.
In that case, you need to be setting the
Reply-to
header, not theFrom
header (though most mail clients will reply to the “From” email by default).March 7, 2014 at 4:01 pm #165044CathykParticipantThanks, Traq.
Can you provide that line of code so I can copy and paste into mine?
March 7, 2014 at 11:55 pm #165078CathykParticipantDiscovered this:
$mailheader = “From: “.$_POST[“email”].”\r\n”;
fills in the email address on the email generated from the form!
Thanks for everyones time & helpful information.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.