项目中用短信通知有时间限制,对一些频率比较大的信息力不从心. 使用邮箱发送信息是个不错的选择\(^o^)/!

首先要注册一个邮箱,在邮箱设置里开通smtp功能.

  简单介绍下smtp,大概就是第三方客户端登录邮箱的功能.打开smtp功能,我们的项目里的邮箱类就可以充当邮箱客户端使用,从而发送信息了.(但要注意,密码将不是邮箱的登录密码.而是邮箱生成授权码或者自己设置的授权码)

下面是干货

<?
class SmtpAction extends Action
{
/* Public Variables */
public $debug = false; //是否开启调试 会打印每一步的执行结果
public $smtp_port = 25;
public $time_out = 30;
public $host_name = "localhost";
public $log_file = '';
public $relay_host = "smtp.163.com";//SMTP服务器
public $auth = true; //使用身份验证,否则不使用身份验证.
public $user = "23213213@163.com";//SMTP服务器的用户帐号
public $pass = "32132132132";//SMTP服务器的用户密码

/* Private Variables */
private $sock = FALSE;

function index($to='',$name='',$body='') {
$to = I('to') ? I('to') : $to; //目标邮箱
$form = $this->user; //发件人
$name = I('name')? I('name') : $name; //标题
$body = I('body')? I('body') : $body; //正文
$mailtype = 'html'; //正文类型 html/txt
$this->sendmail($to,$form,$name,$body,$mailtype);

}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body);
$header = '';
$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 .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (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");
}
#auth
if($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 = preg_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 (!preg_match("/^[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 (preg_match($comment, $address)) {
$address = preg_replace($comment, "", $address);
}

return $address;
}

function get_address($address)
{
$address = preg_replace("/([ \t\r\n])+/", "", $address);
$address = preg_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;
}

} // end class
?>

php 中引入邮箱服务 , 利用第三方的smtp邮件服务的更多相关文章

  1. 免费SMTP邮件服务:Mandrill,Sendgrid,Mailjet,Postmarkapp,MailChimp

    免费的SMTP发邮件服务平常几乎都没有怎么关注,Wordpress发邮件几乎用普通的邮箱的SMTP服务就可以完成任务,但是自从用了Discourse.Ghost博客后,发现找到一个合适的.免费配额大的 ...

  2. C#接入SMTP邮件服务

    我的个人博客 引入SMTP服务包 //引入邮件服务包using System.Net.Mail;using System.Net; 这两个引用用于C#接入邮件的SMTP服务 控件页面 定时器 为了给用 ...

  3. 工程师技术(二):postfix基础邮件服务、postfix空客户端邮件服务、搭建mariadb数据库系统、配置一个数据库、使用数据库查询

    一.postfix基础邮件服务 目标: 本例要求在虚拟机server0上配置 postfix 基础服务,具体要求如下: 1> 监听本机的所有接口    2> 将邮件域和邮件服务主机名都改为 ...

  4. Java邮件服务学习之一:邮件服务概述

    java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...

  5. springboot(十):邮件服务

    springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了.准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5. ...

  6. Spring Boot (十):邮件服务

    Spring Boot 仍然在狂速发展,才几个多月没有关注,现在看官网已经到 2.1.0.RELEASE 版本了.准备慢慢在写写 Spring Boot 相关的文章,本篇文章使用 Spring Boo ...

  7. Spring Boot :邮件服务

    简单使用 1.pom 包配置 pom 包里面添加 spring-boot-starter-mail 包引用 <dependencies> <dependency> <gr ...

  8. spring-boot(六) 邮件服务

    学习文章来自:springboot(十):邮件服务 简单使用 1.pom包配置 pom包里面添加spring-boot-starter-mail包引用 <dependencies> < ...

  9. (转)Spring Boot (十):邮件服务

    http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html Spring Boot 仍然在狂速发展,才几个多月没有关注,现 ...

随机推荐

  1. Tensorflow原理通用

    使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务.在被称之为 会话 (Session) 的上下文 (context) 中执行图.使用 tenso ...

  2. idea debug打得断点第一次可以进入,第二次不能进入

  3. Oracle解除表锁定问题

    1.肯定是你同时打开了多个操作页面,要记得关闭多个打开的sql窗口. 2.可以变相删除表,再重新创建一张同名的表来解除表被锁住的问题

  4. java之 ------ 设计思想

    java的设计思想 (设计思想.是须要不断领悟的.. . ) 一.封装 学java的人都知道这是向对象的编程语言,从字面上理解,就是针对对象的一些操作,将具有某一特性的实体封装成一个类或者是将具有一定 ...

  5. 《Java虚拟机原理图解》 1.2.3、Class文件里的常量池具体解释(下)

    NO9.类中引用到的field字段在常量池中是如何描写叙述的?(CONSTANT_Fieldref_info, CONSTANT_Name_Type_info) 一般而言.我们在定义类的过程中会定义一 ...

  6. cocos2d 3.3 lua 代码加密 luac

    1.0 cocos luacompile 使用方法 我用的普通的cocos2d lua,没用quick,quick好像能够对整个资源包含图像和音频都加密,打包成zip.我看了下luacompile 的 ...

  7. php5.5安装笔记

    这次没想到本来很简单的php编译,没想到遇到那么多问题.再此记录一下. 1.php5.5编译安装主要有一个难点,就是GD库的问题,因为php5.5的GD库必须是2.1以上的版本哦 原来都是用的gd2. ...

  8. 关于1-n任意的gcd的和

    gcd和 题目 GCD sum 公约数的和 大意是让你求1-n任意两个数的gcd的和之类的. 解法 显然你需要枚举对吧,不然你怎么可能求出gcd呢? 其次我们需要一些数学推理 令F(n)表示\(\su ...

  9. bootstrap在input框中加入icon图标

    <form class="form-horizontal"> <div class="form-group has-feedback"> ...

  10. [C#学习笔记之异步编程模式2]BeginInvoke和EndInvoke方法 (转载)

    为什么要进行异步回调?众所周知,普通方法运行,是单线程的,如果中途有大型操作(如:读取大文件,大批量操作数据库,网络传输等),都会导致方法阻塞,表现在界面上就是,程序卡或者死掉,界面元素不动了,不响应 ...