Home

PHP Mail() With HTML

Blog Date 1 April 2021

TLDR

The PHP

<?php 

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $cont = "YES";

    if(empty($_POST["name"]))
    {
        $NameErr="A name is required";
        $cont = "NO";
    }
    if(!preg_match("/^[a-zA-Z-' ]*$/",$_POST["name"]))
    {
        $NameErr="Only letters and blank spaces allowed";
        $cont = "NO";
    }


    if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
    {
        $EmailErr="Incorrect email address, please check your email address";
        $cont = "NO";
    }
    if(empty($_POST["email"]))
    {
        $EmailErr="An email address is required";
        $cont = "NO";
    }


    if($cont == "YES")
    {
    
    $Message = "Message from the Contact form on MyWebSite.com";
    $Message .= "<br /><br />";
    $Message .= "Name : " . $_POST["name"];
    $Message .= "<br /><br />";
    $Message .= "Email Address : " . $_POST["email"];
    $Message .= "<br /><br />";
    $Message .= "Phone Number : " . $_POST["phone"];
    $Message .= "<br /><br />";
    $Message .= "Company Name : " . $_POST["company"];
    $Message .= "<br /><br />";
    $Message .= "Subject : " . $_POST["subject"];
    $Message .= "<br /><br />";
    $Message .= "Message : <hr />" . nl2br($_POST["message"]);

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    mail("sales@MyWebSite.com", "Message from MyWebSite Contact Form", $Message, $headers);

    $EmailSent = "Your message has been sent successfully";
    }
}

?>

The HTML

    <h2>
    Contact Form
    </h2>

    <form action="thispage.php" method="post">
    Your Name (required)
    <br />
    <input type="text" name="name" class="EmailTextBox" value="<?php echo $_POST["name"]; ?>">
    <br />
       <?php echo $NameErr;?>

    <br />
        <br />

    Your Email Address (required)
    <br />
    <input type="text" name="email" class="EmailTextBox" value="<?php echo $_POST["email"]; ?>">
    <br />
    <?php echo $EmailErr;?>

    <br />
        <br />

    Your Phone Number
    <br />
    <input type="text" name="phone" class="EmailTextBox" value="<?php echo $_POST["phone"]; ?>">

    <br />
        <br />

    Your Company Name
    <br />
    <input type="text" name="company" class="EmailTextBox" value="<?php echo $_POST["company"]; ?>">

    <br />
        <br />

    Subject
    <br />
    <input type="text" name="subject" class="EmailTextBox" value="<?php echo $_POST["subject"]; ?>">

    <br />
        <br />

    Your Message
    <br />
    <textarea name="message" rows="6" class="EmailTextBox">
<?php echo $_POST["message"]; ?>
    </textarea>

    <br />
        <br />

    <input type="submit">
    <?php echo $EmailSent; ?>

The explanation

if($_SERVER["REQUEST_METHOD"] == "POST")
{

}

This says "is the page posted back? If it is then run the code between the brackets.

    if(empty($_POST["name"]))
    {
        $NameErr="A name is required";
        $cont = "NO";
    }
    if(!preg_match("/^[a-zA-Z-' ]*$/",$_POST["name"]))
    {
        $NameErr="Only letters and blank spaces allowed";
        $cont = "NO";
    }

This says "we've checked the page is posted back before, now we are looking at the information posted back. In this first case is the textbox called name empty? If it is then run the code between the brackets. In the second case we're doing a regex match to see if we only have letters and white spaces. In the event of an error we set the variable $NameErr to tell the user what is wrong. $NameErr is displayed in the HTML later on with <?php echo $NameErr;?>

    if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
    {
        $EmailErr="Incorrect email address, please check your email address";
        $cont = "NO";
    }
    if(empty($_POST["email"]))
    {
        $EmailErr="An email address is required";
        $cont = "NO";
    }

The first if checks the posted back "email" to ensure it is actually an email address. I have to presume PHP has a very clever inbuilt "email is of the correct format" function built into it. I wish other code had this... The second if again checks for empty text in the textbox named "email"

    $Message = "Message from the Contact form on MyWebSite.com";
    $Message .= "<br /><br />";
    $Message .= "Name : " . $_POST["name"];
    $Message .= "<br /><br />";
    $Message .= "Email Address : " . $_POST["email"];
    $Message .= "<br /><br />";
    $Message .= "Phone Number : " . $_POST["phone"];
    $Message .= "<br /><br />";
    $Message .= "Company Name : " . $_POST["company"];
    $Message .= "<br /><br />";
    $Message .= "Subject : " . $_POST["subject"];
    $Message .= "<br /><br />";
    $Message .= "Message : <hr />" . nl2br($_POST["message"]);

Here we are simply building the message part of the email, the bit that's in the body. We're using HTML. The variable $Message (PHP is case sensitive...) is concatenated with the full stop "." Usefully again PHP has nl2br which simply replaces line breaks with "<br />" for HTML. Clever, I wish other code had this...

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

We're merely creating a string called headers, but this will be used to tell the email client that this is a HTML email. Note the "\r\n" parts, these are carriage returns and/or new lines in plain old text.

    mail("sales@MyWebSite.com", "Message from MyWebSite Contact Form", $Message, $headers);

mail is the function that sends the email. The first variable in the brackets is the email address the mail is going to, the second is the Subject, the third is the actual body of the message and finally the headers.

When you're used to other languages like I am, I'm used to having to set up the email credentials. Things like email login username and password, what ports to use, do we need to POP before SMTP and all that. I *HAVE* to presume PHP's mail function on some servers already has an SMTP relay set up for that server. In this case we're on Godaddy and the email is sent from some complex Godaddy account that will end up in your spam folder.

Reader's Comments

DSG said :-
Cheers, useful thanks
11/6//2021 2:48:44 PM UTC

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog