0

I'm not very good at php so this should (hopefully) be easy for one of you.

I'm trying to send an HTML email from php which includes variables. I'm able to send a none html email with variables, or an HTML email without variables, but I can't seem to do both at the same time. Here's my code:

<?
$to = "[email protected]";
$from = "[email protected]";
$subject = "Hello! This is HTML email";
'
//begin of HTML message
$message = <<<EOF;

<html>
 <p>Opt out of contact by phone<br />
    '. $_POST["Opt_out_phone"] .'
 </p>
</html> EOF;

$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

mail($to, $subject, $message, $headers);

echo "Message has been sent....!"; ?> 

I've never come across <<< EOF before and have no idea what it's doing.

1 Answer 1

1

You're using HEREDOC syntax and attempting to terminate the string and append the variable incorrectly and unnecessarily. To declare your string with the variable, do this:

$message = <<<EOF;

<html>
 <p>Opt out of contact by phone<br />
     {$_POST["Opt_out_phone"]}
 </p>
</html> EOF;

Note that the PHP manual entry on heredoc syntax is with the rest of the string docs, here.

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

4 Comments

Also, EOF is a user defined variable there. You can choose anything as long as you're sure that it won't exist in the string (your html code in your example) there, and it won't be a predefined PHP constant.
Correct except that it's not a variable, it's just an identifier.
You should also note that the terminating identifier needs to be on a new line with no preceding characters - you can't even indent it.
@andrewsi That's something I hadn't read anywhere at the time I discovered the heredoc feature and I had real hard time figuring it out. So thanks for stating that, on behalf of everybody.

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.