包括订单确认电子邮件到PHP邮件功能


Include order confirmation email into PHP mail function

我需要在PHP上生成一个订单确认电子邮件。我有一个php文件,其中包含确认电子邮件(因为它有一些变量,应该在主php处理订单加载时打印。它看起来像这样:

**orderConf.php**
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear <?php echo $firstName." ".$lastName; ?> .....
.....
</body></html>

然后在处理订单的主php中,我有一个邮件函数,我在其中放置了这个变量: orderProcessing.php

$message = include ("orderConf.php");

这是正确的方法吗?或者我应该用另一种方式写确认邮件?

谢谢

这是herdoc可以正常工作的少数情况之一

<?php
$message - <<<HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear $firstName $lastName
.....
</body></html>
HERE;

then just

 include ("orderConf.php");

和你的$message变量。

这样您将只输出orderConf.php的内容。该消息应该由这个文件返回。

<?php
return <<<MSG <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear <?php echo $firstName." ".$lastName; ?> .....
.....
</body></html>
MSG;

或者你可以使用ob_函数

<?php
ob_start();
include('orderConif.php');
$message = ob_get_contents();
ob_end_clean();

你不能把一个文件包含在这样的变量中。您必须使用file_get_contents()。然而,在我看来,这并不是最好的方法。相反,您应该做的是将消息加载到一个变量中,然后使用相同的变量发送电子邮件。在下面的例子:

$body = '<div>Dear' . $firstName . ' ' . $lastName . '... rest of your message</div>';

确保在$body中使用内联样式。表格可能也是一个好主意,因为它们在电子邮件中工作得更好。

那么你所要做的就是使用:

$to = recepients address;
$subject = subject;
$headers = "From: " . strip_tags($_POST['req-email']) . "'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
mail($to, $subject, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><body>' . $body . '</body></html>', $headers);