[英]Trying to send csv attachment AND body text in email
I have been able to write php script (with HTML) to $_REQUEST online form data entry and either (1) write to a CSV and email it as an attachment, or (2) include data in the body of any email. 我已经能够将php脚本(带有HTML)写入$ _REQUEST在线表单数据条目,并且(1)写入CSV并将其作为附件通过电子邮件发送,或者(2)在任何电子邮件的正文中包含数据。
But I cannot do both at the same time. 但是我不能同时做两个。 I want the CSV for quick import into database, and I want email body to also include the details so the shipping dept has a pick list for packing.
我希望将CSV快速导入数据库,并且我希望电子邮件正文中也包含详细信息,以便运输部门可以选择包装清单。 How do I get both features to work simultaneously without interfering with each other?
如何使两个功能同时工作而不互相干扰?
Thank you in advance. 先感谢您。
PHP SCRIPT PHP脚本
$to = "name@domain.com";
$subject = "Order - " . $_Tech_Name;
//Add the headers
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "MIME-Version: 1.0\n" .
"From: {$email_from}\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// email body includes only fields not null
$body = "";
foreach ($_REQUEST as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n\n";
}
}
$email_body = "Here are the details: \n\n $body";
// csv file name format
$today = date("m.d.y");
$csv_name = "order - " . $_Tech_Name . " " . $today . ".csv";
$fp = fopen($csv_name,'a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => $csv_name,
'type' => 'application/vnd.ms-excel'
);
//Add sttachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$message .=
"--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ;
}
**PHP SCRIPT - I have isolated the problem with the following additional script that is part of the script shown above, but I do not as yet understand why it is a problem. ** PHP SCRIPT-通过上面显示的脚本的一部分,下面的其他脚本已隔离了该问题,但我仍不明白为什么会出现此问题。 By commenting out either $message (attachment) or $email_body (body only) I am able to get one or the other to work.
通过注释掉$ message(附件)或$ email_body(仅正文),我可以使其中一个工作。 But if I leave both variables in there, then neither works **
但是,如果我将两个变量都留在那里,那么两个都不会起作用**
mail(
$to,
$subject,
$message, // for attachment only
// $email_body, // for body only
$headers
);
By isolating the problem in my original post I was able to get it to work by writing two (2) "mail" commands as follows: 通过在原始帖子中隔离问题,我可以通过编写两(2)个“ mail”命令来使其正常工作,如下所示:
// attachment
mail($to, $subject, $message, $headers);
// body
mail($to, $subject, $email_body, $headers);
I am happy with this solution, but if someone can find a way to get the same email to include the body content and attach a CSV, that would be ideal. 我对这种解决方案感到满意,但是如果有人可以找到一种方法来获取相同的电子邮件以包含正文内容并附加CSV,那将是理想的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.