2009-09-01 49 views
22

我正在使用PEAR郵件系統發送經過身份驗證的郵件。我需要發送具有鏈接的HTML郵件。在開始使用PEAR郵件之前它工作正常。現在我無法發送HTML郵件。如何使用PEAR郵件發送HTML郵件

郵件正文如下:出現

$body = <<<EOD 

Hiya $username 

You might be interested in the current 'haves' and 'wants' on example.com 

Latest Haves 
<a href="http://www.exmaple.com/product/have/64/Titan+Fast+Track+SunGlass">Titan Fast Track SunGlass</a> 

EOD; 

標籤,因爲它是在mail.Any知道如何解決這個問題?請幫助..

+0

看看Mail_mime包。它允許你包括一個HTML和純文本版本的電子郵件: http://pear.php.net/manual/en/package.mail.mail-mime.example.php –

回答

28

如果按照這個例子沒有原因,它不應該工作:

<? 
     include('Mail.php'); 
     include('Mail/mime.php'); 

     // Constructing the email 
     $sender = "Leigh <[email protected]_spam.net>";        // Your name and email address 
     $recipient = "Leigh <[email protected]_spam.net>";       // The Recipients name and email address 
     $subject = "Test Email";           // Subject for the email 
     $text = 'This is a text message.';         // Text version of the email 
     $html = '<html><body><p>This is a html message</p></body></html>'; // HTML version of the email 
     $crlf = "\n"; 
     $headers = array(
         'From'   => $sender, 
         'Return-Path' => $sender, 
         'Subject'  => $subject 
         ); 

     // Creating the Mime message 
     $mime = new Mail_mime($crlf); 

     // Setting the body of the email 
     $mime->setTXTBody($text); 
     $mime->setHTMLBody($html); 

     $body = $mime->get(); 
     $headers = $mime->headers($headers); 

     // Sending the email 
     $mail =& Mail::factory('mail'); 
     $mail->send($recipient, $headers, $body); 
?> 

注意 爲了上面的例子中,以工作需要一個梨郵件Mime包裏還有梨子郵件一個。你可以在這裏得到這個包https://pear.php.net/package/Mail_Mime/download

+0

這段代碼似乎現在發送電子郵件作爲附件。 –

+1

SMTP服務器配置怎麼樣?,你有鏈接嗎? –

+1

嗨,我讀了https://pear.php.net/manual/en/package.mail.mail.send.php,'$ headers'中有'To'。我想知道'send()'方法和'$ headers'變量中的收件人電子郵件'$ recipient'有什麼區別?是否有必要在'$ headers'中加入'To'? – stenlytw

3

請注意,由karim79發佈的示例有一個標題參數,可能會導致您很大的悲傷: 「返回路徑」 - 當我包含此參數,如示例阻止我添加名稱,只有發件人電子郵件地址有效

具體(當我添加了一個調試參數去看看發生了什麼事)有大約增加從名額外尖括號所以它試圖發送這SMTP服務器:

從:從名稱<名< @ domain.com > >或
From:<「from name」<[email protected]> >當我嘗試使用引號時。
這導致smtp連接退出,出現無效地址錯誤。

另外,當使用mime_mail類時,您需要在標題中指定「To:」參數,或者當您收到它時,它似乎會發送到未公開的地址。因此用To參數替換Return-Path參數,它將起作用。

12

你的標題是什麼樣的?這裏是我的:

$headers = array(
    'To' => $recipients, 
    'From' => $adminEmail, 
    'Subject' => $subject, 
    'MIME-Version' => 1, 
    'Content-type' => 'text/html;charset=iso-8859-1' 
); 
+2

這段代碼非常好,很簡短。謝謝@story – aya

+2

這精彩的作品,沒有人不得不去尋找額外的圖書館 –

+1

無需額外的庫工程perfekt! – Mazz