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.
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. How do I get both features to work simultaneously without interfering with each other?
Thank you in advance.
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 - 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. By commenting out either $message (attachment) or $email_body (body only) I am able to get one or the other to work. 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:
// 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.