[英]Sending HTML email using PHP: including the HTML file
I am trying to send email using PHP. 我正在尝试使用PHP发送电子邮件。 The email is an HTML email with hundreds of line of HTML code (in file email.html ). 该电子邮件是一封包含数百行HTML代码的HTML电子邮件(在文件email.html中 )。 I don't want to write the whole HTML code in the PHP. 我不想在PHP中编写整个HTML代码。 How can I do it? 我该怎么做? Is my below method correct? 我的下方方法是否正确? Any better alternative? 有更好的选择吗?
My Code (PHP): 我的代码(PHP):
$email_text = file_get_contents('email.html');
$headers = "From:def@gmail.com";
mail('abc@gmail.com', 'Hello', $email_text, $headers);
My Email File (email.html): 我的电子邮件文件(email.html):
<html>
<body>
<a href="http://google.com">Hello</a>
</body>
</html>
My problem: I want to send an HTML email which will display HTML content. 我的问题:我想发送一封显示HTML内容的HTML电子邮件。 The HTML content is taken from a file called email.html. HTML内容取自名为email.html的文件。
Note 1: I have simplified my above code just for the clarity. 注1:为了清楚起见,我简化了上面的代码。 Original code is having headers which will display HTML code correctly. 原始代码具有正确显示HTML代码的标题。 Also the original HTML file is having hundreds of line and CSS styling. 原始的HTML文件也有数百种线条和CSS样式。 I have mentioned only few lines just to simplify. 我只提到了几行简化。
Note 2: There are many duplicates of sending email using PHP but I couldn't find how to include a big HTML file in just one line of PHP code. 注意2: 使用PHP发送电子邮件有很多重复,但我找不到如何在一行PHP代码中包含一个大的HTML文件。
Update for updated question: 更新更新的问题:
If you don't want to include all that HTML in your PHP code, then yes file_get_contents()
is a fine alternative. 如果您不想在PHP代码中包含所有HTML,那么yes file_get_contents()
是一个很好的选择。
You need to include the content type, and MIME version in the headers. 您需要在标头中包含内容类型和MIME版本。 This is charset UTF-8 also. 这也是字符集UTF-8。
$headers = "From: $from <$from_email>\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
http://php.net/manual/en/function.mail.php http://php.net/manual/en/function.mail.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.