我已经能够将PHP脚本(带有HTML)编写到$ _REQUEST在线表单数据输入中 (1)写入CSV并将其作为附件发送,或者 (2)在任何电子邮件正文中包含数据。
但我不能同时做到这两点。我想要CSV快速导入数据库,我希望电子邮件正文还包含详细信息,以便运输部门有一个包装选择列表。如何在不干扰彼此的情况下同时使用这两种功能?
提前谢谢。
PHP SCRIPT
$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 - 我已经通过以下附加脚本隔离了问题,该脚本是上面显示的脚本的一部分,但我还没有理解为什么它是一个问题。通过注释掉$ message(附件)或$ email_body(仅限正文),我可以让其中一个或另一个工作。但如果我把两个变量都留在那里,那么它们都不会起作用**
mail(
$to,
$subject,
$message, // for attachment only
// $email_body, // for body only
$headers
);
答案 0 :(得分:0)
通过隔离原始帖子中的问题,我可以通过编写两(2)个“邮件”命令来使其工作,如下所示:
// attachment
mail($to, $subject, $message, $headers);
// body
mail($to, $subject, $email_body, $headers);
我对此解决方案感到满意,但如果有人能找到一种方法来获取相同的电子邮件以包含正文内容并附加CSV,那将是理想的。