简体   繁体   English

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

[英]Sending HTML email from php with variables

I'm not very good at php so this should (hopefully) be easy for one of you. 我不太擅长php,因此(希望)对你们中的一个人来说很容易。

I'm trying to send an HTML email from php which includes variables. 我正在尝试从php发送包含变量的HTML电子邮件。 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. 我可以发送不带变量的HTML电子邮件,也可以发送不带变量的HTML电子邮件,但似乎无法同时执行这两个操作。 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....!"; ?> 

I've never come across <<< EOF before and have no idea what it's doing. 我以前从未见过<<< EOF,也不知道它在做什么。

You're using HEREDOC syntax and attempting to terminate the string and append the variable incorrectly and unnecessarily. 您正在使用HEREDOC语法,并尝试终止字符串并错误地和不必要地附加变量。 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 . 请注意,有关heredoc语法的PHP手册条目与其余字符串docs一起在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM