我看到过类似的问题,但没有直接涉及主题…
我有一个多部分邮件脚本与附件。附件可以正常发送,但是主体文本(由表单填充)没有发送。我试着将附件功能注释掉后发送,表单元素通过了。我的代码是:
if (empty($_POST['RadioGroup1'])){
echo "PLease select a version of message";
} else {$selected_msg = $_POST['RadioGroup1'];}
if (isset($_FILES) && (bool) $_FILES){
$files = array();
// Check for attachments
$questions = $_POST['questions'];
//loop through all the files
foreach($_FILES as $name=>$file){
// define the variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
//check if file type allowed
$path_parts = pathinfo($file_name);
//move this file to server
$server_file = "reports/$path_parts[basename]";
move_uploaded_file($temp_name, $server_file);
//add file to array of file
array_push($files,$server_file);
}
// define mail var
$to = $email;
$from = "[server]";
$subject = "Closed Case: $case_id $casename";
$headers = "From: $from";
//define boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Header know about boundary
$headers .= "nMIME-Version: 1.0n";
$headers .= "Content-Type: multipart/mixed;n";
$headers .= " boundary="{$mime_boundary}"";
// Define plain text mail
$message .= "--{$mime_boundary}n";
$message .= "Content-Type: text/html; charset="iso-8859-1"n";
$message .= "Content-Transfer-Encoding: 7bitrn" . $message . "rn";
$message .= "--{$mime_boundary}n";
// preparing attachments
foreach($files as $file){
$aFile = fopen($file, "rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {"application/octet-stream"}n";
$message .= " name="$file"n";
$message .= "Content-Disposition: attachment;n";
$message .= " filename="$file"n";
$message .= "Content-Transfer-Encoding: base64nn" . $data . "nn";
$message .= "--{$mime_boundary}n";
} // END foreach attachment
$message .= $questions;
$message .= $selected_msg;
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p class="success">mail sent to $to!</p>";
脚本运行无错误,并发送文件附件或正文文本。任何见解将不胜感激。
不从内部调用表单变量
$message .= $questions;
.
$message .= $selected_msg;
将变量硬编码到$message.=
像——$message .= "Content-Transfer-Encoding: 7bitrn" . $questions . "rn" . $selected_msg . "rn";
如果您喜欢在电子邮件中同时添加文本和附件,则使用下面的代码
$bodyMeaasge = 'Your Body Meaasge';
$filename = yourfile.pdf;
$path = '../';
$mpdf->Output($path.$filename,'F');
$file = $path . "/" . $filename;
$to = "senderemail@gmail.com";
$subject = "My Subject";
$random_hash = md5(date('r', time()));
$headers = "From:your@domain.comrn" .
"X-Mailer: PHP" . phpversion() . "rn" .
"MIME-Version: 1.0rn";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hashrnrn";
if(file_exists($file))
{
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
//plain text
$body = "--$random_hashrn";
$body .= "Content-Type: text/html; charset=ISO-8859-1rn";
$body .= "Content-Transfer-Encoding: base64rnrn";
$body .= chunk_split(base64_encode($bodyMeaasge));
//attachment
$body .= "--$random_hashrn";
$body .="Content-Type: application/octet-stream; name=".$filename."rn";
$body .="Content-Disposition: attachment; filename=".$filename."rn";
$body .="Content-Transfer-Encoding: base64rn";
$body .="X-Attachment-Id: ".rand(1000,99999)."rnrn";
$body .= $content;
}else{
//plain text
$body = "--$random_hashrn";
$body .= "Content-Type: text/html; charset=utf-8rn"; // use different content types here
$body .= "Content-Transfer-Encoding: base64rnrn";
$body .= chunk_split(base64_encode($bodyMeaasge));
}
if (mail($to,$subject,$body,$headers)) {
header("Location:".$url.'&success=successfully mail send.');
} else {
header("Location:".$url.'&error=There was a problem sending the email.');
}