您好,欢迎访问一九零五行业门户网

PHPMailer PHP邮件发送类

一、phpmailer简介phpmailer 是一个功能强大的 php邮件发送类,它可以更加便捷的发送邮件,并且还能发送附件和html格式的邮件,同时还能使用 smtp 服务器来发送邮件。
由于php自带mail() 函数功能并不完善,只能发送文本的e-mail,并且mail() 函数只能应用在linux 服务器,还有一个最大的问题就是使用mail() 函数发送的邮件没有进行身份验证,很多邮件收不到使用 mail() 函数发送的邮件或者发送的邮件直接进入垃圾邮箱。而phpmailer给我们提供了所有的邮件发送方式,我们可以按需选择适合的邮件处理方法。
phpmailer 是一个功能强大的邮件类,其主要功能特点:
支持邮件 s/mime加密的数字签名
支持邮件多个 tos, ccs, bccs and reply-tos
可以工作在任何服务器平台,所以不用担心win平台无法发送邮件的问题的
支持文本/html格式邮件
可以嵌入image图像
对于邮件客户端不支持html阅读的进行支持
功能强大的发送邮件调试功能debug
自定义邮件header
冗余smtp服务器支持
支持8bit, base64, binary, and quoted-printable 编码
文字自动换行
支持多附件发送功能
支持smtp服务器验证功能
在sendmail, qmail, postfix, gmail, imail, exchange 等平台测试成功
提供的下载文件中,包括内容详细的说明文档及示例说明,所以不用担心难于上手的问题!
phpmailer 非常小巧、简单、方便、快捷
二、phpmailer使用1.首先是下载phpmailer
http://code.google.com/a/apache-extras.org/p/phpmailer/
2.解压
从中取出class.phpmailer.php 和 class.smtp.php 放到你的项目的文件夹
3.创建发送邮件的函数,其中你需要配置smtp服务器
function postmail($to,$subject = '',$body = ''){
//author:jiucool website: http://www.jiucool.com
//$to 表示收件人地址 $subject 表示邮件标题 $body表示邮件正文
//error_reporting(e_all);
error_reporting(e_strict);
date_default_timezone_set('asia/shanghai');//设定时区东八区
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new phpmailer(); //new一个phpmailer对象出来
$body = eregi_replace([],'',$body); //对邮件内容进行必要的过滤
$mail->charset =gbk;//设定邮件编码,默认iso-8859-1,如果发中文此项必须设置,否则乱码
$mail->issmtp(); // 设定使用smtp服务
$mail->smtpdebug = 1; // 启用smtp调试功能
// 1 = errors and messages
// 2 = messages only
$mail->smtpauth = true; // 启用 smtp 验证功能
$mail->smtpsecure = ssl; // 安全协议,可以注释掉
$mail->host = 'stmp.163.com'; // smtp 服务器
$mail->port = 25; // smtp服务器的端口号
$mail->username = 'wangliang_198x'; // smtp服务器用户名,ps:我乱打的
$mail->password = 'password'; // smtp服务器密码
$mail->setfrom('xxx@xxx.xxx', 'who');
$mail->addreplyto('xxx@xxx.xxx','who');
$mail->subject = $subject;
$mail->altbody = 'to view the message, please use an html compatible email viewer!'; // optional, comment out and test
$mail->msghtml($body);
$address = $to;
$mail->addaddress($address, '');
//$mail->addattachment(images/phpmailer.gif); // attachment
//$mail->addattachment(images/phpmailer_mini.gif); // attachment
if(!$mail->send()) {
echo 'mailer error: ' . $mail->errorinfo;
} else {
// echo message sent!恭喜,邮件发送成功!;
}
}
4. 使用函数
postmail('scutephp@qq.com','my subject','php建站门户');
三、phpmailer详细使用说明a开头:$altbody--属性
出自:phpmailer::$altbody
文件:class.phpmailer.php
说明:该属性的设置是在邮件正文不支持html的备用显示
addaddress--方法
出自:phpmailer::addaddress(),文件:class.phpmailer.php
说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例 addaddress(xiaoxiaoxiaoyu@xiaoxiaoyu.cn,xiaoxiaoyu),但参数2可选,addaddress(xiaoxiaoxiaoyu@xiaoxiaoyu.cn)也是可以的。
函数原型:public function addaddress($address, $name = '') {}
addattachment--方法
出自:phpmailer::addattachment()
文件:class.phpmailer.php。
说明:增加附件。
参数:路径,名称,编码,类型。其中,路径为必选,其他为可选
函数原型:
addattachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){}
addbcc--方法
出自:phpmailer::addbcc()
文件:class.phpmailer.php
说明:增加一个密送。抄送和密送的区别请看[smtp发件中的密送和抄送的区别] 。
参数1为地址,参数2为名称。注意此方法只支持在win32下使用smtp,不支持mail函数
函数原型:public function addbcc($address, $name = ''){}
addcc --方法
出自:phpmailer::addcc()
文件:class.phpmailer.php
说明:增加一个抄送。抄送和密送的区别请看[smtp发件中的密送和抄送的区别] 。
参数1为地址,参数2为名称注意此方法只支持在win32下使用smtp,不支持mail函数
函数原型:public function addcc($address, $name = '') {}
addcustomheader--方法
出自:phpmailer::addcustomheader()
文件:class.phpmailer.php
说明:增加一个自定义的e-mail头部。
参数为头部信息
函数原型:public function addcustomheader($custom_header){}
addembeddedimage --方法
出自:phpmailer::addembeddedimage()
文件:class.phpmailer.php
说明:增加一个嵌入式图片
参数:路径,返回句柄[,名称,编码,类型]
函数原型:public function addembeddedimage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {}
提示:addembeddedimage(picture_path. index_01.jpg , img_01 , index_01.jpg );
在html中引用
addreplyto--方法
出自:phpmailer:: addreplyto()
文件:class.phpmailer.php
说明:增加回复标签,如reply-to
参数1地址,参数2名称
函数原型:public function addreplyto($address, $name = '') {}
addstringattachment-方法
出自:phpmailer:: addstringattachment()
文件:class.phpmailer.php
说明:增加一个字符串或二进制附件(adds a string or binary attachment (non-filesystem) to the list.?)
参数:字符串,文件名[,编码,类型]
函数原型:public function addstringattachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {}
authenticate--方法
出自:smtp::authenticate()
文件:class.smtp.php
说明:开始smtp认证,必须在hello()之后调用,如果认证成功,返回true,
参数1用户名,参数2密码
函数原型:public function authenticate($username, $password) {}
b开头$body--属性
出自:phpmailer::$body
文件: class.phpmailer.php
说明:邮件内容,html或text格式
c开头$charset--属性
出自:phpmailer::$charset
文件:class.phpmailer.php
说明:邮件编码,默认为iso-8859-1
$confirmreadingto--属性
出自:phpmailer::$confirmreadingto 文件class.phpmailer.php
说明:回执?
$contenttype--属性
出自:phpmailer::$contenttype
文件: class.phpmailer.php
说明:文档的类型,默认为text/plain
$crlf--属性
出自:phpmailer::$contenttype
文件:class.phpmailer.php
说明:smtp回复结束的分隔符(smtp reply line ending?)
class.phpmailer.php--对象
出自:class.phpmailer.php
文件: class.phpmailer.php
说明:phpmailer对象
class.smtp.php--对象
出自:class.smtp.php 文件: class.smtp.php
说明:smtp功能的对象
clearaddresses--方法
出自:phpmailer::clearaddresses()
文件: class.phpmailer.php
说明:清除收件人,为下一次发件做准备。返回类型是void
clearallrecipients--方法
出自:phpmailer::clearallrecipients()
文件: class.phpmailer.php
说明:清除所有收件人,包括cc(抄送)和bcc(密送)
clearattachments--方法
出自:phpmailer::clearattachments()
文件: class.phpmailer.php
说明:清楚附件
clearbccs--方法
出自:phpmailer::clearbccs() 文件 class.phpmailer.php
说明:清楚bcc (密送)
clearcustomheaders--方法
出自:phpmailer::clearcustomheaders()
文件: class.phpmailer.php
说明:清楚自定义头部
clearreplytos--方法
出自:phpmailer::clearreplytos()
文件: class.phpmailer.php
说明:清楚回复人
close--方法
出自:smtp::close()
文件: class.smtp.php
说明:关闭一个smtp连接
connect--方法
出自:smtp::connect()
文件: class.smtp.php
说明:建立一个smtp连接[/color]mailer.html
$contenttype--属性
出自:phpmailer::$contenttype
文件: class.phpmailer.php
说明:文档的类型,默认为text/plain
d开头$do_debug--属性
出自:smtp::$do_debug
文件:class.smtp.php
说明:smtp调试输出
data-方法
出自:smtp::data()
文件:class.smtp.php
说明:向服务器发送一个数据命令和消息信息(sendsthemsg_datatotheserver)
e开头$encoding--属性
出自:phpmailer::$encoding
文件:class.phpmailer.php
说明:设置邮件的编码方式,可选:8bit,7bit,binary,base64,和quoted-printable.
$errorinfo--属性
出自:phpmailer::$errorinfo
文件:class.phpmailer.php
说明:返回邮件smtp中的最后一个错误信息
expand--方法
出自:smtp::expand()
文件:class.smtp.php
说明:返回邮件列表中所有用户。成功则返回数组,否则返回 false(expandtakesthenameandaskstheservertolistallthepeoplewhoaremembersofthe_list_.expandwillreturnbackandarrayoftheresultorfalseifanerroroccurs.)
f开头:$from--属性
出自:phpmailer::$from文件class.phpmailer.php
说明:发件人e-mail地址
$fromname--属性
出自:phpmailer::$fromname
文件:class.phpmailer.php
说明:发件人称呼
h开头:$helo--属性
出自:phpmailer::$helo
文件:class.phpmailer.php
说明:设置smtphelo,默认是$hostname(setsthesmtpheloofthemessage(defaultis$hostname).)
$host--属性
出自:phpmailer::$host
文件:class.phpmailer.php
说明:设置smtp服务器,格式为:主机名[端口号],如smtp1.example.com:25和smtp2.example.com都是合法的
$hostname--属性
出自:phpmailer::$hostname
文件:class.phpmailer.php
说明:设置在message-id和andreceivedheaders中的hostname并同时被$helo使用。如果为空,默认为server_name或'localhost.localdomain
hello--方法
出自:smtp::hello()
文件:class.smtp.php
说明:向smtp服务器发送helo命令
help--方法
出自:smtp::help()
文件:class.smtp.php
说明:如果有关键词,得到关键词的帮助信息
i开头:iserror--方法
出自:phpmailer::iserror()
文件:class.phpmailer.php
说明:返回是否有错误发生
ishtml--方法
出自:phpmailer::ishtml()
文件:class.phpmailer.php
说明:设置信件是否是html格式
ismail--方法
出自:phpmailer::ismail()
文件:class.phpmailer.php
说明:设置是否使用php的mail函数发件
isqmail--方法
出自:phpmailer::isqmail()
文件:class.phpmailer.php
说明:设置是否使用qmailmta来发件
issendmail--方法
出自:phpmailer::issendmail()
文件:class.phpmailer.php
说明:是否使用$sendmail程序来发件
issmtp--方法
出自:phpmailer::issmtp()
文件:class.phpmailer.php
说明:是否使用smtp来发件
m开头:$mailer--属性
出自:phpmailer::$mailer
文件:class.phpmailer.php
说明:发件方式,(mail,sendmail,orsmtp).中的一个
mail--方法
出自:smtp::mail()
文件:class.smtp.php
说明:从$from中一个邮件地址开始处理,返回true或false。如果是true,则开始发件
n开头:noop--方法
出自:smtp::noop()
文件:class.smtp.php
说明:向smtp服务器发送一个noop命令
p开头:$password--属性
出自:phpmailer::$password
文件:class.phpmailer.php
说明:设置smtp的密码
$plugindir--属性
出自:phpmailer::$plugindir
文件:class.phpmailer.php
说明:设置phpmailer的插件目录,仅在smtpclass不在phpmailer目录下有效
$port--属性
出自:phpmailer::$port
文件:class.phpmailer.php
说明:设置smtp的端口号
$priority--属性
出自:phpmailer::$priority
文件:class.phpmailer.php
说明:设置邮件投递优先等级。1=紧急,3=普通,5=不急
phpmailer--对象
出自:phpmailer
文件:class.phpmailer.php
说明:phpmailer-phpemailtransportclass
q开头quit--方法
出自:smtp::quit()
文件:class.smtp.php
说明:向服务器发送quit命令,如果没有错误发生。那么关闭sock,不然$close_on_error为true
r开头recipient--方法
出自:smtp::recipient()
文件:class.smtp.php
说明:使用to向smtp发送rcpt命令,参数为:$to
reset--方法
出自:smtp::reset()
文件:class.smtp.php
说明:发送rset命令从而取消处理中传输。成功则返回true,否则为false
s开头:$sender--属性
出自:phpmailer::$sender
文件:class.phpmailer.php
说明:setsthesenderemail(return-path)ofthemessage.ifnotempty,willbesentvia-ftosendmailoras'mailfrom'insmtpmode.
$sendmail--属性
出自:phpmailer::$sendmail
文件:class.phpmailer.php
说明:设置发件程序的目录
$smtpauth--属性
出自:phpmailer::$smtpauth
文件:class.phpmailer.php
说明:设置smtp是否需要认证,使用username和password变量
$smtpdebug--属性
出自:phpmailer::$smtpdebug
文件:class.phpmailer.php
说明:设置smtp是否调试输出?
$smtpkeepalive--属性
出自:phpmailer::$smtpkeepalive
文件:class.phpmailer.php
说明:在每次发件后不关闭连接。如果为true,则,必须使用smtpclose()来关闭连接
$smtp_port--属性
出自:smtp::$smtp_port
文件:class.smtp.php
说明:设置smtp端口
$subject--属性
出自:phpmailer::$subject
文件:class.phpmailer.php
说明:设置信件的主题
send--方法
出自:smtp::send()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
send--方法
出自:phpmailer::send()
文件:class.phpmailer.php
说明:创建邮件并制定发件程序。如果发件不成功,则返回false,请使用errorinfo来查看错误信息
sendandmail--方法
出自:smtp::sendandmail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
sendormail--方法
出自:smtp::sendormail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
setlanguage--方法
出自:phpmailer::setlanguage()
文件:class.phpmailer.php
说明:设置phpmailer错误信息的语言类型,如果无法加载语言文件,则返回false,默认为english
smtp--方法
出自:smtp::smtp()
文件:class.smtp.php
说明:初始化一个对象以便数据处于一个已知的状态
smtp--对象
出自:smtp
文件:class.smtp.php
说明:smtp对象
smtpclose--方法
出自:phpmailer::smtpclose()
文件:class.phpmailer.php
说明:如果有活动的smtp则关闭它。
t开头$timeout--属性
出自:phpmailer::$timeout
文件:class.phpmailer.php
说明:设置smtp服务器的超时(单位:秒)。注意:在win32下,该属性无效
turn--方法
出自:smtp::turn()
文件:class.smtp.php
说明:这是一个可选的smtp参数,目前phpmailer并不支持他,可能未来支持
u开头$username--属性
出自:phpmailer::$username
文件:class.phpmailer.php
说明:设置smtp用户名
v开头$version--属性
出自:phpmailer::$version
文件:class.phpmailer.php
说明:返回phpmailer的版本
verify--方法
出自:smtp::verify()
文件:class.smtp.php
说明:通过服务器检查用户名是否经过验证
w开头:$wordwrap--属性
出自:phpmailer::$wordwrap
文件:class.phpmailer.php 说明:设置每行最大字符数,超过改数后自动换行
其它类似信息

推荐信息