0

0

php socket使用smtp发送带附件的邮件

php中文网

php中文网

发布时间:2016-07-25 08:54:43

|

1157人浏览过

|

来源于php中文网

原创

  1. /**
  2. * php socket smtp发送邮件
  3. * edit: bbs.it-home.org
  4. */
  5. //define("SOL", " ");

  6. define("EOL", " ");
  7. define("SMTP_HOST", "smtp.163.com");//SMTP服务器
  8. define("SMTP_PORT", "25");//SMTP服务器端口
  9. define("SMTP_USER", "");//SMTP服务器的用户帐号
  10. define("SMTP_PASS", "");//SMTP服务器的用户密码
  11. $from = "";//SMTP服务器的用户邮箱

  12. $to = "";//发送给谁 可用逗号隔开多个邮箱
  13. $cc = "";
  14. $bcc = "";
  15. $subject="这是一个由PHP发送的带附件的邮件";//邮件主题 很多客户端会有乱码,所以转一下码

  16. $body = "这个是一个带附件的邮件发送程序
    看到没,这里显示了HTM标签哦;请点开链接 ".date('Y-m-d H:i:s');//邮件内容
  17. $smtp = new smtp(SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS, true);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  18. $smtp->addAttachment("mail.zip");
  19. $smtp->sendmail($to, $from, $subject, $body, $cc, $bcc);
  20. class smtp {

    立即学习PHP免费学习笔记(深入)”;

  21. /* Public Variables */

  22. public $attachments = array();
  23. /* Private Variables */
  24. private $smtp_host;
  25. private $smtp_port;
  26. private $time_out;
  27. private $host_name;
  28. private $auth;
  29. private $user;
  30. private $pass;
  31. private $sock;
  32. /* Constractor */

  33. public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) {
  34. $this->smtp_host = (!empty($smtp_host)) ? $smtp_host : SMTP_HOST;
  35. $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : SMTP_PORT;
  36. $this->user = (!empty($user)) ? $user : SMTP_PORT;
  37. $this->pass = (!empty($pass)) ? $pass : SMTP_PORT;
  38. $this->auth = $auth;
  39. $this->time_out = 15;
  40. #
  41. $this->host_name = "localhost";
  42. $this->sock = FALSE;
  43. }
  44. /* Main Function */

    phpList
    phpList

    phpList提供开源电子邮件营销服务,包括分析、列表分割、内容个性化和退信处理。丰富的技术功能和安全稳定的代码基础是17年持续开发的结果。在95个国家使用,在20多种语言中可用,并用于去年发送了250亿封电子邮件活动。您可以使用自己的SMTP服务器部署它,或在http://phplist.com上获得免费的托管帐户。

    下载
  45. public function sendmail($to, $from, $subject = "", $body = "", $cc = "", $bcc = "") {
  46. $bndp = md5(uniqid("")) . rand(1000, 9999);
  47. $bnd = md5(uniqid("")) . rand(1000, 9999);
  48. list ($msec, $sec) = explode(" ", microtime());
  49. $mail_from = $this->strip_line_breaks($from);

  50. $mail_to = explode(",", $to);
  51. $body = preg_replace("/(^|( ))(\.)/", "", $body);
  52. if ($cc != "") $mail_to = array_merge($mail_to, explode(",", $cc));
  53. if ($bcc != "") $mail_to = array_merge($mail_to, explode(",", $bcc));
  54. $headers = "MIME-Version:1.0" . EOL;

  55. $headers .= "To: " . $to . EOL;
  56. if ($cc != "") {
  57. $headers .= "Cc: " . $cc . EOL;
  58. }
  59. $headers .= "From: $from" . EOL;
  60. $headers .= "Subject: " . $subject . EOL;
  61. $headers .= "Date: " . date("r") . EOL;
  62. $headers .= "X-Mailer: Webmail ver 1.0 (PHP Version/" . phpversion() . ")" . EOL;
  63. $headers .= "Message-ID: " . EOL;
  64. if (count($this->attachments) > 0) {
  65. $headers .= "Content-Type: multipart/mixed;" . EOL . chr(9) . " boundary="" . $bndp . """ . EOL . EOL;
  66. $headers .= '--'.$bndp . EOL;
  67. $headers .= 'Content-Type : multipart/alternative; boundary="' . $bnd . '"' . EOL . EOL;
  68. $headers .= '--' . $bnd . EOL;
  69. $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL;
  70. $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
  71. $headers .= $body . EOL;
  72. $headers .= '--' . $bnd . EOL;
  73. $headers .= 'Content-type: text/html; charset=utf-8' . EOL;
  74. $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
  75. $headers .= $body . EOL;
  76. $headers .= '--' . $bnd . '--' . EOL;
  77. foreach ($this->attachments as $att) {

  78. $headers .= "--" . $bndp . EOL . $att;
  79. }
  80. $headers .= '--' . $bndp . '--' . EOL;
  81. $this->clear_attachments();
  82. } else {
  83. $headers .= 'Content-Type : multipart/alternative;boundary="'.$bnd.'"' . EOL . EOL;
  84. $headers .= '--'.$bnd . EOL;
  85. $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL;
  86. $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
  87. $headers .= $body . EOL;
  88. $headers .= '--'.$bnd . EOL;
  89. $headers .= 'Content-type: text/html; charset=utf-8' . EOL;
  90. $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
  91. $headers .= $body . EOL;
  92. $headers .= '--'.$bnd.'--' . EOL;
  93. }
  94. $sent = TRUE;

  95. foreach ($mail_to as $rcpt_to) {
  96. $rcpt_to = $this->strip_line_breaks($rcpt_to);
  97. if (!$this->smtp_sockopen($rcpt_to)) {
  98. $this->log_write("Error: Cannot send email to " . $rcpt_to);
  99. $sent = FALSE;
  100. continue;
  101. }
  102. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) {
  103. $this->log_write("E-mail has been sent to ");
  104. } else {
  105. $this->log_write("Error: Cannot send email to ");
  106. $sent = FALSE;
  107. }
  108. fclose($this->sock);
  109. }
  110. $this->log_write("{$mail_to} send over;");
  111. return $sent;
  112. }
  113. public function addAttachment($file, $dispo = "attachment") {

  114. $file_data = (file_exists($file)) ? file_get_contents($file) : "";
  115. if ($file_data != "") {
  116. $filename = basename($file);
  117. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  118. $chunks = chunk_split(base64_encode($file_data));
  119. $parts = "Content-Type: application/$ext; name="" . $filename . """ . EOL;
  120. $parts .= "Content-Transfer-Encoding: base64" . EOL;
  121. $parts .= "Content-Disposition: " . $dispo . "; filename="" . $filename . """ . EOL . EOL;
  122. $parts .= $chunks . EOL . EOL;
  123. $this->attachments[] = $parts;
  124. }
  125. }
  126. private function clear_attachments() {

  127. unset($this->attachments);
  128. $this->attachments = array();
  129. }
  130. /* Private Functions */

  131. private function smtp_send($helo, $from, $to, $header, $body = "") {
  132. if (!$this->smtp_putcmd("HELO", $helo)) {
  133. //$this->log_write("Error: Error occurred while sending HELO command.");
  134. return FALSE;
  135. }
  136. #auth
  137. if ($this->auth) {
  138. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  139. //$this->log_write("Error: Error occurred while sending HELO command.");
  140. return FALSE;
  141. }
  142. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  143. //$this->log_write("Error: Error occurred while sending HELO command.");
  144. return FALSE;
  145. }
  146. }
  147. if (!$this->smtp_putcmd("MAIL", "FROM:")) {
  148. //$this->log_write("Error: Error occurred while sending MAIL FROM command.");
  149. return FALSE;
  150. }
  151. if (!$this->smtp_putcmd("RCPT", "TO:")) {
  152. //$this->log_write("Error: Error occurred while sending RCPT TO command.");
  153. return FALSE;
  154. }
  155. if (!$this->smtp_putcmd("DATA")) {
  156. //$this->log_write("Error: Error occurred while sending DATA command.");
  157. return FALSE;
  158. }
  159. if (!$this->smtp_message($header, $body)) {
  160. //$this->log_write("Error: Error occurred while sending message.");
  161. return FALSE;
  162. }
  163. if (!$this->smtp_eom()) {
  164. //$this->log_write("Error: Error occurred while sending . [EOM].");
  165. return FALSE;
  166. }
  167. if (!$this->smtp_putcmd("QUIT")) {
  168. //$this->log_write("Error: Error occurred while sending QUIT command.");
  169. return FALSE;
  170. }
  171. return TRUE;
  172. }
  173. private function smtp_sockopen($address) {
  174. if ($this->smtp_host == "") {
  175. return $this->smtp_sockopen_mx($address);
  176. } else { // bbs.it-home.org
  177. return $this->smtp_sockopen_relay();
  178. }
  179. }
  180. private function smtp_sockopen_relay() {
  181. $this->log_write("Trying to Connect " . $this->smtp_host . ":" . $this->smtp_port . "...");
  182. $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  183. if (!($this->sock && $this->smtp_ok())) {
  184. $this->log_write("Error: connenct error" . $errstr . " (" . $errno . ")");
  185. return FALSE;
  186. }
  187. $this->log_write("Connected Ok");
  188. return TRUE;
  189. }
  190. private function smtp_sockopen_mx($address) {
  191. $domain = preg_replace("/^.+@([^@]+)$/", "", $address);
  192. if (!@getmxrr($domain, $MXHOSTS)) {
  193. $this->log_write("Error: Cannot resolve MX "" . $domain . """);
  194. return FALSE;
  195. }
  196. foreach ($MXHOSTS as $host) {
  197. $this->log_write("Trying to " . $host . ":" . $this->smtp_port);
  198. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  199. if (!($this->sock && $this->smtp_ok())) {
  200. $this->log_write("Connect Error ," . $errstr . " (" . $errno . ")");
  201. continue;
  202. }
  203. $this->log_write("Connected to mx host " . $host);
  204. return TRUE;
  205. }
  206. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")");
  207. return FALSE;
  208. }
  209. private function smtp_message($header, $body) {
  210. fputs($this->sock, $header . " " . $body);
  211. return TRUE;
  212. }
  213. private function smtp_eom() {
  214. fputs($this->sock, " . ");
  215. return $this->smtp_ok();
  216. }
  217. private function smtp_ok() {
  218. $response = str_replace(" ", "", fgets($this->sock, 512));
  219. if (!preg_match("/^[23]/", $response)) {
  220. fputs($this->sock, "QUIT ");
  221. fgets($this->sock, 512);
  222. $this->log_write("Error: Remote host returned "" . $response . """);
  223. return FALSE;
  224. }
  225. return TRUE;
  226. }
  227. private function smtp_putcmd($cmd, $arg = "") {
  228. if ($arg != "") $cmd = ($cmd == "") ? $arg : ($cmd . " " . $arg);
  229. fputs($this->sock, $cmd . " ");
  230. return $this->smtp_ok();
  231. }
  232. private function strip_line_breaks($address) {
  233. $address = preg_replace("/([ ])+/", "", $address);
  234. $address = preg_replace("/^.*.*$/", "", $address);
  235. return $address;
  236. }
  237. public function log_write($message) {
  238. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  239. file_put_contents(dirname(__FILE__) . '/mail.log', $message . PHP_EOL, FILE_APPEND | LOCK_EX);
  240. }
  241. }
复制代码


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

1044

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

334

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

213

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

35

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

111

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

77

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

813

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

97

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号