简体   繁体   English

使用mail()在PHP4中的电子邮件中发送附件AND text / html

[英]Using mail() to send an attachment AND text/html in an email in PHP4

To make life difficult, the client I'm working for is using a really large yet old system which runs on PHP4.0 and they're not wanting any additional libraries added. 为了让生活变得困难,我正在努力的客户使用的是一个在PHP4.0上运行的非常大的旧系统,他们不想添加任何额外的库。

I'm trying to send out an email through PHP with both an attachment and accompanying text/html content but I'm unable to send both in one email. 我正在尝试通过PHP发送一封包含附件和附带的text / html内容的电子邮件,但我无法通过一封电子邮件发送这两封邮件。

This sends the attachment: 这会发送附件:

$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;

mail($emailTo, $emailSubject, $output, $headers);

This sends text/html: 这发送text / html:

$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.

This sends an attachment containing the text/html along with the attachment contents: 这会发送包含text / html的附件以及附件内容:

$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n".$emailBody."\r\n";
$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;

What I need to know is how can I send an email with text/html in the email body and also add an attachment to it. 我需要知道的是如何在电子邮件正文中发送带有text / html的电子邮件,并为其添加附件。 I'm sure I'm missing something really simple here! 我确定我在这里错过了一些非常简单的东西!

Thanks in advance. 提前致谢。

Okay, I've finally cracked it! 好的,我终于搞砸了! For reference: 以供参考:

$emailBody .= "<html><body>Blah</body></html>";
$emailSubject = "Subject";
$emailCSV = "\"Col1\", \"Col2\"\r\n"; // etc.
$attachment = $emailCSV;

$boundary = md5(time());

$header = "From: Name <address@address.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"" . $boundary . "\"\r\n";

$output = "--".$boundary."\r\n";
$output .= "Content-Type: text/csv; name=\"result.csv\";\r\n";
$output .= "Content-Disposition: attachment;\r\n\r\n";
$output .= $attachment."\r\n\r\n";
$output .= "--".$boundary."\r\n";
$output .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$output .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$output .= $emailBody."\r\n\r\n";
$output .= "--".$boundary."--\r\n\r\n";

mail($emailTo, $emailSubject, $output, $header);

Trying to send attachments using the PHP mail() function is an exercise in frustration; 尝试使用PHP mail()函数发送附件是一种令人沮丧的练习; put simply, it's just not worth the effort. 简而言之,这不值得努力。 I would never ever suggest even attempting it, even in current PHP versions. 即使在当前的PHP版本中,我也永远不会建议尝试它。 I would always use a library like phpMailer. 我会一直使用像phpMailer这样的库。

I know you said you aren't allowed to use a third party library, but in this case, you would be crazy not to use one. 我知道你说你不允许使用第三方图书馆,但在这种情况下,你会疯狂不使用它。 We're talking about the difference between one day of work and a year; 我们谈论的是一天工作和一年之间的差异; it's that big a deal. 这是一笔大买卖。 The PHP mail() function is incredibly difficult to work with, and trying to write code with it to send attachments from scratch will leave you with brittle, bug-ridden code that takes forever to get working reliably in all cases. PHP mail()函数非常难以使用,并且尝试使用它来编写代码以从头开始发送附件将使您留下脆弱的,错误的代码,在所有情况下都需要永远可靠地工作。 This is why libraries like phpMailer exist. 这就是为什么像phpMailer这样的库存在的原因。

So I suggest that regardless of what they want you to do, you should download phpMailer -- the PHP4 version is still available -- and see if it works for you. 所以我建议不管他们想要你做什么,你应该下载phpMailer - PHP4版本仍然可用 - 并看看它是否适合你。 (The download page even still has the really old versions, so you should be able to go far enough back in time to find one that works with PHP 4.0. It shouldn't take you any time at all to get a simple demo up and running with it. Demonstrate that it works; that may well soften their stance against third party libraries. (下载页面甚至还有真正的旧版本,所以你应该能够及时找到适合PHP 4.0的版本。它不应该花任何时间去做一个简单的演示和与它一起运行。证明它有效;这可能会缓和他们对第三方图书馆的立场。

(If it doesn't soften their stance, then there's not much hope of this project ever being completed in a sane amount of time. In that case, the best you can do is add a few extra zeros to your quote for the project price and grit your teeth) (如果它没有软化他们的立场,那么这个项目在很长一段时间内完成的希望并不大。在这种情况下,你能做的最好的事情就是为项目价格的报价增加一些额外的零点。咬紧牙关)

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

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