PrestaShop 1.7.5中如何将生成的条码附加到订单确认邮件?
解决PrestaShop订单确认邮件添加条码PNG的问题
你已经成功生成了base64编码的PNG条码,但在邮件中附加的方式出现了问题。下面针对两种常见需求(作为附件/嵌入邮件正文)给出可行的解决方案:
一、将条码作为邮件附件发送
你之前尝试的$extra_pdf数组构造有误——因为你生成的是base64字符串,并非本地文件路径,PrestaShop的Mail::Send方法需要二进制格式的附件内容。正确步骤如下:
- 先把base64编码的条码解码为二进制内容:
$barcodeBinary = base64_decode($barcode);
- 构造符合要求的附件数组:
$attachments = array( array( 'content' => $barcodeBinary, // 这里是二进制内容,不是base64字符串 'name' => 'order-barcode.png', 'mime' => 'image/png' ) );
- 修改
Mail::Send调用,将附件数组传入对应参数:
Mail::Send( (int) $order->id_lang, 'order_conf', Context::getContext()->getTranslator()->trans('Order confirmation', array(), 'Emails.Subject', $orderLanguage->locale), $data, $this->context->customer->email, $this->context->customer->firstname . ' ' . $this->context->customer->lastname, null, null, $attachments, // 替换原有的$barcode参数 null, _PS_MAIL_DIR_, false, (int) $order->id_shop );
二、将条码嵌入邮件正文直接显示(非附件)
如果你希望条码直接展示在邮件内容里,而非作为附件下载,可以这样操作:
- 打开你的订单确认邮件模板(路径通常为
themes/你的主题/mails/目标语言/order_conf.html),添加内嵌图片的标签:
<div class="order-barcode"> <img src="data:image/png;base64,{$barcode}" alt="Order Barcode" style="max-width: 300px;" /> </div>
- 把生成的base64条码字符串传入邮件的
$data数组:
// 把条码变量加入邮件数据中 $data['barcode'] = $barcode;
- 正常调用
Mail::Send即可,无需传入附件参数:
Mail::Send( (int) $order->id_lang, 'order_conf', Context::getContext()->getTranslator()->trans('Order confirmation', array(), 'Emails.Subject', $orderLanguage->locale), $data, // 已包含barcode变量 $this->context->customer->email, $this->context->customer->firstname . ' ' . $this->context->customer->lastname, null, null, null, // 附件参数设为null null, _PS_MAIL_DIR_, false, (int) $order->id_shop );
额外注意事项
- 确保你的邮件模板是HTML格式,纯文本模板无法显示内嵌图片。
- 部分邮件客户端会阻止自动加载内嵌图片,作为附件的方式兼容性更强。
- 提前验证
$order->order_barcode的有效性,避免生成无效的条码内容。
内容的提问来源于stack exchange,提问作者hamdi ben henda
相关产品推荐
相关产品推荐

