Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the php $headers in a Specific Contact Form to Change the Email 'From' Address

I'm trying to use $headers = "From: [email protected]\r\n"; in PHP to set the 'from' email address on a contact form to '[email protected]'.

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("[email protected]", "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>';
            }

        }
    }
?>
like image 810
pjk_ok Avatar asked Jun 19 '19 13:06

pjk_ok


1 Answers

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("[email protected]", "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: [email protected]\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.

like image 144
seahorsepip Avatar answered Nov 02 '22 22:11

seahorsepip