使用PHP发送HTML电子邮件:包括HTML文件 [英] Sending HTML email using PHP: including the HTML file
问题描述
我正在尝试使用PHP发送电子邮件。该电子邮件是带有数百行HTML代码的HTML电子邮件(在文件 email.html 中)。我不想在PHP中编写整个HTML代码。我该怎么做?我的以下方法正确吗?还有更好的选择吗?
I am trying to send email using PHP. The email is an HTML email with hundreds of line of HTML code (in file email.html). I don't want to write the whole HTML code in the PHP. How can I do it? Is my below method correct? Any better alternative?
我的代码(PHP):
$email_text = file_get_contents('email.html');
$headers = "From:def@gmail.com";
mail('abc@gmail.com', 'Hello', $email_text, $headers);
我的电子邮件文件(email.html):
My Email File (email.html):
<html>
<body>
<a href="http://google.com">Hello</a>
</body>
</html>
我的问题:我想发送将显示HTML的HTML电子邮件内容。 HTML内容来自名为email.html的文件。
My problem: I want to send an HTML email which will display HTML content. The HTML content is taken from a file called email.html.
注意1:为了简化起见,我简化了上面的代码。原始代码具有可正确显示HTML代码的标题。同样,原始的HTML文件具有数百行和CSS样式。为了简化起见,我只提到了几行。
Note 1: I have simplified my above code just for the clarity. Original code is having headers which will display HTML code correctly. Also the original HTML file is having hundreds of line and CSS styling. I have mentioned only few lines just to simplify.
注2: 使用PHP发送电子邮件的重复次数很多,但我找不到如何添加只需一行PHP代码即可生成一个大型HTML文件。
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.
推荐答案
已更新问题的更新:
如果您不想在PHP代码中包含所有HTML,那么 file_get_contents()
是一个不错的选择。
If you don't want to include all that HTML in your PHP code, then yes file_get_contents()
is a fine alternative.
您需要在标题中包括内容类型和MIME版本。这也是字符集UTF-8。
You need to include the content type, and MIME version in the headers. This is charset UTF-8 also.
$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/zh/function.mail.php
这篇关于使用PHP发送HTML电子邮件:包括HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!