使用变量从php发送HTML电子邮件

时间:2012-07-12 17:13:15

标签: php html email

我不擅长php,所以这对你们其中一人来说应该很容易。

我正在尝试从php发送包含变量的HTML电子邮件。我能够发送带有变量的无html电子邮件,或者没有变量的HTML电子邮件,但我似乎无法同时执行这两项操作。这是我的代码:

<?
$to = "me@something.co.uk";
$from = "m2@something.com";
$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....!"; ?> 

我从未遇到过&lt;&lt;&lt; EOF之前并不知道它在做什么。

1 个答案:

答案 0 :(得分:1)

您正在使用HEREDOC语法并尝试终止字符串并错误地并且不必要地附加变量。要使用变量声明字符串,请执行以下操作:

$message = <<<EOF;

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

请注意,关于heredoc语法的PHP手册条目与string docs, here的其余部分一致。