2013-04-17 48 views
0

我需要在PHP上生成訂單確認電子郵件。我有一個包含確認電子郵件(因爲它有一些變量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"); 

這將是做正確的方式還是應該撰寫我的確認郵件以不同的方式

感謝

+0

'包括()'不會返回orderConf.php生成的文本。該文本將是OUTPUT。你需要重寫orderConf來生成一個字符串並返回它,或者使用輸出緩衝來捕獲輸出。 –

回答

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變量。

另一個選項將使用output buffering

+0

HEREDOC中如何使用變量?我需要打開/關閉報價嗎? – samyb8

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(); 
+0

輸出緩衝應作爲修補問題的最後手段。如果你不得不依賴輸出緩衝,那麼你的代碼有問題。 – Styphon

+0

@Styphon雖然你的陳述一般是真實的,但這個特殊情況是OB發明的一個例子。這不是修補,而是直接和公平地使用緩衝。 –

+0

是的,看到很多人暗示我做了一些Google搜索,發現我錯了,這對於這種情況來說是完美的。我剛回來修改我上面的陳述。 – Styphon

-1

您不能將文件包含到像這樣的變量中。你將不得不使用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);