简体   繁体   English

PHP Pear Mime Mail出现附件问题

[英]PHP Pear Mime Mail with Attachment problem

I'm trying to send an email with a pdf attachment using Pear. 我正在尝试使用Pear发送带有pdf附件的电子邮件。 The email sends, but it seems to be getting sent in the wrong format. 电子邮件已发送,但似乎是以错误的格式发送的。 It appears the email is displaying in text format. 电子邮件似乎以文本格式显示。 This is what I receive: 这是我收到的:

Content-Type: multipart/alternative; 内容类型:多部分/替代;

boundary="=_5a78298c81e9b7e60dee1049b9239270" boundary =“ = __ 5a78298c81e9b7e60dee1049b9239270”

--=_5a78298c81e9b7e60dee1049b9239270 -= _ 5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding: quoted-printable 内容传输编码:带引号的可打印

Content-Type: text/plain; 内容类型:文本/纯文本; charset=ISO-8859-1 字符集= ISO-8859-1

Hello world 你好,世界

--=_5a78298c81e9b7e60dee1049b9239270 -= _ 5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding: quoted-printable 内容传输编码:带引号的可打印

Content-Type: text/html; 内容类型:text / html; charset=ISO-8859-1 字符集= ISO-8859-1

Hello world 你好,世界

--=_5a78298c81e9b7e60dee1049b9239270-- -= _ 5a78298c81e9b7e60dee1049b9239270--

Content-Transfer-Encoding: base64 内容传输编码:base64

Content-Type: application/octet-stream; 内容类型:应用程序/八位字节流;

name=phpk0AQHD 名称= phpk0AQHD

Content-Disposition: attachment; 内容处置:附件;

filename=phpk0AQHD 文件名= phpk0AQHD

This is followed by a massive amount of random characters which I can only assume is the attached file in text format. 其次是大量随机字符,我只能假定是文本格式的附件。 I've tried sending it to Gmail, Hotmail and an Microsoft Exchange address that uses Outlook, and I get the same results. 我尝试将其发送到Gmail,Hotmail和使用Outlook的Microsoft Exchange地址,但得到的结果相同。

This is my code: 这是我的代码:

<?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/");
}
?>

Any tips on what I'm doing wrong would be really appreciated. 我在做错什么的任何提示将不胜感激。

I've spent hours on exactly the same issue: Emails delivered fine with the attachment to Gmail and outher webmail services, but in OUtlook or Outlook Web Access, the attachments came in as gibberish in the body of the message. 我已经花了几个小时处理完全相同的问题:电子邮件与Gmail附件和Web邮件服务的附件都可以很好地传递,但是在OUtlook或Outlook Web Access中,附件在邮件正文中显得很乱。

For me, I discovered the problem was in the HEADERS array sent to the mail item, even before the attachment was added. 对我来说,我发现问题出在发送邮件项目的HEADERS数组中,甚至在添加附件之前。

I originally had this: 我最初有这个:

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

This caused the trouble. 这引起了麻烦。 Once I removed the Content-Type from the headers, the attachment worked fine in all the mail readers I tried. 一旦我从标题中删除了Content-Type,附件在我尝试过的所有邮件阅读器中都可以正常工作。 I'm sure there's a 'right' setting for the content type, but I was thrilled it was working for me at all, so I went ahead and left it off. 我敢肯定内容类型有一个“正确”的设置,但我很高兴它对我完全有用,所以我继续进行,并取消了它。

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

Far from an ideal solution, but it worked. 远非理想的解决方案,但却奏效了。 Hope this time investment helps someone else :) 希望这次投资能帮助别人:)

-D -D

Tim 提姆

addAttachment takes a mime content type too. addAttachment也采用了mime内容类型。 Have you tried 你有没有尝试过

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM