[英]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中,我具有將此變量放置在其中的mail函數: orderProcessing.php
$message = include ("orderConf.php");
這是正確的方法嗎? 還是應該以其他方式撰寫確認電子郵件?
謝謝
這樣,您將只輸出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()。 但是,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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.