php mail() : 已发送附件但未发送正文

php mail() : attachments sent but not body text

我看过类似的问题,但没有直接针对主题...

我有一个包含附件的多部分邮件脚本。附件发送正常,但未发送从表单填充的主体文本。我尝试发送带有注释掉的附件功能,并且表单元素通过了。我的代码是:

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.0\n";
 $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: 7bit\r\n" . $message . "\r\n"; 
$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: base64\n\n" . $data . "\n\n";
    $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: 7bit\r\n" . $questions . "\r\n" . $selected_msg . "\r\n";

如果您喜欢在电子邮件中同时添加文本和附件,请使用以下代码

$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.com\r\n" .
           "X-Mailer: PHP" . phpversion() . "\r\n" .
           "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hash\r\n\r\n";

if(file_exists($file))
{
    $content = file_get_contents($file);
    $content = chunk_split(base64_encode($content));

    //plain text 
    $body = "--$random_hash\r\n";
    $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($bodyMeaasge)); 

    //attachment
    $body .= "--$random_hash\r\n";
    $body .="Content-Type: application/octet-stream; name=".$filename."\r\n";
    $body .="Content-Disposition: attachment; filename=".$filename."\r\n";
    $body .="Content-Transfer-Encoding: base64\r\n";
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
    $body .= $content;

}else{
    //plain text 
    $body = "--$random_hash\r\n";
    $body .= "Content-Type: text/html; charset=utf-8\r\n"; // use different content types here
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $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.');
}