当前位置: 首页 > 知识库问答 >
问题:

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

杨雪松
2023-03-14

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

其次,当邮件被发送时,除了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个答案

洪俊拔
2023-03-14

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

https://github.com/PHPMailer/PHPMailer

 类似资料:
  • 尝试发送带有pdf附件的电子邮件,尝试使用swickmailer,但没有成功,此代码使用zip但不使用PDF:( 邮件被发送罚款,我得到的邮件:但附件是不存在的,在meial有所有的bas64编码在电子邮件像: onatatent-Type: Application/octet-stream; name="media.pdf"Content-transver-Encode: base 64 Con

  • 问题内容: 我在网站上使用PHP,并且想添加电子邮件功能。 我已经安装了WAMPSERVER。 如何使用PHP发送电子邮件? 问题答案: 使用PHP的功能是可能的。请记住,邮件功能在本地服务器上不起作用。

  • 问题内容: 我在理解如何使用Python通过电子邮件发送附件时遇到问题。我已成功通过电子邮件将简单消息通过电子邮件发送。有人可以在电子邮件中说明如何发送附件。我知道在线上还有其他帖子,但是作为Python初学者,我很难理解它们。 问题答案: 这是另一个:

  • 我目前正试图发送一封电子邮件,并附上一份pdf格式的表格作为附件。 我正在使用FPDM库填充我的pdf:https://github.com/codeshell/fpdm 这是我的代码: 然后我试着发送一封邮件,里面有这个填好的pdf作为附件。以下是我的邮寄方式: 这种“电子邮件发送”功能在普通PDF中运行良好,但在我的PDF中,所有文本和字段都会消失。我想知道是有办法还是不可能? 此外,我正在使

  • 问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送

  • 我在发送带有多个附件的电子邮件时遇到问题。代码如下: 我收到邮件,但找不到任何附件。有人知道会出什么问题吗? 下面是我在Mail中得到的电子邮件正文: