-1

I am triggering an email to be sent with data from a filled out form once a submit button has been clicked. The email successfully sends with the correct data, however, I am wanting to be able to apply some basic styling to the email such as some <h3> or <strong> tags. Am I able to do this within my php $message variable, and if so, what would that look like?

<?php 
    $to = "[email protected]"; // this is your Email address
    $from = "[email protected]"; // this is the sender's Email address

    $company_name = $_POST['company_name'];
    $rep_name = $_POST['rep_name'];
    $prod_type = $_POST['prod_type'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    $subject = "New Form Submission";

    $message =  "New Form Submission" . "\n" . "\n" .
                "Company Name: " . $company_name . "\n" .
                "Representative Name: " . $rep_name . "\n" .
                "Product Type: " . $prod_type . "\n" .
                "Address: " . $address . "\n" .
                "City: " . $city . "\n" .
                "State: " . $state . "\n" .
                "Zip: " . $zip . "\n" .
                "Phone: " . $phone . "\n" .
                "Email: " . $email . "\n";

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
?>

Edit: I have tried using ob_start() and ob_get_clean() that I found at this link Defining html code inside PHP variables but had no success

5
  • @ADyson I've tried just adding html tags within the quotes and it just prints out the tag itself. I also tried ob_start() and ob_get_clean() that I found here stackoverflow.com/questions/7305869/… but didnt have any success with it Commented Mar 23, 2020 at 15:40
  • 3
    That's because there's more to it than that. HTML comes in distinct flavours - plain text and email are the main ones. If you simply put HTML tags into a plain-text email, it won't be treated as HTML by the receiving mail client. Follow one of the guides in the link I gave you , e.g. this one perhaps css-tricks.com/sending-nice-html-email-with-php . P.S. that link stackoverflow.com/questions/7305869/… is not really relevant to your question. Certainly ob_start and ob_get_clean are not needed. Commented Mar 23, 2020 at 15:42
  • @ADyson Perfect, this looks like what I need, thanks for the direction! Commented Mar 23, 2020 at 15:44
  • 1
    I would recommend that you use one of the tried and tested mail libraries, like PHPMailer, SwiftMailer etc, instead of using the low level mail-function. It will make it much easier to send multi-part emails (containing both HTML and text versions) Commented Mar 23, 2020 at 15:44
  • There's no HTML in this code. \n in HTML would not render a new line. Commented Mar 23, 2020 at 15:52

1 Answer 1

3

Set Content-Type: text/html header to enable HTML emails;

$headers = "From:" . $from;
$headers .= "Content-Type: text/html";

Add some HTML elements to the email;

$message = <<<EOL
    <h1>Hi!</h1>
    <h2>HTML Emails are awesome!</h2>
EOL;
mail($to, $subject, $message, $headers);


Small working example;

<?php

$to = '----';
$subject = 'SO test Mail';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();

$message = <<<EOL
<html>
    <body>
        <h1>Hi!</h1>
        <h2>HTML Emails are awesome!</h2>
    </body>
</html>
EOL;

if ($res = (mail($to, $subject, $message, $headers))) {
    echo 'OK';
} else {
    echo 'Error';
    var_dump($res);
}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.