开发者

PHP Pear Mime Mail with Attachment problem

I'm trying to send an email with a pdf attachment using Pear. 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"

--=_5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding: quoted-printable

Content-Type: text/plain; charset=ISO-8859-1

Hello world

--=_5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding: quoted-printable

Content-Type: text/html; charset=ISO-8859-1

Hello world

--=_5a78298c81e9b7e60dee1049b9239270--

Content-Transfer-Encoding: base64

Content-Type: application/octet-stream;

name=phpk0AQHD

Content-Disposition: attachment;

filename=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.

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.

For me, I discovered the problem was in the HEADERS array sent to the mail item, even before the attachment was added.

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. 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


Tim

addAttachment takes a mime content type too. Have you tried

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜