从PHP发送HTML电子邮件与变量 [英] Sending HTML email from php with variables
问题描述
我不是很好的PHP,所以这应该(希望)对你们中的一个很容易。
I'm not very good at php so this should (hopefully) be easy for one of you.
我正在尝试从php发送一封HTML电子邮件其中包括变量。我可以发送一个没有变量的HTML电子邮件,或者没有变量的HTML电子邮件,但我似乎不能同时做这两个。这是我的代码:
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 = "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....!"; ?>
我从来没有遇到过<< EOF之前,不知道它在做什么。
I've never come across <<< EOF before and have no idea what it's doing.
推荐答案
您正在使用HEREDOC语法,并尝试终止字符串,并附加变量不正确和不必要。要使用该变量声明您的字符串,请执行以下操作:
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;
请注意,heredoc语法的PHP手册条目与其余的 string docs,here 。
Note that the PHP manual entry on heredoc syntax is with the rest of the string docs, here.
这篇关于从PHP发送HTML电子邮件与变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!