带有PHP附件的HTML /文本电子邮件

时间:2012-01-31 15:21:53

标签: php html email

我正在尝试设置一个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 );

有人可以帮忙吗?!

3 个答案:

答案 0 :(得分:2)

首先,不要重新发明轮子 - 使用像Pear Mail这样的库http://pear.php.net/package/Mail/redirected

其次,你的标题之间有空格,这是不允许的。

来自Mail RFC:

本标准中有几个地方可以提供评论和FWS    自由插入。为了适应该语法,需要一个额外的令牌    “CFWS”是为可能出现注释和/或FWS的地方定义的。    但是,如果CFWS出现在此标准中,则不得插入    以这种方式组成折叠标题字段的任何行    完全是WSP角色,没有别的。

亲切的问候,

约翰

答案 1 :(得分:1)

这是我使用的有效方法。它没有考虑文本版本与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";
?>

答案 2 :(得分:1)

也许你想看看PHPmailer。它提供了您想要使用的所有功能,但是比内置的mail()函数更清晰,更新标准。那里有一些很好的教程。