【问题标题】:PHP Pear Mime Mail with Attachment problem带有附件问题的 PHP Pear Mime 邮件
【发布时间】:2011-08-25 14:42:19
【问题描述】:

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

内容类型:多部分/替代;

boundary="=_5a78298c81e9b7e60dee1049b9239270"

--=_5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding:quoted-printable

内容类型:文本/纯文本; charset=ISO-8859-1

世界你好

--=_5a78298c81e9b7e60dee1049b9239270

Content-Transfer-Encoding:quoted-printable

内容类型:文本/html; charset=ISO-8859-1

你好世界

--=_5a78298c81e9b7e60dee1049b9239270--

内容传输编码:base64

Content-Type: application/octet-stream;

name=phpk0AQHD

Content-Disposition:附件;

文件名=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/");
}
?>

非常感谢任何关于我做错的提示。

【问题讨论】:

    标签: php email pear mime email-attachments


    【解决方案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

    【讨论】:

      【解决方案2】:

      提姆

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

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

      【讨论】:

      • 刚刚尝试过,但没有改变。似乎整个电子邮件都以内容类型文本的形式发送。
      • Tim 尝试使用 $newsletterPath 将完整路径添加到文件中
      猜你喜欢
      • 2011-01-18
      • 2014-04-07
      • 2012-02-18
      • 2021-11-05
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多