php使用smtp.sina.com邮箱发送邮件

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

需要写一个smtp发送类

<?php
set_time_limit(120);class smtp
{/* Public Variables */var $smtp_port;var $time_out;var $host_name;var $log_file;var $relay_host;var $debug;var $auth;var $user;var $pass;var $sender;/* Private Variables */var $sock;/* Constractor */function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass,$sender){$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->sender = $sender;$this->host_name = "localhost"; //is used in HELO command$this->log_file = "";$this->logs = ""; //记录跟服务器的交互过程$this->sock = FALSE;}/* Main Function */function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){$sent = TRUE;$mail_from = $this->get_address($this->strip_comment($from));$body = 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 .= "To: ".$to."\r\n";if ($cc != "") {$header .= "Cc: ".$cc."\r\n";}//$header .= "From: $from<".$from.">\r\n";$header .= "From: ".$from."\r\n";$header .= "Subject: ".$subject."\r\n";$header .= $additional_headers;$header .= "Date: ".date("r")."\r\n";$header .= "X-Mailer: 72e.net (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)));}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");}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 AUTH command");}if (!$this->smtp_putcmd("", base64_encode($this->pass))) {return $this->smtp_error("sending AUTH command");}}#//if (!$this->smtp_putcmd("MAIL", "FROM:".$from."")) {if (!$this->smtp_putcmd("MAIL", "FROM:<".$this->sender.">")) {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));//echo "response=".$response."\r\n";$this->smtp_debug($response."\n");//echo "ereg 23 ==".ereg("^[23]", $response)."\n";if (!ereg("^[23]", $response)) {//echo "@@@@@";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");//echo "cmd=".$cmd."\r\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->logs .= $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 (ereg($comment, $address)) {$address = ereg_replace($comment, "", $address);}return $address;}function get_address($address){$address = ereg_replace("([ \t\r\n])+", "", $address);$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);return $address;}function smtp_debug($message){if ($this->debug) {echo $message;}}} // end class
?>

测试sina邮箱可否使用

<?php
include "smtp.php";$smtpserver = "smtp.sina.com";//您的smtp服务器的地址
$port = 25; //smtp服务器的端口,一般是 25
$smtpuser = "your-mail@sina.com"; //您登录smtp服务器的用户名
$smtppwd = "your-password"; //您登录smtp服务器的密码
$mailtype = "HTML"; //邮件的类型,可选值是 TXT 或 HTML ,TXT 表示是纯文本的邮件,HTML 表示是 html格式的邮件
$sender = "your-mail@sina.com"; //发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,否则可能会因为smtp服务器的设置导致发送失败
$smtp  =   new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
$smtp->debug = true; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释
$to = "bubifengyun@sina.com"; //收件人
$subject = "你好";
$body = "<h1>这是一个用 <font color='red'><b> php socket </b></font> 发邮件的测试。支持SMTP认证!</h1>";
$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
if($send==1){echo "邮件发送成功";
}else{echo "邮件发送失败<br>";echo "原因:".$this->smtp->logs;
}
?>

使用

php mailtest.php

测试可用。感谢原作者分享。

转载于:https://my.oschina.net/bubifengyun/blog/544578

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

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

相关文章

java 不存在就创建_Java 判断多级路径是否存在,不存在就创建

Java 判断多级路径是否存在&#xff0c;不存在就创建方案一&#xff1a;(带文件名的Path&#xff1a;如&#xff1a;D:\news\2014\12\abc.text)public boolean isexitsPath(String path)throws InterruptedException{String [] pathspath.split("\\\\");StringBuffer…

asynchttpclient 超时_DNF:95更新前还能免费获得一件超时空装备?但这个任务一定完成...

现在距离95版本更新还有16天的时间&#xff0c;在前段时间体验服更新的内容中&#xff0c;相信玩家们已经对新版本了解的差不多了。最受玩家关注的就是装备升级方面的一些内容&#xff0c;都说95版本是一个土豪专属副本&#xff0c;但是小编并不那么认为&#xff01;从材料上来…

sqllite开发安卓项目_【兼职项目】预算3万开发无线温度电流传感,2万开发直流电机打磨机控制...

【个人/团队兼职项目】是小包为大家筛选的酬金预算≤3万元的中小型软硬件兼职项目&#xff0c;适合高级工程师、技术团队服务商竞标&#xff0c;从而赚取“零花钱”。个人/团队兼职项目&#xff08;酬金≤3万元&#xff09;1、摄像头在安卓开发板上成像项目预算&#xff1a;&am…

java还是c 2017_2017年9月编程语言排行榜:Java、C与C++三巨头还能统治排行榜多久?...

【51CTO.com快译】过去16年来(自2001年起)&#xff0c;TIOBE编程语言排行榜的三甲位置一直被Java、C与C长期占据。C#与Python虽然已经被广泛认定为下一世代的核心编程语言&#xff0c;但在人气方面却仍然未得到充分体现。C#之所以无法进军三甲&#xff0c;主要是因为其在非Wind…

excel换行按什么键_电脑结束任务按什么键

如今&#xff0c;电脑在我们日常生活中几乎无处不在&#xff0c;然而有时候可能我们在使用电脑的过程中会出现这样或者那样的小问题&#xff0c;比如有时候电脑打开的软件太多&#xff0c;导致电脑有点卡机&#xff0c;我们想要关闭一些程序却无法快速关闭&#xff0c;最常见的…

npoi 导入 winform excel_勤哲Excel服务器做影视制作企业管理系统 - 科技

在人们心中&#xff0c;影视作品的生产是一个系统化的工程&#xff0c;从剧本选择、班底选择&#xff0c;到拍摄和发行&#xff0c;每个环节的信息化和大数据应用&#xff0c;正在释放越来越大的能量。在行业专家们看来&#xff0c;曾经国内影视行业的工业化程度较低&#xff0…

关于 Android 和 iOS 流畅度的一切

2019独角兽企业重金招聘Python工程师标准>>> 之前发表了《论苹果公司的系统拖累策略》&#xff0c;在看了锋友的回复之后&#xff0c;发现无论大家是赞成或者反对&#xff0c;可能都对Android和iOS的认识上存在着一些盲区和误区&#xff0c;于是答应转载这篇专业分析…

Extjs4 MVC 添加view层

如果这不是您感兴趣或者需要学习的东西&#xff0c;何必点开呢&#xff1f;人生苦短。实例中我们通过view层望主界面中添加一个grid&#xff0c; 1、app.js改成如下&#xff1a; app.js 1 Ext.Loader.setConfig({enabled:true});//开启动态加载2 Ext.application({3 name: …

剪映电脑版_2020 年双十一要不要选一个平板电脑?

其实大路很久以前就一直在关注平板电脑&#xff0c;我记得前几年平板电脑有好多厂家都在做&#xff0c;比如小米的红米平板&#xff0c;1000元左右&#xff0c;性能像手机一样&#xff0c;非常香。华为也有很多&#xff0c;但是今年我突然想买的时候&#xff0c;尼玛停产的停产…

Qt之自定义搜索框

简述 关于搜索框&#xff0c;大家都经常接触。例如&#xff1a;浏览器搜索、Windows资源管理器搜索等。 当然&#xff0c;这些对于Qt实现来说毫无压力&#xff0c;只要思路清晰&#xff0c;分分钟搞定。 方案一&#xff1a;调用QLineEdit现有接口 void addAction(QAction * act…

跟我学Windows7的33个技巧(二)

18. 自定义电源开关 默认情况下&#xff0c;Windows 7在开始菜单处将以文本形式显示关机按钮&#xff0c;但是你只需要几秒钟就可以将这种默认的方式改变。如果每天你都需要重启电脑许多次&#xff0c;那么这样的改变比默认的方式更加有趣&#xff0c;右击开始菜单选择属性“Pr…

android gps 锁屏更新坐标_把手机锁屏设置成任意字体,悄悄给男(女)朋友一个惊喜吧...

有好软件就要与大家分享&#xff0c;我是阿喵&#xff0c;阿喵每天会定时推送出安卓手机、苹果手机、电脑相关、学习等优质软件&#xff0c;如果各位觉得软件不错&#xff0c;就请转发评论&#xff0c;留言需要什么样功能的&#xff0c;留言需要什么样功能的&#xff0c;阿喵定…

实验一:SQL server 2005高可用性之----日志传送

如转载&#xff0c;请注明出处&#xff1a;http://blog.csdn.net/robinson_0612/archive/2009/10/31/4751070.aspx SQL server 2005高可用性之日志传送是在SQL server 2000 日志传送基础之上的延续&#xff0c;两者其本质上并没有太大的差异。日志传送能够同步位于不同服务器或…

反向代理服务器

一 反向代理服务器简介      反向代理&#xff08;Reverse Proxy&#xff09;方式是指以代理服务器来接受Internet上的连接请求&#xff0c;然后将请求转发给内部网络上的服务器&#xff1b;并将从服务器上得到的结果返回给Internet上请求连接的客户端&#xff0c;此时代…

location 拦截所有_让它帮你干掉所有流氓软件,还你一个清静系统

最近大家应该都要复工了吧&#xff01;我们又到了捧着电脑跑来跑去的日子&#xff0c;不过相比在家&#xff0c;复工还是蛮激动的。今天队长聊一下电脑软件。电脑这种东西&#xff0c;现在虽算不上是生命之源&#xff0c;但是复工上班工作或者交作业的时候那可真是当祖宗供着的…

grub4dos和winsetupfromusb1.4

其实grub4dos也是一个多系统启动盘制作软件&#xff0c;GRUB4DOS 最大的成功之处就是既学习了windows的方便易用&#xff0c;又引入linux的强大功能。http://baike.baidu.com/link?urlvFkYDEOTmdnw0dLueYeNz0V48egdnjpHczS3YwWiSsXtM7krYaE6KiDohCT4hlo5-LihtQU7fYhXuURazx_OK…

一份完整的问卷模板_一份完整市场推广策划方案模板

完整的市场方案主要分为以下几大板块&#xff0c;在策略撰写的过程中&#xff0c;依据策略优先级按照以下三个步骤划分&#xff0c;前后承接。为避免无用功&#xff0c;尽量确定前一部分后&#xff0c;再开始后一部分&#xff01;方案目录&#xff0c;记住这个&#xff01;整个…

Application是什么

2019独角兽企业重金招聘Python工程师标准>>> 1:Application是什么&#xff1f; Application和Activity,Service一样,是android框架的一个系统组件&#xff0c;当android程序启动时系统会创建一个 application对象&#xff0c;用来存储系统的一些信息。通常我们是不需…

linux mysql udf打包_Linux下MySQL 5.1编写UDF 并运行(Install)获取当前时间的毫秒数...

Mysql 无法获取当前时间的毫秒数自行定制UDF&#xff0c;以提供current_ms方法1. 编写 C 文件#ifdef STANDARD#include lt;stdio.hgt;#incMysql 无法获取当前时间的毫秒数自行定制UDF&#xff0c;以提供current_ms方法1. 编写 C 文件#ifdef STANDARD#include #include #ifdef _…

Daily Scrum 2012/11/08

TeamSH-IT 今天经完成了之前在数据定义存在一些问题。sui老师经过小组之间的交流&#xff0c;完成了对数据定义的最终版。周末将进行初步的整合和测试。 详细的完成情况&#xff1a; 组员今天任务明天任务Hu Renjun 任务213 整合各个模块功能 完成完整的数据流过程 任务213 …