PHP Pear Mime 邮件带有附件问题
我正在尝试使用 Pear 发送一封带有 pdf 附件的电子邮件。电子邮件已发送,但似乎以错误的格式发送。电子邮件似乎以文本格式显示。这就是我收到的内容:
内容类型:多部分/替代;
border="=_5a78298c81e9b7e60dee1049b9239270"
--=_5a78298c81e9b7e60dee1049b9239270
内容传输编码:可引用打印
内容类型:文本/纯文本;字符集 = ISO-8859-1
你好世界
--=_5a78298c81e9b7e60dee1049b9239270
内容传输编码:引用打印
内容类型:text/html; charset=ISO-8859-1
Hello world
--=_5a78298c81e9b7e60dee1049b9239270--
内容传输编码:base64
内容类型:application/octet-stream;
name=phpk0AQHD
内容处置:附件;
filename=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"=>"[email protected]", "Subject"=>"My Subject 7");
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send("[email protected]", $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/");
}
?>
任何有关我做错的事情的提示将不胜感激。
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"=>"[email protected]", "Subject"=>"My Subject 7");
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send("[email protected]", $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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我花了几个小时研究完全相同的问题:带有附件的电子邮件可以很好地发送到 Gmail 和其他网络邮件服务,但在 OUtlook 或 Outlook Web Access 中,附件在邮件正文中以乱码形式出现。
对我来说,我发现问题出在发送到邮件项目的 HEADERS 数组中,甚至在添加附件之前也是如此。
我最初是这样的:
这引起了麻烦。一旦我从标题中删除内容类型,附件在我尝试过的所有邮件阅读器中都可以正常工作。我确信内容类型有一个“正确”的设置,但我很高兴它对我有用,所以我继续并保留它。
远非理想的解决方案,但它确实有效。希望这次投资可以帮助别人:)
-D
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:
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.
Far from an ideal solution, but it worked. Hope this time investment helps someone else :)
-D
Tim
addAttachment 也采用 mime 内容类型。你尝试过吗
Tim
addAttachment takes a mime content type too. Have you tried