php邮件功能无法发送附加的pdf文件和邮件正文

时间:2018-03-19 05:03:42

标签: php function email

我想通过php邮件功能发送邮件。为此,我用Google搜索并找到了发送附有pdf文件的邮件的代码。结果很好,邮件发送但邮件只发送附件的pdf文件,它不能发送邮件正文。

这是代码:

<?php  

$name        = "myname";
$to          = "receive@gmail.com";
$email       = "sender@gmail.com";
$from        = "myname";
$subject     = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt     =  $_SERVER['DOCUMENT_ROOT']."/xxx/ticket.pdf";

$fileatttype = "application/pdf";
$fileattname = "ticket.pdf";
$headers     = "From: $from";

// 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 = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage  . "\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)) {    
    echo "The email was sent.";    
}
else {    
    echo "There was an error sending the mail.";    
}   


?>

我无法确定我在哪里犯了错误,请帮助我并提出一些建议。

注意:不要建议使用PHPMailer。

由于

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码:

$to = "youremail@gmail.com";
$from = "Myname <sender@gmail.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";


//$pdfdoc is PDF generated by FPDF
$pdfdoc     = "/opt/transmail/2018-03-07_32_11564_invoice.pdf";
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$message = "Thanks";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}

希望这对你有用。