Search code examples
phpformsemail-headers

Change e-mail address PHP contact form


I am currently working on my contact form script and everytime when I fill in my contact form I am receiving the mail perfectly with no problems at all. The e-mail looks like this

From: [email protected] and subject: Contact Form Submission. I would like to change [email protected] into My company name... so that everytime when I'm receiving the mail that I won't see the email address displaying but instead of that I will see my company name. Ofcourse, reply-to function has to stay active. Can someone help me out?

    <?php

// Check if the form has been posted
if (isset($_POST['submit'])) {
    // The email address the email will be sent to
    $to = "[email protected]";
    // The email subject
    $subject = "Contact Form Submission";
    // Set the from and reply-to address for the email
    $headers = "From: [email protected]\r\n"
             . "X-Mailer: PHP/" . phpversion();
    // Build the body of the email
    $mailbody = "The contact form has been filled out.\n\n"
              . "Name: " . $_POST['naam'] . "\n"
              . "Email: " . $_POST['email'] . "\n"
              . "Message:\n" . $_POST['vraag'];
    // Send the email
    mail($to, $subject, $mailbody, $headers);
    // Go to the thank you page
    header("location: thankyou.html");
    exit;

}

Solution

  • The format of the e-mail adress should comply with the RFC 2822 standard:

    Normally, a mailbox is comprised of two parts: (1) an optional display name that indicates the name of the recipient (which could be a person or a system) that could be displayed to the user of a mail application, and (2) an addr-spec address enclosed in angle brackets ("<" and ">"). There is also an alternate simple form of a mailbox where the addr-spec address appears alone, without the recipient's name or the angle brackets.

    So, you should write like "Company name <[email protected]>". Replace this line:

    $headers = "From: [email protected]\r\n"
    

    with:

    $headers = "From: Company name <[email protected]> \r\n"