全部
常见问题
产品动态
精选推荐

分享一个定制的 将订单发生到邮箱打印的代码。

管理 管理 编辑 删除

<?php

// 1. 从数据库查询订单数据

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "your_database";


$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {

   die("连接数据库失败: " . $conn->connect_error);

}


$sql = "SELECT * FROM orders";

$result = $conn->query($sql);


if ($result->num_rows > 0) {

   while ($row = $result->fetch_assoc()) {

       // 2. 构建发货单内容

       $orderNumber = $row['order_number'];

       $customerName = $row['customer_name'];

       $shippingAddress = $row['shipping_address'];

       // 其他字段根据需要自行添加


       $invoiceContent = "<h1>发货单</h1>";

       $invoiceContent .= "<p>订单号:" . $orderNumber . "</p >";

       $invoiceContent .= "<p>收货人姓名:" . $customerName . "</p >";

       $invoiceContent .= "<p>收货地址:" . $shippingAddress . "</p >";

       // 其他字段也可以在此处添加到发货单内容中


       // 3. 打印发货单

       $printerDevice = "TP874VHH172"; // 打印机设备名称


       $printerHandle = printer_open($printerDevice);

       if (!$printerHandle) {

           echo "无法打开打印机,请检查打印机驱动程序是否正确安装。";

           exit;

       }


       $printerStatus = printer_status($printerHandle);

       if ($printerStatus['status'] == PRINTER_STATUS_ERROR) {

           echo "打印机错误,请检查打印机是否正常工作。";

           exit;

       }


       // 设置打印机参数,如纸张类型、对齐方式等

       printer_set_option($printerHandle, PRINTER_MODE, "RAW");

       printer_set_option($printerHandle, PRINTER_TEXT_ALIGN, PRINTER_TA_LEFT);


       // 开始打印

       printer_start_doc($printerHandle, "发货单");

       printer_start_page($printerHandle);


       printer_set_option($printerHandle, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_CUSTOM);

       printer_set_option($printerHandle, PRINTER_PAPER_WIDTH, 80); // 假设纸张宽度为80字符


       printer_write($printerHandle, $invoiceContent);

       

       printer_end_page($printerHandle);

       printer_end_doc($printerHandle);

       printer_close($printerHandle);


       // 4. 将发货单保存为文件并发送到指定位置

       $fileLocation = "path/to/save/invoice_" . $orderNumber . ".pdf";


       $pdf = new TCPDF(); // 假设您已经安装了TCPDF库

       $pdf->AddPage();

       $pdf->writeHTML($invoiceContent);

       $pdf->Output($fileLocation, 'F');


       // 发送到指定位置(假设通过电子邮件发送)

       $recipientEmail = "[email protected]";

       $subject = "发货单";

       $message = "请查收附件中的发货单。";

       $attachment = $fileLocation;


       $headers = "From: [email protected]\r\n";

       $headers .= "Reply-To: [email protected]\r\n";

       $headers .= "MIME-Version: 1.0\r\n";

       $headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";


       $fileContent = file_get_contents($attachment);

       $fileType = mime_content_type($attachment);

       $fileName = basename($attachment);


       $message .= "--boundary\r\n";

       $message .= "Content-Type: " . $fileType . "; name=\"" . $fileName . "\"\r\n";

       $message .= "Content-Transfer-Encoding: base64\r\n";

       $message .= "Content-Disposition: attachment; filename=\"" . $fileName . "\"\r\n\r\n";

       $message .= chunk_split(base64_encode($fileContent)) . "\r\n";

       $message .= "--boundary--";


       mail($recipientEmail, $subject, $message, $headers);

   }

} else {

   echo "没有找到订单数据。";

}


$conn->close();

?>



怎么用我也不会。有会的来教一下。妈的二开的跑路了,工程烂尾了

请登录后查看

吴汐 最后编辑于2023-09-07 10:25:12

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
779
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

A0000千山花田蝴蝶兰 作者
暂无简介

回答

68

发布

26

经验

1946

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服