我有一个非常好看的HTML模板,我现在需要在我的邮件系统中实现。我目前正在使用它来发送电子邮件:
$to = $dbuser;
$subject = "Welcome";
$from = "support@mysite.com";
$headers = "From: $from";
$server = "";
ini_set ("SMTP", $localhost);
$url="";
$msg="$url";
$body = Example Text!
mail($to, $subject, $body, $headers);
如何将HTML模板(和CSS一起)直接包含在我的php电子邮件表单的$ body变量中?
我做了很多研究,但我找不到任何实质性的东西。
答案 0 :(得分:2)
您缺少电子邮件客户端将邮件解释为HTML所需的标头。将以下内容添加到标题中:
$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
答案 1 :(得分:2)
我过去使用过的一种方法就是像往常一样创建页面(使用html / php等),然后像这样使用file_get_contents($ url):
$body = file_get_contents("http://mydomain.com/emailtemplates/template.php?name=John Doe&subject=Hello");
因为你正在使用http://执行php而不是拉入模板,简单但有效!
我也建议你使用内联css,不要害怕使用表格!
答案 2 :(得分:1)
http://php.net/manual/en/function.mail.php - 示例#5
还要记住,在HTML电子邮件中,强烈建议您尽可能使用内联CSS和旧式HTML格式,以确保与不同电子邮件客户端的最大兼容性。也没有divs
- 只是普通的旧货table
- s
答案 3 :(得分:0)
首先,您需要添加一些标头,以便正确显示HTML。 取自mail()PHP文档,您就是这样做的:
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
在那之后,我假设$ body是文本所在的位置,所以这是将所有HTML放在引号之间(用HTML中的每个引号转换为向后斜杠),这几乎就是它
答案 4 :(得分:0)
我昨天为自己写了这个:
就是这样......我想。 =)
//attachment file paths/names
$files[0] = './world.jpg';
$files[1] = './world2.jpg';
$to = '';
$bcc = "";
$subject = '';
$from = "";
$htmlx = '';
$handle = @fopen("./email.html", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$htmlx .= $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x".$semi_rand."x";
$headers = "From: $from \n";
$headers .= "Reply-To: $from \n";
$headers .= 'Bcc: '. $bcc . "\n";
$headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . ' boundary="'.$mime_boundary.'"'."\n";
$headers .= "X-Author: <Timothy Martens>\n";
$message = '--'.$mime_boundary."\n";
$message .= 'Content-Type: text/html; charset=UTF-8'."\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n\n". $htmlx . "\n\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--".$mime_boundary."\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-ID: <".basename($files[$i]).">\n".
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--".$mime_boundary."--";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.'."\n";
} else {
echo 'There was a problem sending the email.'."\n";
}