使用 MPDF 发送 PDF 电子邮件

Posted

技术标签:

【中文标题】使用 MPDF 发送 PDF 电子邮件【英文标题】:Sending PDF email with MPDF 【发布时间】:2017-07-21 00:31:27 【问题描述】:

我已经设置了系统,当点击按钮时,合同会附上 pdf。我已经使用 mpdf 将电子邮件发送到所需的电子邮件地址。 以下代码在我的本地主机上运行良好并且可以正确完成工作,但是当我将它放在服务器上时,它没有显示任何错误,但也没有发送任何电子邮件,因为我没有收到任何东西。 你能帮我弄清楚我做错了什么吗?

// Setup PDF
    $mpdf = new mPDF('utf-8', 'Letter', 0, '', 0, 0, 0, 0, 0, 0); // New PDF object with encoding & page size
    ob_start();
    $mpdf->setAutoTopMargin = 'stretch'; // Set pdf top margin to stretch to avoid content overlapping
    $mpdf->setAutoBottomMargin = 'stretch'; // Set pdf bottom margin to stretch to avoid content overlapping
    $mpdf->Writehtml($stylesheet,1); // Writing style to pdf
    $mpdf->WriteHTML($html); // Writing html to pdf
    // FOR EMAIL
    $content = $mpdf->Output('', 'S'); // Saving pdf to attach to email 
    $content = chunk_split(base64_encode($content));
    $emailto = 'abc@abc.com';
    //The function to send email when the contract is requested
    $recepients = array(
      $email,
      'abc@abc.com'
      );
    $mailto = implode(',', $recepients);
    $from_name = 'Random Project';
    $from_mail = 'abc@abc.com';
    $replyto = 'abc@abc.com';
    $uid = md5(uniqid(time())); 
    $subject = 'Wedding Contract Form';
    $message =  '
      </<!DOCTYPE html>
      <html>
      </style> 
      <body>
      <img src="http://tanglewoodestate.com.au/assets/contract-header.png" >
      <div width ="300px" style="background-color:#f5e9dd;padding:10px">
      <p>Hi <strong>'.$cname.'</strong>,<p>
      <p>Please Find the <strong>attached contract</strong> from the Tanglewood Estate.</p>
      <p>For any questions or queries,Please contact Us at:<br>
      ABC<br>
      abc@abc.com<br>
      </div>
      <img src="http://tanglewoodestate.com.au/assets/contract-footer.png" >
      </body>
      </html>';
    $filename = 'Wedding_contract.pdf';
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html;charset=UTF-8\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    $is_sent = @mail($mailto, $subject, "", $header);
    //$mpdf->Output(); // For sending Output to browser

    //$mpdf->Output('Wedding_contract.pdf','D'); // For Download
    ob_end_flush();
    

【问题讨论】:

不发送和不接收不是一回事 你最好使用标准的邮件库之一,例如 phpmailer,而不是 mail() rtfms - 仅当您能理解出于测试目的我将其发送给自己时。 【参考方案1】:

答案取自线程:mPDF Auto Generated PDF Mailer sends blank Email by https://***.com/users/3818025/drehx

所以,我只是使用 PHP Mailer 来发送电子邮件。只是发布这个来帮助任何想用它作为例子的人:

try 

$mail = new PHPMailer; 
$mail->AddAddress($email);
$mail->AddAddress('abc@abc.com');
$mail->AddAddress('abc@abc.com');
$mail->SetFrom('abc@abc.com');
$mail->AddReplyTo('abc@abc.com');
$mail->Subject = 'Tanglewood Contract';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->isHTML(true);
$mail->Body = '</<!DOCTYPE html>
  <html>
  </style> 
  <body>
  <img src="http://tanglewoodestate.com.au/assets/contract-header.png" >
  <div width ="300px" style="background-color:#f5e9dd;padding:10px">
  <p>Hi <strong>'.$cname.'</strong>,<p>
  <p>Please Find the <strong>attached contract</strong> from the Tanglewood Estate.</p>
  <p>For any questions or queries,Please contact Us at:<br>
  Tanglewood Estate<br>
  abc@abc.com<br>
  </div>
  <img src="http://tanglewoodestate.com.au/assets/contract-footer.png" >
  </body>
  </html>';

//$mail->MsgHTML("*** Form attached! Please see the attached form (.pdf).");
$mail->AddStringAttachment($content, $filename = 'TanglewoodContract.pdf',
      $encoding = 'base64',
      $type = 'application/pdf');      // attachment
if (isset($_FILES['attached']) &&
    $_FILES['attached']['error'] == UPLOAD_ERR_OK) 
    $mail->AddAttachment($_FILES['attached']['tmp_name'],
                         $_FILES['attached']['name']);

$mail->Send();
echo "<div style='margin-left:4%;'>Message Sent OK</div>\n";
 catch (phpmailerException $e) 
echo $e->errorMessage(); //Pretty error messages from PHPMailer
 catch (Exception $e) 
echo $e->getMessage(); //Boring error messages from anything else!

【讨论】:

以上是关于使用 MPDF 发送 PDF 电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

使用 MPDF 将 HTML 保存为 PDF 和电子邮件

mPDF 错误:一些数据已经输出到浏览器,无法发送 PDF 文件

无法使用 CakePHP Mail 发送电子邮件附件 PDF

php发送带有PDF附件的电子邮件

如何使用 Python 将 pdf 文件发送到多个电子邮件?

使用 django 通过模板管理 pdf 并作为电子邮件附件发送