簡體   English   中英

使用PHP的HTML /文本電子郵件及附件

[英]HTML/Text E-mail with attachment using PHP

我正在嘗試建立一個Web應用程序,該應用程序在完成時向用戶發送帶有附件的電子郵件。 我希望電子郵件使用HTML和文本格式,以確保每個人都可以查看它,而無論其郵件設置如何。 我無法使其正常工作,所以我想知道是否有人可以發現我的代碼存在的問題。

我正在使用的代碼如下。 這是另一個站點的模板,但是我已經做了大量的閱讀工作,所以我對它們的工作原理有一個大概的了解。 不幸的是,對解決問題的理解還不夠!

$firstname = $_POST['firstname'];
$email = $_POST['email'];
//define the receiver of the email 
$to = $email; 
//define the subject of the email 
$subject = "$firstname, thanks for using the One Minute Leveller"; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: The Xenon Group\r\nReply-To: no-reply@xenongroup.co.uk"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('Level3info.pdf'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  

Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

TEXT E-MAIL GOES HERE. ACTUAL CONTENT REMOVED

--PHP-alt-<?php echo $random_hash; ?>  

Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<html><h1>HTML CONTENT GOES HERE. ACTUAL CONTENT REMOVED</h1></html>

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  

Content-Type: application/pdf; name="Level3info.pdf"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 

--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
mail( $to, $subject, $message, $headers );

有人可以幫忙嗎?

首先,不要重新發明輪子-使用諸如Pear Mail http://pear.php.net/package/Mail/redirected之類的庫

其次,您的標頭之間有空格,這是不允許的。

從郵件RFC:

在此標准中,可以自由插入注釋和FWS的幾個位置。 為了適應該語法,為可能出現注釋和/或FWS的位置定義了“ CFWS”的附加標記。 但是,在此標准中出現CFWS的地方,絕不能以這樣的方式插入CFWS:折疊的標頭字段的任何行都完全由WSP字符組成,別無其他。

親切的問候,

約翰

這是我使用的有效方法。 它沒有考慮文本版本與HTML版本...僅考慮HTML版本,但也許它將引導您找到答案:

<?php
    $mime_boundary = md5(uniqid(time()));

    // to
    $to = $_REQUEST["to"];
    if (is_array($to)) $to = implode(", ", $to);

    // from
    $header = "From:". ($_REQUEST["fromName"] == "" ? $_REQUEST["fromAddress"] : "{$_REQUEST["fromName"]} <{$_REQUEST["fromAddress"]}>") ."\r\n";
    $header .= "MIME-Version:1.0\r\n";
    $header .= "Content-Type:multipart/mixed; boundary=\"{$mime_boundary}\"\r\n\r\n";

    // message
    $header .= "--{$mime_boundary}\r\n";
    $header .= "Content-Type:text/html; charset=\"ISO-8859-1\"\r\n";
    $header .= "Content-Transfer-Encoding:7bit\r\n\r\n";
    $header .= "<html><head><style type=\"text/css\">body { font:10pt Arial; }</style></head><body>". str_replace("\r", "", str_replace("\n", "<br />", $_REQUEST["message"])) ."</body></html>\r\n\r\n";

    // attachment
    $attachments = (array)$_REQUEST["attachment"];
    $attachmentNames = (array)$_REQUEST["attachmentName"];
    for ($i = 0; $i < count($attachments); $i++)
    {
        if (empty($attachmentNames[$i]))
            $attachmentNames[$i] = "attachment". ($i+1) .".txt";

        if (!base64_decode($attachments[$i], true))
            $attachments[$i] = base64_encode($attachments[$i]);
        else
            $attachments[$i] = str_replace("\r", "", str_replace("\n", "", str_replace(" ", "+", $attachments[$i])));

        $header .= "--{$mime_boundary}\r\n";
        $header .= "Content-Type:application/octet-stream; name=\"{$attachmentNames[$i]}\"\r\n";
        $header .= "Content-Transfer-Encoding:base64\r\n";
        $header .= "Content-Disposition:attachment; filename=\"{$attachmentNames[$i]}\"\r\n";
        $header .= "{$attachments[$i]}\r\n\r\n";
    }

    if (mail($to, $_REQUEST["subject"], "", $header))
        echo "SUCCESS";
    else
        echo "FAIL";
?>

也許您想看看PHPmailer 它提供了您要使用的所有功能,但是比內置的mail()函數更簡潔,最新的標准方式。 並且那里有一些很好的教程。

暫無
暫無

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

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