Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP4中使用mail()在电子邮件中发送附件和文本/html_Php_Php4 - Fatal编程技术网

在PHP4中使用mail()在电子邮件中发送附件和文本/html

在PHP4中使用mail()在电子邮件中发送附件和文本/html,php,php4,Php,Php4,为了让生活变得困难,我为之工作的客户机使用的是一个运行在PHP4.0上的非常大但很旧的系统,他们不希望添加任何额外的库 我试图通过PHP发送一封包含附件和附带文本/html内容的电子邮件,但我无法在一封电子邮件中同时发送这两个内容 这会发送附件: $headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: att

为了让生活变得困难,我为之工作的客户机使用的是一个运行在PHP4.0上的非常大但很旧的系统,他们不希望添加任何额外的库

我试图通过PHP发送一封包含附件和附带文本/html内容的电子邮件,但我无法在一封电子邮件中同时发送这两个内容

这会发送附件:

$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);
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.
$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;
这会发送文本/html:

$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);
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.
$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;
这会发送一个包含文本/html以及附件内容的附件:

$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);
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.
$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;
我需要知道的是,如何发送电子邮件正文中包含文本/html的电子邮件,以及如何向其添加附件。我肯定我错过了一些非常简单的东西


提前感谢。

尝试使用PHP
mail()
函数发送附件是一种挫折感;简单地说,这不值得付出努力。即使在当前的PHP版本中,我也绝不会建议尝试它。我总是使用像phpMailer这样的库

我知道你说过不允许使用第三方库,但在这种情况下,不使用第三方库是疯了。我们谈论的是一天工作和一年工作的区别;这是一件大事。PHP
mail()
函数非常难以使用,尝试用它编写代码以从头开始发送附件会给您留下脆弱、充满错误的代码,在任何情况下都需要永远才能可靠地工作。这就是像phpMailer这样的库存在的原因

所以我建议,不管他们想让你做什么,你都应该下载PHP4版本,看看它是否适合你。(下载页面甚至仍然有非常旧的版本,因此您应该能够及时找到一个与PHP 4.0兼容的版本。不需要花费任何时间就可以启动并运行一个简单的演示。演示它的工作原理;这可能会软化他们对第三方库的立场


(如果这不能软化他们的立场,那么这个项目在合理的时间内完成的希望就不大了。在这种情况下,你能做的最好的事情就是在项目价格的报价中加上几个额外的零并咬紧牙关)

好的,我终于破解了它!供参考:

$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);
$emailBody.=“废话”;
$emailSubject=“Subject”;
$emailCSV=“\“Col1\”,\“Col2\”\r\n;//等。
$attachment=$emailCSV;
$boundary=md5(time());
$header=“From:Name\r\n”;
$header.=“MIME版本:1.0\r\n”;
$header.=“内容类型:多部分/混合;边界=\”“.$boundary.\”\r\n”;
$output=“-->$boundary”。\r\n”;
$output.=“内容类型:text/csv;名称=\“result.csv\”;\r\n”;
$output.=“内容处置:附件;\r\n\r\n”;
$output.=$attachment.“\r\n\r\n”;
$output.=“-”$boundary.\r\n”;
$output.=“内容类型:text/html;字符集=\“utf-8\”\r\n”;
$output.=“内容传输编码:8位\r\n\r\n”;
$output.=$emailBody.“\r\n\r\n”;
$output.=“-”$boundary.-\r\n\r\n”;
邮件($emailTo、$emailSubject、$output、$header);

Search for sending'multipart'email.。此示例有一部分text/html和text/plain,但您应该能够根据自己的需要对其进行调整。如果您能够这样做,请更新PHP。据我所知,PHP4太旧了,不再维护,因此甚至不会对PHP4分支进行安全修复。上一次PHP4更新是在200年进行的8!坦率地说,如果客户对我说“我在使用PHP4.0,不准备升级”,我会告诉他们我不准备处理他们的项目。@Hikaru Shindo:正如我所说,这是一个非常大的系统(~15000个PHP文件)。大部分是在1998/9年创建的,升级是不可能的,因为它需要太长的时间。编辑:我并不是说我喜欢他们使用PHP4.0,但我对此无能为力。你会遇到的另一个问题是PHP4本身是危险的不安全,就像任何仍然使用PHP4.0的操作系统一样支持。如果他们将此用于面向公众的网站,那么他们是疯了,并且忽视了客户的数据。此系统面临被黑客入侵的直接危险(如果尚未被入侵).对你来说,危险在于,如果你是最后一个做这件事的人,你最终可能会受到指责。仅出于这个原因,我建议在你卷入其中之前,现在就走开。