PHP Pear Mime Mail附件问题

时间:2011-05-14 17:48:11

标签: php email pear mime email-attachments

我正在尝试使用Pear发送包含pdf附件的电子邮件。电子邮件发送,但似乎是以错误的格式发送。电子邮件似乎以文本格式显示。这是我收到的:

内容类型:multipart / alternative;

边界= “= _ 5a78298c81e9b7e60dee1049b9239270”

- = _ 5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding:quoted-printable

Content-Type:text / plain;字符集= ISO-8859-1

Hello world

- = _ 5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding:quoted-printable

Content-Type:text / html;字符集= ISO-8859-1

Hello world

- = _ 5a78298c81e9b7e60dee1049b9239270 -

Content-Transfer-Encoding:base64

Content-Type:application / octet-stream;

名= phpk0AQHD

内容 - 处置:附件;

文件名= phpk0AQHD

此之后是随机字符,我只能假设是文本格式的附加文件的巨量。我试图把它发送到Gmail,Hotmail和使用Outlook中的Microsoft Exchange地址,我也得到了相同的结果。

这是我的代码:

<?php

include('../includes/Mail.php');
include('../includes/Mail/mime.php');

$newsletterName = $_POST['newsletterName'];
$newsletterDate = $_POST['newsletterDate'];

$_SESSION['newsletterName'] = $newsletterName;
$_SESSION['newsletterDate'] = $newsletterDate;

$uploadDir = "newsletters/";
$tempLocation = $_FILES['newsletter']['tmp_name'];
$newsletterPath = $uploadDir . str_replace(" ", "-", $_FILES['newsletter']['name']);
$newsletterFile = $_FILES['newsletter'];

if ($_FILES["newsletter"]["type"] == "application/pdf") 
{
    include '../includes/db-connection-wp.php';
    $query = "INSERT INTO newsletters (newsletter_title, newsletter_path, newsletter_date) VALUES ('" . mysql_real_escape_string($newsletterName) . "', '" . mysql_real_escape_string($newsletterPath) . "', '$newsletterDate')";
    if (!mysql_query($query,$connection))
    {
        die('Error: ' . mysql_error() . ' Please go back and try again.');
    }

    //copy pdf to the right location
    if (copy($tempLocation, "../" . $newsletterPath))
    {
        include '../includes/db-connection.php';

        $queryContact = "SELECT users.email_address FROM form_pages LEFT JOIN users ON form_pages.user_id = users.user_id WHERE form_pages.page_name = 'add-newsletter'";
        $resultContact = mysql_query($queryContact);

        while ($rowContact = mysql_fetch_assoc($resultContact)) 
        {
            $from = $rowContact["email_address"];
        }

        $query = "SELECT * FROM newsletter_recipients";
        $result = mysql_query($query);

        $subject = 'Newsletter';

        $message = new Mail_mime();
        $text = file_get_contents("mail_text.txt");
        $html = file_get_contents("mail_html.html");

        $message->setTXTBody("Hello world");
        $message->setHTMLBody("<b>Hello world</b>");
        $message->addAttachment($tempLocation);
        $body = $message->get();
        $extraheaders = array("From"=>"timottewellis@gmail.com", "Subject"=>"My Subject 7");
        $headers = $message->headers($extraheaders);

        $mail = Mail::factory("mail");
        $mail->send("timottewellis@gmail.com", $headers, $body);

        $_SESSION['pdf'] = true;
        $_SESSION['newsletterName'] = null;
        $_SESSION['newsletterDate'] = null;
        header("location:success/");
    }
    else 
    {
        $_SESSION['upload'] = false;
        header("location:add-newsletter/");
    }
}
else 
{
    $_SESSION['pdf'] = false;
    echo "hello " . $_FILES["newsletter"]["type"];
    header("location:add-newsletter/");
}
?>

任何关于我做错的提示都会非常感激。

2 个答案:

答案 0 :(得分:1)

我花了好几个小时处理完全相同的问题:电子邮件与Gmail和其他网络邮件服务的附件交付得很好,但在OUtlook或Outlook Web Access中,附件在消息正文中显得粗糙。

对我来说,我发现问题出在发送到邮件项目的HEADERS数组中,甚至在添加附件之前。

我最初有这个:

$headers = array ('MIME-Version' => '1.0',
'Content-type' => 'text/html; charset=iso-8859-1',
'From' => 'ask@'.$siteurl,
'To' => $emailTo,
'Subject' => $subject);

这造成了麻烦。一旦我从标题中删除了Content-Type,附件在我尝试的所有邮件阅读器中都能正常工作。我确信内容类型有一个“正确”的设置,但我很高兴它完全适合我,所以我继续前进并将其关闭。

$headers = array ('MIME-Version' => '1.0',
'From' => 'ask@'.$siteurl,
'To' => $emailTo,
'Subject' => $subject);

远非理想的解决方案,但它有效。希望这次投资可以帮助别人:)

-D

答案 1 :(得分:0)

addAttachment也采用mime内容类型。你试过吗

$message->addAttachment($tempLocation,'application/pdf');