搜索
首页后端开发php教程PHP支持发送HTML格式邮件的发送类

  1. /**
  2. * 邮件发送类
  3. * 支持发送纯文本邮件和HTML格式的邮件
  4. * @example
  5. * $config = array(
  6. * "from" => "*****",
  7. * "to" => "***",
  8. * "subject" => "test",
  9. * "body" => "test",
  10. * "username" => "***",
  11. * "password" => "****",
  12. * "isHTML" => true
  13. * );
  14. *
  15. * $mail = new MySendMail();
  16. *
  17. * $mail->setServer("smtp.126.com");
  18. *
  19. * $mail->setMailInfo($config);
  20. * if(!$mail->sendMail()) {
  21. * echo $mail->error();
  22. * return 1;
  23. * }
  24. */
  25. class MySendMail{
  26. /**
  27. * @var 邮件传输代理用户名
  28. * @access private
  29. */
  30. private $_userName;
  31. /**
  32. * @var 邮件传输代理密码
  33. * @access private
  34. */
  35. private $_password;
  36. /**
  37. * @var 邮件传输代理服务器地址
  38. * @access protected
  39. */
  40. protected $_sendServer;
  41. /**
  42. * @var 邮件传输代理服务器端口
  43. * @access protected
  44. */
  45. protected $_port=25;
  46. /**
  47. * @var 发件人
  48. * @access protected
  49. */
  50. protected $_from;
  51. /**
  52. * @var 收件人
  53. * @access protected
  54. */
  55. protected $_to;
  56. /**
  57. * @var 主题
  58. * @access protected
  59. */
  60. protected $_subject;
  61. /**
  62. * @var 邮件正文
  63. * @access protected
  64. */
  65. protected $_body;
  66. /**
  67. * @var 是否是HTML格式的邮件
  68. * @access protected
  69. */
  70. protected $_isHTML=true;
  71. /**
  72. * @var socket资源
  73. * @access protected
  74. */
  75. protected $_socket;
  76. /**
  77. * @var 错误信息
  78. * @access protected
  79. */
  80. protected $_errorMessage;
  81. public function __construct($from="", $to="", $subject="", $body="", $server="", $username="", $password="",$isHTML="", $port="") {
  82. if(!empty($from)){
  83. $this->_from = $from;
  84. }
  85. if(!empty($to)){
  86. $this->_to = $to;
  87. }
  88. if(!empty($subject)){
  89. $this->_subject = $subject;
  90. }
  91. if(!empty($body)){
  92. $this->_body = $body;
  93. }
  94. if(!empty($isHTML)){
  95. $this->_isHTML = $isHTML;
  96. }
  97. if(!empty($server)){
  98. $this->_sendServer = $server;
  99. }
  100. if(!empty($port)){
  101. $this->_port = $port;
  102. }
  103. if(!empty($username)){
  104. $this->_userName = $username;
  105. }
  106. if(!empty($password)){
  107. $this->_password = $password;
  108. }
  109. }
  110. /**
  111. * 设置邮件传输代理
  112. * @param string $server 代理服务器的ip或者域名
  113. * @param int $port 代理服务器的端口,smtp默认25号端口
  114. * @param int $localPort 本地端口
  115. * @return boolean
  116. */
  117. public function setServer($server, $port=25) {
  118. if(!isset($server) || empty($server) || !is_string($server)) {
  119. $this->_errorMessage = "first one is an invalid parameter";
  120. return false;
  121. }
  122. if(!is_numeric($port)){
  123. $this->_errorMessage = "first two is an invalid parameter";
  124. return false;
  125. }
  126. $this->_sendServer = $server;
  127. $this->_port = $port;
  128. return true;
  129. }
  130. /**
  131. * 设置邮件
  132. * @access public
  133. * @param array $config 邮件配置信息
  134. * 包含邮件发送人、接收人、主题、内容、邮件传输代理的验证信息
  135. * @return boolean
  136. */
  137. public function setMailInfo($config) {
  138. if(!is_array($config) || count($config) $this->_errorMessage = "parameters are required";
  139. return false;
  140. }
  141. $this->_from = $config['from'];
  142. $this->_to = $config['to'];
  143. $this->_subject = $config['subject'];
  144. $this->_body = $config['body'];
  145. $this->_userName = $config['username'];
  146. $this->_password = $config['password'];
  147. if(isset($config['isHTML'])){
  148. $this->_isHTML = $config['isHTML'];
  149. }
  150. return true;
  151. }
  152. /**
  153. * 发送邮件
  154. * @access public
  155. * @return boolean
  156. */
  157. public function sendMail() {
  158. $command = $this->getCommand();
  159. $this->socket();
  160. foreach ($command as $value) {
  161. if($this->sendCommand($value[0], $value[1])) {
  162. continue;
  163. }
  164. else{
  165. return false;
  166. }
  167. }
  168. $this->close(); //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放
  169. echo 'Mail OK!';
  170. return true;
  171. }
  172. /**
  173. * 返回错误信息
  174. * @return string
  175. */
  176. public function error(){
  177. if(!isset($this->_errorMessage)) {
  178. $this->_errorMessage = "";
  179. }
  180. return $this->_errorMessage;
  181. }
  182. /**
  183. * 返回mail命令
  184. * @access protected
  185. * @return array
  186. */
  187. protected function getCommand() {
  188. if($this->_isHTML) {
  189. $mail = "MIME-Version:1.0\r\n";
  190. $mail .= "Content-type:text/html;charset=utf-8\r\n";
  191. $mail .= "FROM:test_from . ">\r\n";
  192. $mail .= "TO:_to . ">\r\n";
  193. $mail .= "Subject:" . $this->_subject ."\r\n\r\n";
  194. $mail .= $this->_body . "\r\n.\r\n";
  195. }
  196. else{
  197. $mail = "FROM:test_from . ">\r\n";
  198. $mail .= "TO:_to . ">\r\n";
  199. $mail .= "Subject:" . $this->_subject ."\r\n\r\n";
  200. $mail .= $this->_body . "\r\n.\r\n";
  201. }
  202. $command = array(
  203. array("HELO sendmail\r\n", 250),
  204. array("AUTH LOGIN\r\n", 334),
  205. array(base64_encode($this->_userName) . "\r\n", 334),
  206. array(base64_encode($this->_password) . "\r\n", 235),
  207. array("MAIL FROM:_from . ">\r\n", 250),
  208. array("RCPT TO:_to . ">\r\n", 250),
  209. array("DATA\r\n", 354),
  210. array($mail, 250),
  211. array("QUIT\r\n", 221)
  212. );
  213. return $command;
  214. }
  215. /**
  216. * @access protected
  217. * @param string $command 发送到服务器的smtp命令
  218. * @param int $code 期望服务器返回的响应吗
  219. * @param boolean
  220. */
  221. protected function sendCommand($command, $code) {
  222. echo 'Send command:' . $command . ',expected code:' . $code . '
    ';
  223. //发送命令给服务器
  224. try{
  225. if(socket_write($this->_socket, $command, strlen($command))){
  226. //读取服务器返回
  227. $data = trim(socket_read($this->_socket, 1024));
  228. echo 'response:' . $data . '

    ';
  229. if($data) {
  230. $pattern = "/^".$code."/";
  231. if(preg_match($pattern, $data)) {
  232. return true;
  233. }
  234. else{
  235. $this->_errorMessage = "Error:" . $data . "|**| command:";
  236. return false;
  237. }
  238. }
  239. else{
  240. $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
  241. return false;
  242. }
  243. }
  244. else{
  245. $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
  246. return false;
  247. }
  248. }catch(Exception $e) {
  249. $this->_errorMessage = "Error:" . $e->getMessage();
  250. }
  251. }
  252. /**
  253. * 建立到服务器的网络连接
  254. * @access private
  255. * @return boolean
  256. */
  257. private function socket() {
  258. if(!function_exists("socket_create")) {
  259. $this->_errorMessage = "extension php-sockets must be enabled";
  260. return false;
  261. }
  262. //创建socket资源
  263. $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  264. if(!$this->_socket) {
  265. $this->_errorMessage = socket_strerror(socket_last_error());
  266. return false;
  267. }
  268. //连接服务器
  269. if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
  270. $this->_errorMessage = socket_strerror(socket_last_error());
  271. return false;
  272. }
  273. socket_read($this->_socket, 1024);
  274. return true;
  275. }
  276. /**
  277. * 关闭socket
  278. * @access private
  279. * @return boolean
  280. */
  281. private function close() {
  282. if(isset($this->_socket) && is_object($this->_socket)) {
  283. $this->_socket->close();
  284. return true;
  285. }
  286. $this->_errorMessage = "no resource can to be close";
  287. return false;
  288. }
  289. }
  290. /**************************** Test ***********************************/
  291. $config = array(
  292. "from" => "********@163.com",
  293. "to" => "******@163.com",
  294. "subject" => "test",
  295. "body" => "test",
  296. "username" => "******",
  297. "password" => "password",
  298. );
  299. $mail = new MySendMail();
  300. $mail->setServer("smtp.163.com");
  301. $mail->setMailInfo($config);
  302. if(!$mail->sendMail()){
  303. echo $mail->error();
  304. return 1;
  305. }
复制代码

PHP, HTML


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP与Python:了解差异PHP与Python:了解差异Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

php:死亡还是简单地适应?php:死亡还是简单地适应?Apr 11, 2025 am 12:13 AM

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来:改编和创新PHP的未来:改编和创新Apr 11, 2025 am 12:01 AM

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

您什么时候使用特质与PHP中的抽象类或接口?您什么时候使用特质与PHP中的抽象类或接口?Apr 10, 2025 am 09:39 AM

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

什么是依赖性注入容器(DIC),为什么在PHP中使用一个?什么是依赖性注入容器(DIC),为什么在PHP中使用一个?Apr 10, 2025 am 09:38 AM

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

与常规PHP阵列相比,解释SPL SplfixedArray及其性能特征。与常规PHP阵列相比,解释SPL SplfixedArray及其性能特征。Apr 10, 2025 am 09:37 AM

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP如何安全地上载文件?PHP如何安全地上载文件?Apr 10, 2025 am 09:37 AM

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

什么是无效的合并操作员(??)和无效分配运算符(?? =)?什么是无效的合并操作员(??)和无效分配运算符(?? =)?Apr 10, 2025 am 09:33 AM

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!