0

0

php写的smtp邮件发送类

php中文网

php中文网

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

|

1033人浏览过

|

来源于php中文网

原创

  1. $smtpserver = "*****";
  2. $smtpserverport = 25;
  3. $smtpuser = "******";
  4. $smtppass = "*******";
  5. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  6. $smtp->debug = false;
  7. //$emailtype = "HTML";
  8. for ($i=0; $i
  9. $smtp->sendmail("*****", "******", "Hello world!","This is only a test!");
  10. }
  11. echo "共发送了 $i 封邮件!";
  12. ?>
复制代码

下面是具体类的实现。

  1. class smtp {
  2. /* public variables */
  3. var $smtp_port;
  4. var $time_out;
  5. var $host_name;
  6. var $log_file;
  7. var $relay_host;
  8. var $debug;
  9. var $auth;
  10. var $user;
  11. var $pass;
  12. /* private variables */
  13. var $sock;
  14. /* constractor */
  15. function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  16. {
  17. $this->debug = false;
  18. $this->smtp_port = $smtp_port;
  19. $this->relay_host = $relay_host;
  20. $this->time_out = 30; //is used in fsockopen()
  21. $this->auth = $auth; //auth
  22. $this->user = $user;
  23. $this->pass = $pass;
  24. $this->host_name = "localhost"; //is used in helo command
  25. $this->log_file = "";
  26. $this->sock = false;
  27. }
  28. /* main function */
  29. function sendmail($to, $from, $subject = "", $body = "", $mailtype= "", $cc = "", $bcc = "", $additional_headers = "")
  30. {
  31. $mail_from = $this->get_address($this->strip_comment($from));
  32. $body = ereg_replace("(^|( ))(.)", "1.3", $body);
  33. $header .= "mime-version:1.0 ";
  34. if ($mailtype == "html") {
  35. $header .= "content-type:text/html ";
  36. }
  37. $header .= "to: " . $to . " ";
  38. if ($cc != "") {
  39. $header .= "cc: " . $cc . " ";
  40. }
  41. $header .= "from: $from; ";
  42. $header .= "subject: " . $subject . " ";
  43. $header .= $additional_headers;
  44. $header .= "date: " . date("r") . " ";
  45. $header .= "x-mailer:by redhat (php/" . phpversion() . ") ";
  46. list($msec, $sec) = explode(" ", microtime());
  47. $header .= "message-id: ; ";
  48. $to = explode(",", $this->strip_comment($to));
  49. if ($cc != "") {
  50. $to = array_merge($to, explode(",", $this->strip_comment($cc)));
  51. }
  52. if ($bcc != "") {
  53. $to = array_merge($to, explode(",", $this->strip_comment($bcc)));
  54. }
  55. $sent = true;
  56. foreach ($to as $rcpt_to) {
  57. $rcpt_to = $this->get_address($rcpt_to);
  58. if (!$this->smtp_sockopen($rcpt_to)) {
  59. $this->log_write("error: cannot send email to " . $rcpt_to . " ");
  60. $sent = false;
  61. continue;
  62. }
  63. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  64. $this->log_write("e-mail has been sent to ; ");
  65. } else {
  66. $this->log_write("error: cannot send email to ; ");
  67. $sent = false;
  68. }
  69. fclose($this->sock);
  70. $this->log_write("disconnected from remote host ");
  71. }
  72. return $sent;
  73. }
  74. /* Private Functions */

    迦恩计算机资源网源码(图书销售类)
    迦恩计算机资源网源码(图书销售类)

    采用三层架构开发,前台集成了产品在线展示,用户注册、在线调查、在线投稿后台有类别管理\图书管理\订单管理\会员管理\配送范围管理\邮件列表\广告管理\友情链接管理等后台添加图书时自动生成缩略图和文字水印主要参考了petshop的设计架构、使用了Asp.net2.0中很多MemberShip、master等新功能后台管理地址/web/admin/ 超级管理员账号密码均为aspx1特别提示:该系统需要

    下载
  75. function smtp_send($helo, $from, $to, $header, $body = "")
  76. {
  77. if (!$this->smtp_putcmd("HELO", $helo)) {
  78. return $this->smtp_error("sending HELO command");
  79. }
  80. // auth
  81. if ($this->auth) {
  82. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  83. return $this->smtp_error("sending HELO command");
  84. }
  85. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  86. return $this->smtp_error("sending HELO command");
  87. }
  88. }
  89. if (!$this->smtp_putcmd("MAIL", "FROM:;")) {
  90. return $this->smtp_error("sending MAIL FROM command");
  91. }
  92. if (!$this->smtp_putcmd("RCPT", "TO:;")) {
  93. return $this->smtp_error("sending RCPT TO command");
  94. }
  95. if (!$this->smtp_putcmd("DATA")) {
  96. return $this->smtp_error("sending DATA command");
  97. }
  98. if (!$this->smtp_message($header, $body)) {
  99. return $this->smtp_error("sending message");
  100. }
  101. if (!$this->smtp_eom()) {
  102. return $this->smtp_error("sending ;;.;; [EOM]");
  103. }
  104. if (!$this->smtp_putcmd("QUIT")) {
  105. return $this->smtp_error("sending QUIT command");
  106. }
  107. return true;
  108. }
  109. function smtp_sockopen($address)
  110. {
  111. if ($this->relay_host == "") {
  112. return $this->smtp_sockopen_mx($address);
  113. } else {
  114. return $this->smtp_sockopen_relay();
  115. }
  116. }
  117. function smtp_sockopen_relay()
  118. {
  119. $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . " ");
  120. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  121. if (!($this->sock && $this->smtp_ok())) {
  122. $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . " ");
  123. $this->log_write("Error: " . $errstr . " (" . $errno . ") ");
  124. return false;
  125. }
  126. $this->log_write("Connected to relay host " . $this->relay_host . " ");
  127. return true;;
  128. }
  129. function smtp_sockopen_mx($address)
  130. {
  131. $domain = ereg_replace("^.+@([^@]+)$", "1", $address);
  132. if (!@getmxrr($domain, $MXHOSTS)) {
  133. $this->log_write("Error: Cannot resolve MX "" . $domain . "" ");
  134. return false;
  135. }
  136. foreach ($MXHOSTS as $host) {
  137. $this->log_write("Trying to " . $host . ":" . $this->smtp_port . " ");
  138. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  139. if (!($this->sock && $this->smtp_ok())) {
  140. $this->log_write("Warning: Cannot connect to mx host " . $host . " ");
  141. $this->log_write("Error: " . $errstr . " (" . $errno . ") ");
  142. continue;
  143. }
  144. $this->log_write("Connected to mx host " . $host . " ");
  145. return true;
  146. }
  147. $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ") ");
  148. return false;
  149. }
  150. function smtp_message($header, $body)
  151. {
  152. fputs($this->sock, $header . " " . $body);
  153. $this->smtp_debug(">; " . str_replace(" ", " " . ">; ", $header . " >; " . $body . " >; "));
  154. return true;
  155. }
  156. function smtp_eom()
  157. {
  158. fputs($this->sock, " . ");
  159. $this->smtp_debug(". [EOM] ");
  160. return $this->smtp_ok();
  161. }
  162. function smtp_ok()
  163. {
  164. $response = str_replace(" ", "", fgets($this->sock, 512));
  165. $this->smtp_debug($response . " ");
  166. if (!ereg("^[23]", $response)) {
  167. fputs($this->sock, "QUIT ");
  168. fgets($this->sock, 512);
  169. $this->log_write("Error: Remote host returned "" . $response . "" ");
  170. return false;
  171. }
  172. return true;
  173. }
  174. function smtp_putcmd($cmd, $arg = "")
  175. {
  176. if ($arg != "") {
  177. if ($cmd == "") $cmd = $arg;
  178. else $cmd = $cmd . " " . $arg;
  179. }
  180. fputs($this->sock, $cmd . " ");
  181. $this->smtp_debug(">; " . $cmd . " ");
  182. return $this->smtp_ok();
  183. }
  184. function smtp_error($string)
  185. {
  186. $this->log_write("Error: Error occurred while " . $string . ". ");
  187. return false;
  188. }
  189. function log_write($message)
  190. {
  191. $this->smtp_debug($message);
  192. if ($this->log_file == "") {
  193. return true;
  194. }
  195. $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  196. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  197. $this->smtp_debug("Warning: Cannot open log file "" . $this->log_file . "" ");
  198. return false;;
  199. }
  200. flock($fp, LOCK_EX);
  201. fputs($fp, $message);
  202. fclose($fp);
  203. return true;
  204. }
  205. function strip_comment($address)
  206. {
  207. $comment = "([^()]*)";
  208. while (ereg($comment, $address)) {
  209. $address = ereg_replace($comment, "", $address);
  210. }
  211. return $address;
  212. }
  213. function get_address($address)
  214. {
  215. $address = ereg_replace("([ ])+", "", $address);
  216. $address = ereg_replace("^.*;.*$", "1", $address);
  217. return $address;
  218. }
  219. function smtp_debug($message)
  220. {
  221. if ($this->debug) {
  222. echo $message . ";";
  223. }
  224. }
  225. }
  226. ?>
复制代码


相关文章

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官方网站,实现便捷、安全的网页端浏览与账号登录体验。

928

2026.02.13

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

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

307

2026.02.13

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

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

183

2026.02.13

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

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

29

2026.02.13

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

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

103

2026.02.13

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

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

54

2026.02.12

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

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

17

2026.02.12

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

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

764

2026.02.12

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

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

92

2026.02.12

热门下载

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

精品课程

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

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