发送带附件的电子邮件时,正文部分丢失

时间:2014-11-25 10:50:14

标签: php email

您好我发送了一封包含pdd / doc附件的电子邮件。当我点击发送电子邮件时,我收到一封包含正确附件的电子邮件但我没有收到邮件正文部分中的邮件内容。当我点击查看show original时,我可以看到原始内容但不是电子邮件。 这是我的代码。任何人都可以帮助我知道如何将它们放在正确的位置?

<?PHP

session_start();

$applicant_name = (isset($_POST["applicant_name"])) ? $_POST["applicant_name"] : '';
$applicant_age = (isset($_POST["age"])) ? $_POST["age"] : '';
$applicant_gender = (isset($_POST["gender"])) ? $_POST["gender"] : '';
$preferred_email = (isset($_POST["preferred_email"])) ? $_POST["preferred_email"] : '';
$MobileNo = (isset($_POST["preferred_mobile_no"])) ? $_POST["preferred_mobile_no"] : '';
$_SESSION['jobTitle'];
$_SESSION['jobId'];

$pdf_doc='';

if(isset($_FILES['document_url']['name'])){
$pdf_doc = rand() . "_" . $_FILES['document_url']['name'];
copy($_FILES['document_url']['tmp_name'], "job_docs/" . $pdf_doc);
}

$name = $applicant_name;
$email = "jyotiepr@gmail.com";
$to = "Karda <".$email.">";
$from = $preferred_email;
$subject = "Application for post : ".$_SESSION['jobTitle']."[".$_SESSION['post_id']."]";

$mainMessage = "<br/>Applicant Name :-&nbsp;" . $applicant_name . "\n\n"
    . "<br/>Email:-&nbsp;" . $preferred_email . "\n\n"
    . "<br/>Phone:-&nbsp;" . $MobileNo . "\n\n"
    . "<br/>Age:-&nbsp;" . $applicant_age . "\n\n"
    . "<br/>Gender:-&nbsp;" . $applicant_gender . "\n\n";

$fileatt = "job_docs/".$pdf_doc;
$fileatttype = "application/pdf";
$fileattname = "job_docs/".$pdf_doc;

$headers = "From: $preferred_email";

// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);

// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" .  " boundary=\"{$mime_boundary}\"";         

$message = $mainMessage."This is a multi-part message in MIME format.\n\n" ."-{$mime_boundary}\n" ."Content-Type: text/plain; charset=\"iso-8859-1\n" ."Content-Transfer-Encoding: 7bit\n\n\n";


$data = chunk_split(base64_encode($data));

$message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatttype};\n" ." name=\"{$fileattname}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileattname}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" ."-{$mime_boundary}-\n";

// Send the email

if(mail($to, $subject, $message, $headers)) {

header("location:we_are_hiring.php");

}

else {

    header("location:index.php");;

}
?>

2 个答案:

答案 0 :(得分:0)

您尚未正确格式化边界标记。正确的语法如下:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"

This is a multi-part message in MIME format.
--$boundary
Content-Type: text/plain

(message body)

--$boundary
Content-Type: image/jpeg; name="attachment.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="attachment.jpg"

(base64 attachment)

--$boundary--

您的代码在$boundary之前的几个地方只有一个连字符。

答案 1 :(得分:0)

您已将标题与正文部分混合在一起,将它们分开以解决此问题,如下所示:

<?PHP

session_start();

$applicant_name = (isset($_POST["applicant_name"])) ? $_POST["applicant_name"] : '';
$applicant_age = (isset($_POST["age"])) ? $_POST["age"] : '';
$applicant_gender = (isset($_POST["gender"])) ? $_POST["gender"] : '';
$preferred_email = (isset($_POST["preferred_email"])) ? $_POST["preferred_email"] : '';
$MobileNo = (isset($_POST["preferred_mobile_no"])) ? $_POST["preferred_mobile_no"] : '';
$_SESSION['jobTitle'];
$_SESSION['jobId'];

$pdf_doc='';

if(isset($_FILES['document_url']['name'])){
$pdf_doc = rand() . "_" . $_FILES['document_url']['name'];
copy($_FILES['document_url']['tmp_name'], "job_docs/" . $pdf_doc);
}

$name = $applicant_name;
$email = "jyotiepr@gmail.com";
$to = "Karda <".$email.">";
$from = $preferred_email;
$subject = "Application for post : ".$_SESSION['jobTitle']."[".$_SESSION['post_id']."]";

$mainMessage = "<br/>Applicant Name :-&nbsp;" . $applicant_name . "\n\n"
 . "<br/>Email:-&nbsp;" . $preferred_email . "\n\n"
. "<br/>Phone:-&nbsp;" . $MobileNo . "\n\n"
. "<br/>Age:-&nbsp;" . $applicant_age . "\n\n"
. "<br/>Gender:-&nbsp;" . $applicant_gender . "\n\n";

$fileatt = "job_docs/".$pdf_doc;
$fileatttype = "application/pdf";
$fileattname = "job_docs/".$pdf_doc;

$headers = "From: $preferred_email";

//文件     $ file = fopen($ fileatt,'rb');     $ data = fread($ file,filesize($ fileatt));     FCLOSE($文件);

// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" .  " boundary=\"      {$mime_boundary}\"";         

$headers = "Content-Type: text/plain; charset=\"iso-8859-1\n" ."Content-Transfer-Encoding: 7bit\n\n\n";


$data = chunk_split(base64_encode($data));

$headers .= "--{$mime_boundary}\n" ."Content-Type: {$fileatttype};\n" ." name=\"   {$fileattname}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileattname}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" ."-{$mime_boundary}-\n";

//发送电子邮件

if(mail($email, $subject, $mainMessage, $headers)) {

header("location:we_are_hiring.php");

}

else {

header("location:index.php");;

} ?&GT;