Php邮件发送源码
好久冒写点东西了.....最近生活压抑的很....为生活而劳累,整理下邮件发送的实例了,网上也有很多,我这个也是提取整理好的,测试Ok,首页邮件类smtp_email_class.php如下:
<?php
class email
{
function send_mail($to,$subject,$message,$from,$from_name,$mailformat=1)
{
if(function_exists('mail'))
{
$headers = 'From: '.$from_name.'<'.$from.'>'."\r\n";
$headers .= 'TO: '.$to."\r\n";
if($mailformat)
{$headers .="Content-Type: text/html;\r\n";}
else
{$headers .="Content-Type: text/plain;\r\n";}
$headers .="charset=gb2312\r\n\r\n";
$message = str_replace("\r", '', $message);
$mail_return=@mail($to, str_replace("\n",' ',$subject), $message,$headers);
if(!$mail_return)
{
return $to.'发送不成功';
}
return 1;
}
}
function send_win32_mail($to,$subject,$message,$from,$from_name,$host,$port,$mailformat=1)
{
ini_set('SMTP', $host);
ini_set('smtp_port', $port);
ini_set('sendmail_from', $from);
$headers = 'From: '.$from_name.'<'.$from.'>'."\r\n";
$headers .= 'TO: '.$to."\r\n";
if($mailformat)
{$headers .="Content-Type: text/html;\r\n";}
else
{$headers .="Content-Type: text/plain;\r\n";}
$headers .="charset=gb2312\r\n\r\n";
foreach(explode(',', $to) as $touser)
{
$touser = trim($touser);
if($touser)
{
$mail_return=@mail($touser, $subject, $message, $headers);
if(!$mail_return)
{
return $touser.'发送不成功';
}
}
}
return 1;
}
//通过sock发送e_mail,不支持附件,
//-------------------------------------------------------------------------------------------------------
function email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from)//构造函数
{
$this->host = $host;
$this->port = $port;
$this->errno = $errno;
$this->errstr = $errstr;
$this->timeout = $timeout;
$this->auth = $auth;
$this->user = $user;
$this->pass = $pass;
$this->from = $from;
}
function send_mail_sock($subject,$message,$to,$from_name,$mailformat=0)//邮件标题,邮件内容,收件地址,邮件格式1=text|0=html,默认为0
{
$host = $this->host;
$port = $this->port;
$errno = $this->errno;
$errstr = $this->errstr;
$timeout = $this->timeout;
$auth = $this->auth;
$user = $this->user;
$pass = $this->pass;
$from = $this->from;
/*
1.创建sock,并打开连接
2.设置为阻塞模式
3.测试smtp应答码是否为220,220代表邮件服务就绪
4.发送用户身份验证,由用户设置
1=EHLO Host Domain \r\n
0=HELO Host Domain \r\n
?.读取服务器端发送给客户端的返回数据
smtp.163.com 发送的数据为:
250-PIPELINING//流水命令,告诉客户端可以一次发送多个命令来提高速度,在这里PHP
并没有使用,因为PHP单个文件的运行还是单线程的
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME//得到这一行也就是smtp服务器发送结束了,等待客户端发送命令
5.发送AUTH LOGIN命令
6.发送用户名
7.发送密码
?.身份验证过成功后后,
8.向服务器添加from
9.向服务器添加to
10.发送DATA命令,开始输入email数据,以"."号结束
11.书写邮件内容
12.将邮件内容发送到smtp服务器
13.发送QUIT命令,结束会话
*/
$fp = fsockopen($host,$port,$errno,$errstr,$timeout);//打开sock的网络连接
if(!$fp){return '1.没有设置好smtp服务';}
stream_set_blocking($fp, true);//设置为阻塞模式,此模式读不到数据则会停止在那
$mail_return=fgets($fp, 512);//读取512字节内容
if(substr($mail_return, 0, 3) != '220')
{return $host.'-2.返回应答码为'.substr($mail_return, 0, 3);}//返回应答码所代表意思请参考'smtp协议.txt'
fputs($fp, ($auth ? 'EHLO' : 'HELO')." ".$host."\r\n");//服务器标识用户身份 1=身份验证的标识,0=不需要身份验证的标识
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 220 && substr($mail_return, 0, 3) != 250)
{return $host.'-3.返回应答码为'.substr($mail_return, 0, 3);}
while(true)
{
$mail_return = fgets($fp, 512);
if(substr($mail_return, 3, 1) != '-' || empty($mail_return))
{break;}
}
if($auth)
{
fputs($fp, "AUTH LOGIN\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 334)
{return $host.'-5.返回应答码为'.substr($mail_return, 0, 3);}
fputs($fp, base64_encode($user)."\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 334)
{return $host.'-6.返回应答码为'.substr($mail_return, 0, 3).'user='.$user;}
fputs($fp, base64_encode($pass)."\r\n");
$mail_return=fgets($fp, 512);
if(substr($mail_return, 0, 3) != 235)
{return $host.'-7.用户验证失败,应答码为'.substr($mail_return, 0, 3);}
}
//向服务器添加FROM and TO
//------------------------------------------------------------------------------------------------------------------------
fputs($fp, "MAIL FROM: ".$from."\r\n");//有两种格式,MAIL FROM:xxx@xx.com和MAIL FROM: <xxx@xx.com>
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{
fputs($fp, "MAIL FROM: <".$from.">\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{return $host.'-8.返回应答码为'.substr($mail_return, 0, 3);}
}
foreach(explode(',', $to) as $mailto)
{
$mailto = trim($mailto);
if($mailto)
{
fputs($fp, "RCPT TO: ".$mailto."\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{
fputs($fp, "RCPT TO: <".$mailto.">\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{return $host.'-9.返回应答码为'.substr($mail_return, 0, 3);}
}
}
}
//------------------------------------------------------------------------------------------------------------------------
fputs($fp, "DATA\r\n");//开始输入email数据,以"."号结束
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 354)
{return $host.'-10.返回应答码为'.substr($mail_return, 0, 3);}
//邮件内容
//-----------------------------------------------------------
$mail_message = "From:".$from_name.'<'.$from.">\r\n";
$mail_message .= "To:".$to."\r\n";
$mail_message .= "Subject:".str_replace("\n",' ',$subject)."\r\n";
if($mailformat==1)
{$mail_message .= "Content-Type: text/html;\r\n"; }
else
{$mail_message .= "Content-Type: text/plain;\r\n";}
$mail_message .= "charset=gb2312\r\n\r\n";
$mail_message .= $message;
$mail_message .= "\r\n.\r\n";
//-----------------------------------------------------------
fputs($fp,$mail_message);
fputs($fp,"QUIT\r\n");
return 1;
}
}
?>
使用方法,新建send.php,源码如下
<?php
//*
include('smtp_email_class.php');
//定义参数------------------------------------------------------
$host = 'smtp.163.com';//smtp服务器地址,我这里又能够的是163的,视情况而定
$from = 'fantasy_lxh@163.com';//自己的邮件地址
$port = 25;//端口
$errno = 0;//错误返回号
$errstr = '';//错误返回内容
$timeout = 10;//系统运行超时
$auth = 1;//是否需要 AUTH LOGIN 验证, 1=是, 0=否
$user = '你的邮件用户名';//smtp服务器用户名
$pass = '你的邮件密码';//smtp服务器密码
$from_name='你的大名';//联系人名称
//---------------------------------------------------------------
$send_mode=2;
$em=new email();//使用类
if($send_mode==1){
//使用PHP自己支持的mail函数发送邮件,发送邮件需要smtp服务支持
if($em->send_mail('发送给某人(张三)的邮件地址','标题',"内容",'fantasy_lxh@163.com','自己的名称/联系人名称',0)){
echo("邮件发送成功....");
}
}elseif($send_mode==2){
$em->email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from);
if($em->send_mail_sock('标题','欢迎和谐','发送给某人(张三)的邮件地址','你的大名',0)){
//$em->send_mail_sock('标题','内容','koiykoiy@gmail.com','联系人名称',0)
echo("邮件发送成功Ok....");
}
}
?>
Php邮件发送源码的更多相关文章
- Python邮件发送源码
-- coding:utf-8 -- i = 0 while i < 10: #发送十次 import smtplib from email.mime.text import MIMEText ...
- Python简单邮件发送源码
环境: Python27 主要代码: # -*- coding: utf-8 -*- ''' Created on 2016年10月18日 @author: xuxianglin ''' import ...
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- C#UDP(接收和发送源码)源码完整
C#UDP(接收和发送源码)源码完整 最近做了一个UDP的服务接收和发送的东西.希望能对初学的朋友一点帮助. 源码如下: 一.逻辑--UdpServer.cs using System;using S ...
- 如何查看Outlook邮件的源码(包括ip)
如何查看Outlook邮件的源码(包括ip) 一.总结 1.右键点击邮件可出现 view message details. 二.如何查看Outlook邮件的源码(包括ip) 1.点收件箱 2.鼠标右键 ...
- Jquery+Ajax+asp.net+sqlserver-编写的通用邮件管理(源码)
开始 邮件管理通常用在各个内部系统中,为了方便快捷的使用现有的代码开发一个邮件管理系统而诞生的. 准备条件 这是我的设计表结构,大家一看就懂了 --邮件接收表CREATE TABLE [dbo]. ...
- 工具类学习-java实现邮件发送激活码
问题:用java实现服务器发送激活码到用户邮件. 步骤一:如果是个人的话,确保在本地安装邮件服务器(易邮服务器)和邮件客户端(foxmail). 步骤二:导入jar包 mail.jar,其他的需要什 ...
- C# 真正能发邮件的源码
在网上找了很多例子都试邮件发送都失败,今天无意有试了一下居然行了 public static void ErrorMessageMail(string _subject, string _body) ...
- c# .NET开发邮件发送功能的全面教程(含邮件组件源码)
http://www.cnblogs.com/heyuquan/p/net-batch-mail-send-async.html
随机推荐
- [LuoguP1829]Crash的文明表格(二次扫描与换根+第二类斯特林数)
Solution: 由于 \[ x^m = \sum_{i=0}^m{~m~\choose i}{~x~\brace i}i! \] 将所求的式子化成这样,挖掘其性质,考虑是否能从儿子转移(或 ...
- jvm性能监控(5)-jdk自带工具 VisualVM
一.在服务器的jdk的bin目录下添加配置文件 jstatd.all.policy [root@localhost /]# cd /usr/local/src/jdk1.8.0_131/bin/ [r ...
- Codeforces 1110E (差分)
题面 传送门 分析 一开始考虑贪心和DP,发现不行 考虑差分: 设d[i]=c[i+1]-c[i] (i<n) 那么一次操作会如何影响差分数组呢? \(c[i]'=c[i+1]+c[i-1]-c ...
- Java并发编程:Concurrent锁机制解析
Java并发编程:Concurrent锁机制解析 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: # ...
- SQL server 聚集索引与主键的区别
主键是一个约束(constraint),他依附在一个索引上,这个索引可以是聚集索引,也可以是非聚集索引. 所以在一个(或一组)字段上有主键,只能说明他上面有个索引,但不一定就是聚集索引. 例如下面: ...
- 浏览器如何减少 reflow/repaint
1.不要一条一条地修改 DOM 的样式.与其这样,还不如预先定义好 css 的 class,然后修改 DOM 的 className. 2)把 DOM 离线后修改.如: 使用 documentFrag ...
- worldcloud库的使用
worldcloud库的使用 worldcloud是一个优秀的第三方词云展示库,用来实现比较有逼格的数据可视化效果.更加直观与艺术的展示单词. worldcloud对象的创建 worldcloud.W ...
- HDU-5378 概率DP
题意:给定一棵有n个节点的树,现在要给节点附1~n的权值(各节点权值不能相同),一棵子树的领袖就是子树中权值最大的节点,问有多少种分配方案使得最后有恰好K个领袖. 解法:这道题一看以为是树上的计数问题 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- Spring事务中的readonly
来源:https://www.cnblogs.com/straybirds/p/9147892.html Spring的事务经常会有这样的配置: <tx:method name="se ...