0

我需要在 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");

这是正确的方法吗?或者我应该以不同的方式撰写我的确认电子邮件?

谢谢

4

3 回答 3

1

这是HEREDOC可以的少数情况之一

<?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;

那么就

 include ("orderConf.php");

并有你的$message变量。

另一种选择是使用输出缓冲

于 2013-04-17T15:02:39.780 回答
0

这样,您只需输出 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();
于 2013-04-17T15:03:54.177 回答
-1

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

$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);
于 2013-04-17T15:06:04.903 回答