简体   繁体   中英

How to send email body and PDF attachment using PHP?

The script below can send pdf file as an attachment to Gmail and Outlook, but will not display in Yahoo mail. What's wrong with my code?

Secondly, when mail is sent, it doesn't carry along the message body apart from the pdf attachment itself. Any suggestion on how I can add message body with pdf attachment will be appreciated.

<?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" />';
}
}
?>

kindly use phpmailer class library to get your desired output.

https://github.com/PHPMailer/PHPMailer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM