2

I'm trying to create an email automation using PHP. Originally I had this:

function send_email($email) {
    $message = "Hello! Here is an email message";
    mail($email, "Subject Line", $message, "from: [email protected]");
}

However I want to make the message emailed much longer and include some HTML markup (simple h1's and p tags).

I'm new to PHP but have been OK finding information on how to output PHP in HTML, but not how to include HTML within a PHP variable. I need to be able to store the markup inside $message.

And another thing I can't get my head around. If I want to write words like don't in my HTML, then I can't use a "'" to open and close. I must use " instead. Is this correct?

ETA: Is there anyway to instead send an entire HTML file via this function? So I could create a nice looking template for the email instead and just send that .html file via email.

1

1 Answer 1

5

Don't forget to set your mail headers to support HTML.

As far as PHP goes, it's pretty much standard:

$message = "<div>Hello! Here is an <span class='myClass'>email</span> message</div>";

Read also about heredoc and nowdoc here: http://www.php.net/manual/en/language.types.string.php. They will be very helpful when writing long multiline messages. For example:

$message = <<<EOT
Hello!
Here is an <span class='myClass'>email</span> message!
EOT;

For quotes you need to use backslash for escaping, like this:

$message = "Hello, \"people\"! Don't you know this is an email message";

or

$message = 'Hello, "people"! Don\'t you know this is an email message';

Note that using double quotes parses the PHP variables inside the string:

$x = 'world';
$message = "Hello $x"; // outputs: Hello world
$message = 'Hello $x'; // outputs: Hello $x

You might also want to consider encoding/decoding HTML entities within a PHP string. This is a good start: http://php.net/htmlentities

About templating, you can always create a function or an include file which would contain the template message, and then just decorate it with custom data for each message. There are many ways to achieve that.

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

2 Comments

Thanks, I have done this now. However the HTML markup I have is very long. How do I call the file in? Can I do $message = email.html and put require email.html at the top of my functions file?
No problem. Hm, the fastest way would probably be to assign the contents of your template to a variable. Try converting the email.html into email.php and use the heredoc method I've shown you to assign the full html into a variable, and then just require it (just like you suggested) in your main file. If not, you can use file_get_contents to load it into a variable using only a single PHP file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.