2011-04-14 98 views
0

我已經設置了一個郵件表單來從我的網頁發送電子郵件,我希望能夠在這些電子郵件中設置圖像。這是我目前擁有的代碼:將圖像放入PHP電子郵件

$to = "[email protected]"; 
$subject = "Emergency details"; 
$body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>"; 
if (mail($to, $subject, $body)) { 
    echo("<p>Message successfully sent!</p>"); 
    } else { 
    echo("<p>Message delivery failed...</p>"); 
    } 

當我發這封郵件,輸出看起來是這樣的:

Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/test.jpg"/> 

,實際上顯示的代碼,而不是圖片。是否有可能讓這個顯示畫面變成圖片?

感謝所有幫助

回答

0

您發送此郵件爲純文本。您應該使用mail()的第四個參數(標題)指定它應該被解釋爲一個html郵件。

示例可在documentation中找到。

的片段:

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 
3

那是因爲你實際上發送文本郵件,而不是一個HTML郵件。你必須設置正確的標題。

看一看郵件()功能手冊:http://php.net/manual/en/function.mail.php

具體做法是:例4中發送HTML郵件

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
0

您發送一個簡單的郵件,並且由於這一點,被解釋爲純文本。你想要做的是發送一個包含html而不是簡單文本的郵件。這要求您在郵件中包含標題,說明郵件的內容。

嘗試這種情況:

$headers .= "--$boundary\r\n 
Content-Type: text/html; charset=ISO_8859-1\r\n 
Content-Transfer_Encoding: 7bit\r\n\r\n"; 

$to = "[email protected]"; 
$subject = "Emergency details"; 
$body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>"; 
if (mail($to, $subject, $body, $headers)) { 
echo("<p>Message successfully sent!</p>"); 
} else { 
echo("<p>Message delivery failed...</p>"); 
} 

(例如,從http://www.daniweb.com/web-development/php/threads/2959折斷)