gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:30:34 28 4
gpt4 key购买 nike

我正在使用FPDF创建pdf 。 PDF 正在完美生成,并且 PDF 也可以通过电子邮件获取。但我也想发送正文信息。我尝试过使用正文消息。示例 精美短信 这是来自 shohag 的短信 但只有 pdf 附件可用,正文为空。这是我的代码。

function send_pdf_to_user(){
if($_REQUEST['action'] == 'pdf_invoice' ){
require('html2pdf.php');
$pdf=new PDF_HTML();
$pdf->SetFont('Arial','',11);
$pdf->AddPage();

$text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
if(ini_get('magic_quotes_gpc')=='1')
$text=stripslashes($text);
$pdf->WriteHTML($text);

//documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
$attach_pdf_multipart = chunk_split( base64_encode( $pdf->Output( '', 'S' ) ) );


//define the receiver of the email
$to = 'monirulmask@gmail.com';

//define the subject of the email
$subject = 'Test Invoice';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@test.ch\r\nReply-To: webmaster@test.ch";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";



$msg .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment\r\n";
$msg .= $attach_pdf_multipart . "\r\n";

$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$msg .= "<p>This is text message from shohag</p>\r\n\r\n";

global $message;
$message = '';
$mail_sent = @mail( $to, $subject, $msg, $headers );
//@mail( $to1, $subject, $msg, $headers );
if(!empty($mail_sent)):
$message = "Invoice sent succuessfully";
else:
$message = "Error occured. Please try again.";
endif;
}
}

请检查我的代码并让我了解进一步的可能性。提前致谢。

最佳答案

您可以使用PHPMailerFPDF 。它可以正常工作,没有任何麻烦。您需要更改 $pdf->Output 的参数。下载并复制 class.phpmailer.phpPHPMailerAutoload.php 到您的工作文件夹。在 require('html2pdf.php'); 下方或上方附加 class.phpmailer.php 。我之前已经这样做过,所以这会起作用。根据您的代码,这应该有效。

function send_pdf_to_user(){
if($_REQUEST['action'] == 'pdf_invoice' ){
require('html2pdf.php');
require_once('class.phpmailer.php');
$pdf=new PDF_HTML();
$pdf->SetFont('Arial','',11);
$pdf->AddPage();

$text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
if(ini_get('magic_quotes_gpc')=='1')
$text=stripslashes($text);
$pdf->WriteHTML($text);

$mail = new PHPMailer(); // defaults to using php "mail()"
$body = "This is test mail by monirul";

$mail->AddReplyTo("webmaster@test.ch","Test Lernt");
$mail->SetFrom('webmaster@test.ch', 'Test Lernt');

$address = "monirulmask@gmail.com";
$mail->AddAddress($address, "Abdul Kuddos");
$mail->Subject = "Test Invoice";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);
//documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
$pdf->Output("Test Invoice.pdf","F");
$path = "Walter Lernt Invoice.pdf";

$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
global $message;
if(!$mail->Send()) {
$message = "Invoice could not be send. Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Invoice sent!";
}

}
}

关于php 发送带有 PDF 附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21556757/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com