簡體   English   中英

使用Pear Mail發送純文本和HTML電子郵件(如何設置“內容類型”並正確使用邊界)

[英]Using Pear Mail to send plain text and html emails (How to set “Content-Type” and use boundary properly)

我正在嘗試開發一個函數來發送可以在我網站的各個部分使用的電子郵件。 我想要電子郵件進行SMTP身份驗證,所以我正在使用Pear。

我的函數的參數被設置為確定電子郵件應該是純文本,html還是作為兩者發送(multipart / alternative)....

    if($set_content_type == 'text'){
        $content_type = 'text/plain';
    } elseif($set_content_type == 'html'){
        $content_type = 'text/html';
    } elseif($set_content_type == 'html_and_text'){
        $boundary = uniqid();
        $content_type = 'multipart/alternative; boundary=' . $boundary;
    }

然后我使用Mail_mime發送電子郵件...

    $mime = new Mail_mime();
    $mime->setHTMLBody($html);
    $mime->setTXTBody($text);
    $body = $mime->get();

    $host = "myhost";
    $port = "25"; // 8025, 587 and 25 can also be used.

    $username = "myusername";
    $password = "mypassword";

    $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'Content-Type:' => $content_type);

    $headers = $mime->headers($headers);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);  

我的問題/問題: 在這段代碼中我可以設置消息的各個部分以遵守multipart / alternative mime類型並正確使用邊界,就像這個答案中顯示的那樣( 電子郵件中的不同內容類型 )?

您必須在$ mime對象上設置ContentType

$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$mime->setContentType($content_type);
$body = $mime->get();

現在我建議你使用另一個郵件,而不是來自梨的郵件。 Pear是一個舊的數據包管理器,一些應用程序停止支持它像PHPUnit和Symfony。

最好使用另一個可以通過composer安裝的。 你可以看看Swiftmailer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM