5

I'm trying to use $headers = "From: webmaster@example.com\r\n"; in PHP to set the 'from' email address on a contact form to 'name@companyname.com'.

It is in reference to this answer PHP mail function 'from' address I'm quite new to php so apologies if the answer is obvious, but it's driving me around the bend.

The code for the form is below, but I can't seem to get it work? Does anyone know how/where I would integrate it in with the code below.

Kind regards,

<?php 

    if($_POST['submit']) {

        if(!$_POST['name']) {
            $error="<br>- Please enter your name";
        }
        if(!$_POST['email']) {
            $error.="<br>- Please enter your email";
        }
        if(!$_POST['telephone']) {
            $error.="<br>- Please enter your telephone number";
        }
        if(!$_POST['message']) {
            $error.="<br>- Please enter your message";
        }
        if(!$_POST['checkbox']) {
            $error.="<br>- Please confirm you agree to the Privacy Policy";
        }

        if ($error) {
            $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
        } else {
        mail("name@company.com", "Contact Message", "Name: ".htmlspecialchars($_POST['name'])."
            Email: ".htmlspecialchars($_POST['email'])."
            Telephone: ".htmlspecialchars($_POST['telephone'])."
            Company: ".htmlspecialchars($_POST['company'])."
            Budget: ".htmlspecialchars($_POST['budget'])."
            Message: ".htmlspecialchars($_POST['message']));

            {
                $_POST= array();
                $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
            }

        }
    }
?>
pjk_ok
  • 618
  • 7
  • 35
  • 90
  • 1
    Best advice I can offer is using PHPMailer rather than the native `mail()` function. https://github.com/PHPMailer/PHPMailer – benJ Jun 19 '19 at 17:53

1 Answers1

5

Right now you've passed 3 parameters to the mail() function, the 4th parameter is for headers.

So just pass that string as fourth parameter after the message. More precisely:

<?php 

    if($_POST['submit']) {

        if(!$_POST['name']) {
            $error="<br>- Please enter your name";
        }
        if(!$_POST['email']) {
            $error.="<br>- Please enter your email";
        }
        if(!$_POST['telephone']) {
            $error.="<br>- Please enter your telephone number";
        }
        if(!$_POST['message']) {
            $error.="<br>- Please enter your message";
        }
        if(!$_POST['checkbox']) {
            $error.="<br>- Please confirm you agree to the Privacy Policy";
        }

        if ($error) {
            $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
        } else {
        mail("name@company.com", "Contact Message", "Name: ".htmlspecialchars($_POST['name'])."
            Email: ".htmlspecialchars($_POST['email'])."
            Telephone: ".htmlspecialchars($_POST['telephone'])."
            Company: ".htmlspecialchars($_POST['company'])."
            Budget: ".htmlspecialchars($_POST['budget'])."
            Message: ".htmlspecialchars($_POST['message']),
            "From: webmaster@example.com\r\n"
        );

            {
                $_POST= array();
                $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
            }

        }
    }
?>

More info: https://www.php.net/manual/en/function.mail.php

Also for transactional email, check API's like mailgun, sendgrid etc.

They additionally offer PHP libraries for sending e-mail from their servers which is often more reliable than the average mail server (mail ending up in spam box etc). These services also have great dashboards so you can see how many mails were successfully sent and received.

seahorsepip
  • 4,519
  • 1
  • 19
  • 30