Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Contact Form – show email in from and reply to

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #164954
    Cathyk
    Participant

    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][email protected]([-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!

    #165001
    __
    Participant

    The 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 );
    
    #165004
    Cathyk
    Participant

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

    #165007
    __
    Participant

    Received 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 );
    
    #165008
    Cathyk
    Participant

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

    #165015
    __
    Participant

    The “Your Email” field is named email. The code you showed above already validates this field, so, you can simply use it.

    #165018
    Cathyk
    Participant

    So my code should be:

    $headers = ‘From: “$email”‘ . “\r\n”;

    #165019
    Alen
    Participant

    $headers = "From: $email\r\n";

    #165021
    Cathyk
    Participant

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

    #165042
    __
    Participant

    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.

    In that case, you need to be setting the Reply-to header, not the From header (though most mail clients will reply to the “From” email by default).

    #165044
    Cathyk
    Participant

    Thanks, Traq.

    Can you provide that line of code so I can copy and paste into mine?

    #165078
    Cathyk
    Participant

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

Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘Back End’ is closed to new topics and replies.