如何使用PHP发送电子邮件正文和PDF附件?

时间:2017-07-11 11:08:49

标签: php email pdf

下面的脚本可以将pdf文件作为附件发送到Gmail和Outlook,但不会显示在Yahoo邮件中。我的代码出了什么问题?

其次,当发送邮件时,除了pdf附件本身之外,它不会携带邮件正文。关于如何添加带有pdf附件的邮件正文的任何​​建议将不胜感激。

<?php
include("inc/conn2.php");

$customer_ID=$_GET['id'];
$invoice_ref_no=$_GET['ref'];

$query_sql= "SELECT * FROM add_biller_setup";
$result = mysqli_query($conn,$query_sql);
$row = $result->fetch_assoc();
$biller_email_address= $row['email_address'];
$biller_email = str_replace(' ','',$biller_email_address); 


$customer_emails=$_POST['customer_emails'];
//$cust_emails=explode(',', $customer_emails);
foreach($customer_emails as $key => $email_value){

$to =$email_value;
$subject = "Customer Invoice Details";

$pdfdoc = $mpdf->Output('', 'S');

$fileatttype = "application/pdf";
$fileattname = "Invoice.pdf";
$mainMessage = "Please find the attachment included the mail.";


$headers .= 'From: ABS Motor LTD<billing@example.com>' . "\r\n";
$headers .= 'Cc: '.$biller_email.'' . "\r\n";

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

  //$headers = "MIME-Version: 1.0\n" ;
  //$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
 // $headers .= "X-Priority: 1 (Highest)\n";
  //$headers .= "X-MSMail-Priority: High\n";
  //$headers .= "Importance: High\n";

  $message = "This is a multi-part message in MIME format.\n\n" .
  "-{$mime_boundary}\n" .
  "Content-type:text/html;charset=UTF-8\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $mainMessage. "\n\n";

$data = chunk_split(base64_encode($pdfdoc));
$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 email
if(mail($to,$subject,$message,$headers)){
  $customer_ID=$_GET['id'];
    $invoice_ref_no=$_GET['ref'];
  $successMsg = 'Invoice has sent successfully.';
  echo '<p style="color:green; font-weight:600; font-size:1.5em;">'.$successMsg.'</p>';
  echo '<p style="color:black; font-size:1.2em;">Shortly you will be re-directed back to the invoice page.</p>';
 echo '<meta content="3;invoice_details?id='.$customer_ID.'&ref='.$invoice_ref_no.'" http-equiv="refresh" />';
}
else{
  $customer_ID=$_GET['id'];
    $invoice_ref_no=$_GET['ref'];
  $errorMsg = 'Some problem occurred, please try again.';
  echo '<p style="color:red; font-weight:600; font-size:1.5em;">'.$errorMsg.'</p>';
  echo '<p style="color:black; font-size:1.2em;">Shortly you will be re-directed back to the invoice page.</p>';
  echo '<meta content="3;invoice_details?id='.$customer_ID.'&ref='.$invoice_ref_no.'" http-equiv="refresh" />';
}
}
?>

1 个答案:

答案 0 :(得分:0)

请使用phpmailer类库来获得所需的输出。

https://github.com/PHPMailer/PHPMailer