php 发邮件附件pdf,PHP发送包含PDF附件的电子邮件而不创建文件?

我使用FPDF生成PDF,我需要通过电子邮件发送给客户.

由于我可以从数据库生成PDF,因此我不想在本地保存所有PDF,因为它会使我的服务器变得杂乱无章.

我希望能够发送附有PDF的电子邮件.我是否需要创建一个临时文件,然后每次都删除它还是有另一种方法吗?

解决方法:

//define the receiver of the email

$to = 'youraddress@example.com';

//define the subject of the email

$subject = 'Test email with attachment';

//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@example.com\r\nReply-To: webmaster@example.com";

//add boundary string and mime type specification

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

//read the atachment file contents into a string,

//encode it with MIME base64,

//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

//define the body of the message.

ob_start(); //Turn on output buffering

?>

--PHP-mixed-<?php echo $random_hash; ?>

Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/plain; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

Hello World!!!

This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

Hello World!

This is something with HTML formatting.

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>

Content-Type: application/zip; name="attachment.zip"

Content-Transfer-Encoding: base64

Content-Disposition: attachment

--PHP-mixed-<?php echo $random_hash; ?>--

//copy current buffer contents into $message variable and delete current output buffer

$message = ob_get_clean();

//send the email

$mail_sent = @mail( $to, $subject, $message, $headers );

//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"

echo $mail_sent ? "Mail sent" : "Mail failed";

?>

你会注意到这一行

$attachment = chunk_split(base64_encode(file_get_contents(‘attachment.zip’)));并更改以适合您的内存文件.

标签:php,pdf,file,email

来源: https://codeday.me/bug/20190928/1829303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Magento中发送带有PDF附件电子邮件,您可以按照以下步骤进行操作: 1. 创建一个新的Magento模板,其中包含您要在电子邮件包含的文本和变量。 2. 在您的Magento主题文件夹中创建一个名为“pdf”的文件夹,然后将您要附加到电子邮件中的PDF文件保存到这个文件夹中。 3. 使用以下代码将PDF文件附加到电子邮件中: ```php $pdfFilePath = Mage::getBaseDir('media') . DS . 'pdf' . DS . 'example.pdf'; // Replace example.pdf with your PDF file name $pdfContent = file_get_contents($pdfFilePath); $pdfAttachment = new Zend_Mime_Part($pdfContent); $pdfAttachment->type = 'application/pdf'; $pdfAttachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $pdfAttachment->encoding = Zend_Mime::ENCODING_BASE64; $pdfAttachment->filename = 'example.pdf'; // Replace example.pdf with your PDF file name $mail = Mage::getModel('core/email_template'); $mail->getMail()->addAttachment($pdfAttachment); ``` 4. 将完整的电子邮件代码添加到您的Magento模板中,并使用以下代码发送电子邮件: ```php $mail->setDesignConfig(array('area' => 'frontend', 'store' => Mage::app()->getStore()->getId())) ->setTemplateSubject('Your email subject') ->sendTransactional( Mage::getStoreConfig('contacts/email/ident_general_email'), array('name' => 'Recipient Name', 'email' => 'recipient@example.com'), null, null, array('pdf_attachment' => $pdfAttachment) ); ``` 请注意,您需要将电子邮件代码中的“example.pdf”替换为您要附加到电子邮件中的实际PDF文件名,并将电子邮件代码中的收件人名称和电子邮件地址替换为您要发送电子邮件的实际收件人信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值