PHP使用SMTP邮件服务器

https://blog.csdn.net/qq_20343517/article/details/77453666

 

用之前记得先去163注册一个邮箱,然后打开SMTP服务,当然也可以使用QQ邮箱等,但配置信息得改。

话不多说,直接上代码

email.class.php  定义发送邮件的库

 

<?php
class smtp
{/* Public Variables */var $smtp_port; //smtp_port 端口号var $time_out;var $host_name; //服务器主机名var $log_file;var $relay_host; //服务器主机地址var $debug;var $auth; //验证var $user; //服务器用户名var $pass; //服务器密码/* Private Variables */var $sock;/* Constractor 构造方法*/function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass){$this->debug      = FALSE;$this->smtp_port  = $smtp_port;$this->relay_host = $relay_host;$this->time_out   = 30; //is used in fsockopen()#$this->auth       = $auth; //auth$this->user       = $user;$this->pass       = $pass;#$this->host_name  = "localhost"; //is used in HELO command// $this->host_name = "smtp.163.com"; //is used in HELO command$this->log_file   = "";$this->sock = FALSE;}/* Main Function */function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){$header    = "";$mail_from = $this->get_address($this->strip_comment($from));$body      = mb_ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);$header .= "MIME-Version:1.0\r\n";if ($mailtype == "HTML") { //邮件发送类型//$header .= "Content-Type:text/html\r\n";$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";}$header .= "To: " . $to . "\r\n";if ($cc != "") {$header .= "Cc: " . $cc . "\r\n";}$header .= "From: " . $from . "\r\n";// $header .= "From: $from<".$from.">\r\n";   //这里只显示邮箱地址,不够人性化$header .= "Subject: " . $subject . "\r\n";$header .= $additional_headers;$header .= "Date: " . date("r") . "\r\n";$header .= "X-Mailer:By (PHP/" . phpversion() . ")\r\n";list($msec, $sec) = explode(" ", microtime());$header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";$TO = explode(",", $this->strip_comment($to));if ($cc != "") {$TO = array_merge($TO, explode(",", $this->strip_comment($cc))); //合并一个或多个数组}if ($bcc != "") {$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));}$sent = TRUE;foreach ($TO as $rcpt_to) {$rcpt_to = $this->get_address($rcpt_to);if (!$this->smtp_sockopen($rcpt_to)) {$this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");$sent = FALSE;continue;}if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {$this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");} else {$this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");$sent = FALSE;}fclose($this->sock);$this->log_write("Disconnected from remote host\n");}echo "<br>";//echo $header;return $sent;}/* Private Functions */function smtp_send($helo, $from, $to, $header, $body = ""){if (!$this->smtp_putcmd("HELO", $helo)) {return $this->smtp_error("sending HELO command");}#authif ($this->auth) {if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {return $this->smtp_error("sending HELO command");}if (!$this->smtp_putcmd("", base64_encode($this->pass))) {return $this->smtp_error("sending HELO command");}}#if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {return $this->smtp_error("sending MAIL FROM command");}if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {return $this->smtp_error("sending RCPT TO command");}if (!$this->smtp_putcmd("DATA")) {return $this->smtp_error("sending DATA command");}if (!$this->smtp_message($header, $body)) {return $this->smtp_error("sending message");}if (!$this->smtp_eom()) {return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");}if (!$this->smtp_putcmd("QUIT")) {return $this->smtp_error("sending QUIT command");}return TRUE;}function smtp_sockopen($address){if ($this->relay_host == "") {return $this->smtp_sockopen_mx($address);} else {return $this->smtp_sockopen_relay();}}function smtp_sockopen_relay(){$this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);if (!($this->sock && $this->smtp_ok())) {$this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");$this->log_write("Error: " . $errstr . " (" . $errno . ")\n");return FALSE;}$this->log_write("Connected to relay host " . $this->relay_host . "\n");return TRUE;;}function smtp_sockopen_mx($address){$domain = ereg_replace("^.+@([^@]+)$", "\\1", $address);if (!@getmxrr($domain, $MXHOSTS)) {$this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");return FALSE;}foreach ($MXHOSTS as $host) {$this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);if (!($this->sock && $this->smtp_ok())) {$this->log_write("Warning: Cannot connect to mx host " . $host . "\n");$this->log_write("Error: " . $errstr . " (" . $errno . ")\n");continue;}$this->log_write("Connected to mx host " . $host . "\n");return TRUE;}$this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");return FALSE;}function smtp_message($header, $body){fputs($this->sock, $header . "\r\n" . $body);$this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));return TRUE;}function smtp_eom(){fputs($this->sock, "\r\n.\r\n");$this->smtp_debug(". [EOM]\n");return $this->smtp_ok();}function smtp_ok(){$response = str_replace("\r\n", "", fgets($this->sock, 512));$this->smtp_debug($response . "\n");if (!mb_ereg("^[23]", $response)) {fputs($this->sock, "QUIT\r\n");fgets($this->sock, 512);$this->log_write("Error: Remote host returned \"" . $response . "\"\n");return FALSE;}return TRUE;}function smtp_putcmd($cmd, $arg = ""){if ($arg != "") {if ($cmd == "")$cmd = $arg;else$cmd = $cmd . " " . $arg;}fputs($this->sock, $cmd . "\r\n");$this->smtp_debug("> " . $cmd . "\n");return $this->smtp_ok();}function smtp_error($string){$this->log_write("Error: Error occurred while " . $string . ".\n");return FALSE;}function log_write($message){$this->smtp_debug($message);if ($this->log_file == "") {return TRUE;}$message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {$this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");return FALSE;}flock($fp, LOCK_EX);fputs($fp, $message);fclose($fp);return TRUE;}function strip_comment($address){$comment = "\\([^()]*\\)";while (mb_ereg($comment, $address)) {$address = mb_ereg_replace($comment, "", $address);}return $address;}function get_address($address){$address = mb_ereg_replace("([ \t\r\n])+", "", $address);$address = mb_ereg_replace("^.*<(.+)>.*$", "\\1", $address);return $address;}function smtp_debug($message){if ($this->debug) {echo $message . "<br>";}}function get_attach_type($image_tag) //{$filedata = array();$img_file_con = fopen($image_tag, "r");unset($image_data);while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))$image_data .= $tem_buffer;fclose($img_file_con);$filedata['context']  = $image_data;$filedata['filename'] = basename($image_tag);$extension            = substr($image_tag, strrpos($image_tag, "."), strlen($image_tag) - strrpos($image_tag, "."));switch ($extension) {case ".gif":$filedata['type'] = "image/gif";break;case ".gz":$filedata['type'] = "application/x-gzip";break;case ".htm":$filedata['type'] = "text/html";break;case ".html":$filedata['type'] = "text/html";break;case ".jpg":$filedata['type'] = "image/jpeg";break;case ".tar":$filedata['type'] = "application/x-tar";break;case ".txt":$filedata['type'] = "text/plain";break;case ".zip":$filedata['type'] = "application/zip";break;default:$filedata['type'] = "application/octet-stream";break;}return $filedata;}}
?>

 

index.php 发送邮件的具体实现

 

<?php
require 'email.class.php';$mailto='*********@qq.com';  //收件人
$subject="恭喜您开通年费会员成功"; //邮件主题
$body="回复TD退订";  //邮件内容
sendmailto($mailto,$subject,$body);
echo "finish".date('时间:Y年m月d日   H:i');function sendmailto($mailto, $mailsub, $mailbd)
{//require_once ('email.class.php');//##########################################$smtpserver     = "smtp.163.com"; //SMTP服务器$smtpserverport = 25; //SMTP服务器端口$smtpusermail   = "***********@163.com"; //SMTP服务器的用户邮箱$smtpemailto    = $mailto;$smtpuser       = "*******@163.com"; //SMTP服务器的用户帐号$smtppass       = "**********"; //SMTP服务器的用户密码$mailsubject    = $mailsub; //邮件主题$mailsubject    = "=?UTF-8?B?" . base64_encode($mailsubject) . "?="; //防止乱码$mailbody       = $mailbd; //邮件内容//$mailbody = "=?UTF-8?B?".base64_encode($mailbody)."?="; //防止乱码$mailtype       = "HTML"; //邮件格式(HTML/TXT),TXT为文本邮件. 139邮箱的短信提醒要设置为HTML才正常##########################################$smtp           = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.$smtp->debug    = TRUE; //是否显示发送的调试信息$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);}
?>

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399720.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用jquery解析xml

使用Jquery解析XML&#xff1a;$.ajax({ url: ajax/test.xml, dataType : xml, cache: false, success: function(xml) { $("AUTHOR", xml).each(function(id) { AUTHOR $("AUTHOR", xml).get(id); …

cv1159 最大全0子矩阵(极大子矩阵)

题目描述 Description 在一个01方阵中找出其中最大的全0子矩阵&#xff0c;所谓最大是指0的个数最多。 输入描述 Input Description 输入文件第一行为整数N&#xff0c;其中1<N<2000&#xff0c;为方阵的大小&#xff0c;紧接着N行每行均有N个0或1&#xff0c;相邻两数…

Docker认识基础

版权声明&#xff1a;本文为博主chszs的原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/48212081 Docker认识基础 作者&#xff1a;chszs&#xff0c;版权所有&#xff0c;未经同意&#xff0c;不得转载。博主主页&#xff1a;http:…

信管 - 挣值 - 资料收集

信息系统项目管理师计算题之挣值分析、完工预测知识与习题 挣值分析&#xff1a;早期只需要记住三个参数&#xff0c;4个指标以及公式即可。PV、EV、AC、CV、SV、CPI、SPI。但现在没这么简单了&#xff0c;深入考核PV、EV、AC的理解&#xff0c;从一段文字描述中计算出PV、EV、…

獲取mysql字段的注释或描述

show full fields from table 获取mysql的表注释和字段注释 - 杨宇的技术博客 - 博客频道 - CSDN.NET http://blog.csdn.net/yangyu112654374/article/details/5419807转载于:https://www.cnblogs.com/abinlove/p/3559011.html

开源cms - 资料收集

schoolcms opensns http://www.opensns.cn/home/index/download.html 在线 IM 解决方案 QIQI-IM https://www.oschina.net/p/qiqi-im

MVC3 学习总结一(未发布)

MVC3 学习总结一(未发布) MVC: Model,View,Control 设置View中的数据 1. 返回model&#xff0c;View中强类型化 Control: public ActionResult Browse(string Genre) { var Album db.Genres.Include("Albums").Single(c > c.Name Genre); …

NOIP 选择客栈

描述 丽江河边有n家很有特色的客栈&#xff0c;客栈按照其位置顺序从1到n编号。每家客栈都按照某一种色调进行装饰&#xff08;总共k种&#xff0c;用整数0~ k-1表示&#xff09;&#xff0c;且每家客栈都设有一家咖啡店&#xff0c;每家咖啡店均有各自的最低消费。 两位游客一…

PHP-thinkPHP快速入门

https://blog.csdn.net/cgl_zyw/article/details/78045913 1.教程大纲 这是一篇零基础的thinkPHP教程&#xff0c;全篇看完只需要要一个小时&#xff0c;通过一个简单的网站&#xff0c;我会由点到面的讲述 a.基础的讲述thinkPHP的路由、请求、响应、模板的渲染。 b.如何在th…

mybatis中的多对一的查询

多对一也分为单条sql语句和多条sql语句 下面就以员工和就职部门为例&#xff1a; 员工实体类 private Integer empno;private String empname;private Integer deptno;//植入部门实体private Dept dept;public Integer getEmpno() {return empno;}public void setEmpno(Intege…

hdu5424 Rikka with Graph II

给一个n个节点n条边的无向图G&#xff0c;试判断图中是否存在哈密顿路径。 若G中存在哈密顿路径l&#xff0c;则路径端点度数不小于1&#xff0c;其余点度数不小于2。 则G存在哈密顿路径的必要条件&#xff1a; 1&#xff09;G连通&#xff1b; 2&#xff09;G中度数为1的点不超…

VisualStudio中的代码段

VS很强大&#xff0c;在这里就不过多说了&#xff0c;在平时码代码时应用代码段会提高我们的编写速度。 举个例子&#xff1a; 比如输入Console.WriteLine (); 传统方法就是一个字母一个字母的输入进去。 如果大家掌握了代码段&#xff0c;就变得非常简单了。只需要输入cw按两次…

schoolcms - 学习笔记

schoolcms // 版本信息 const THINK_VERSION 3.2.3; ThinkPHP3.2完全开发手册 http://document.thinkphp.cn/manual_3_2.html

tcp和udp的区别和三次 四次挥握手 http://www.cnblogs.com/bizhu/archive/2012/05/12/2497493.html...

小结TCP与UDP的区别&#xff1a;1.基于连接与无连接&#xff1b;2.对系统资源的要求&#xff08;TCP较多&#xff0c;UDP少&#xff09;&#xff1b;3.UDP程序结构较简单&#xff1b;4.流模式与数据报模式 &#xff1b;5.TCP保证数据正确性&#xff0c;UDP可能丢包&#xff0c;…

Windows开发

1. 介绍 这里简单介绍了Windows应用程序开发的基础知识 2. 基础 Windows下的应用程序有控制台程序和Win32窗口程序&#xff0c;这里讲的是Win32窗口程序 Windows提供了相关静态库(LIB)和动态库(DLL)供程序使用 - 控制台 main - Win32 WinMain - DLL DllMain - LIB 无2.1 编…

java concurrent包介绍及使用

2019独角兽企业重金招聘Python工程师标准>>> 说一说java的concurrent包1-concurrent包简介 前面一个系列的文章都在围绕hash展开&#xff0c;今天准备先说下concurrent包&#xff0c;这个系列可能会以使用场景说明为主&#xff0c;concurrent包本身的代码分析可能比…

Codeforces 864E Fire(背包DP)

背包DP&#xff0c;决策的时候记一下 jc[i][j]1 表示第i个物品容量为j的时候要选&#xff0c;输出方案的时候倒推就好了 #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; c…

ThinkPHP3.2.3快速入门 · 看云

https://www.kancloud.cn/thinkphp/thinkphp_quickstart/2138

EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态...

EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态 原文:EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态本文目录 查看实体当前、原始和数据库值&#xff1a;DbEntityEntry查看实体的某个属性值&#xff1a;…

Linux命令与shell

为什么80%的码农都做不了架构师&#xff1f;>>> 资料来自&#xff1a;《http://blog.chinaunix.net/uid-14880649-id-2954340.html》 所谓shell就是命令解释程序。它提供了程序设计接口&#xff0c;可以使用程序来编程。学习shell对于Linux初学者理解Linux系统是非…