php 中引入邮箱服务 , 利用第三方的smtp邮件服务

项目中用短信通知有时间限制,对一些频率比较大的信息力不从心. 使用邮箱发送信息是个不错的选择\(^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 = "[email protected]";//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("/^[email protected]([^@]+)$/", "\\1", $address);
if ([email protected]($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 ([email protected]_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
?>

时间: 2024-10-10 10:18:20

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

杨泽业:给你的wordpress博客添加SMTP邮件服务,评论以后邮件通知

当你的博客有人评论的时候,就会收到邮件通知,那是非常好的体验,有人评论了自己第一时间知晓.特别是,有人在你的博客留言,需要有人解答问题的时候,这个人是迫切的需要及时通知的,有的评论自动邮件通知的话,那么就轻松了. 要实现评论邮件通知,还是不难的.下面,我就说说,使用腾讯的免费企业邮箱配置SMTP邮件服务.(没有企业邮箱的请查看我上一个文章<用你自己的域名免费的开通腾讯企业邮箱图文教程>) 先在模板函数 (functions.php)文件底部添加下面代码即可,邮箱的帐号密码改成自己的帐号密码.

Java中解析HTML数据 (利用第三方库Jsoup)

需求分析: 在为网页服务提取API时需要解析页面中的信息 项目地址: https://github.com/hwding/LibXDUQuery 准备工作: 下载第三方库Jsoup(一款非常优秀的HTML Parser): https://jsoup.org/download 阅读Jsoup API Reference: https://jsoup.org/apidocs/ 查阅相关代码 了解JAVA解析XML的方式(具有异曲同工之妙): http://www.cnblogs.com/hwdin

【初学菜鸟作--邮件服务的简单配置案例】

邮件服务器的配置以及使用 实验一:                    实验目的:简单搭建出邮件服务器并测试其可用性                    实验环境:DNS服务器一台,安装有Portfix的邮件服务器一台 实验步骤: 一.邮件的发送(SMTP) 1.在邮件服务器配置主机名,ip,并安装portfix并启动 [[email protected]~]# tail -2 /etc/sysconfig/network HOSTNAME=mail.tarena.com   [[email

springboot(十):邮件服务

springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了.准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5.3进行开发. 发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息.最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring退出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的sp

spring-boot(六) 邮件服务

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

(转)Springboot邮件服务

springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了.准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5.3进行开发. 发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息.最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring推出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的sp

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

java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,但是使用java编写邮件服务端: 一.邮件客户端: web应用根据依赖的API,常用的有两种: 第一种:J2EE中提供的java mail API(javax.mail.*) Javamail API是一个用于阅读.编写和发送电子消息的可选包(标准扩展),可以用来建立基于标准的电子邮件客户机,它支持各种因特网邮件协议,包括:SMTP.POP.IMAP.MIME.NNTP.S/MIME及其它协议. 第二种:spring 对

Linux 邮件服务搭建

Linux 邮件服务搭建 邮件服务针对,在大型企业使用的比较多,一般小型企业都会买一些邮件服务,或者使用一些免费的邮件服务,达到我们使用的需求,并且不需要自己维护,下面我就来简单安装一下两个邮箱的案例,方便我们去理解. 邮件方案 1.免费方案,免费邮件企业邮箱 有限制- 最多50人等··· 2.付费企业邮箱 3.搭建的邮箱 邮件相关知识了解 发邮件协议:smtp 25端口 发邮件服务:postfix 服务 收邮件协议:pop pop3 110端口.imap 143端口 收邮件服务:dovecot

邮件服务的修真之旅

一.筑基期之邮件服务简介 邮件服务通过以下几个协议来实现简单邮件传输协议(SMTP),用来发送或中转发出的电子邮件,占用tcp 25端口.邮局协议 第三版(POP3),用于把服务器上的邮件存储到本地主机,占用tcp 110端口.互联网信息访问协议 第四版(IMAP4),用于在本地主机上访问邮件,占用tcp 143端口.了解了三种协议,现在再来看看邮件服务的基本概念邮件传输:MT 邮件传输代理:MTA邮件投递:MD 邮件投递代理:MDA邮件用户:MU 邮件用户代理:MUA在一台邮件服务器上,用户要