利用 PHPMailer发送邮件(可发送 HTML内容,图片,附件)
phpmailer是一个用于发送电子邮件的php类,他比php自带的函数mail强多了,phpmailer可以到官方下载。
下面来看一个只发送文本的实例。
*/
代码如下 | 复制代码 |
require("class.phpmailer.php"); $mail->addaddress("email@example.com"); if(!$mail->send()) issendmail - via sendmail command. require("class.phpmailer.php"); $mail->addaddress("email@example.com"); if(!$mail->send()) |
/*
添加有关发件人inforation,使用以下功能
代码如下 | 复制代码 |
mail->from="mailer@example.com"; for specifying various types of recepients use these: $mail->addaddress("mail1@domain.com", "recepient 1"); |
如何出现乱码可利用
$mail->charset="windows-1251";
$mail->charset="utf-8";
设置编码
如果要想发送邮件可以发送图片和附低年及html代码就在$mail-send()前面加
代码如下 | 复制代码 |
$mail->ishtml(true); $mail->addembeddedimage('logo.jpg', 'logoimg', 'logo.jpg'); // attach file logo.jpg, and later link to it using identfier logoimg $mail->body = "<h1>test 1 of phpmailer html</h1> <p>this is a test picture: <img src="cid:logoimg" /></p>"; $mail->altbody="this is text only alternative body."; |
发送附件
代码如下 | 复制代码 |
$mail->ishtml(false); |
*/
本款php发送邮件代码他利用了php自由的邮件发送函数mail进行邮件发送的,他会利用二种试一是判断mail函数是否可用,如果行就利用它来发送否则fsockopen来操作。
*/
代码如下 | 复制代码 |
class email function __construct() function set($server, $port, $user, $password, $type = 1, $delimiter = 1, $mailusername = 0) function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = '') function errorlog($type, $message, $is) |
代码如下 | 复制代码 |
function send_msg($to,$subject,$body) { |
pop3邮箱登录
代码如下 | 复制代码 |
function pop3_login($host,$username,$password) |
smtp登录验证函数
代码如下 | 复制代码 |
function smtp_login($host,$username,$password) |
imap登录验证函数
代码如下 | 复制代码 |
function imap_login($host,$username,$password) |
在php中发邮件会用到mail函数 ,但是大多数情情况mail函数是不可用的,我们有插件来实例了phpmailer来实现发送邮件。
代码如下 | 复制代码 |
require("class.phpmailer.php"); $mail->from = "这里应该可以填写你想要填写的邮箱"; //$mail->wordwrap = 50; // set word wrap to 50 characters $mail->subject = "phpmailer测试邮件"; if(!$mail->send()) echo "message has been sent"; |
/*
$altbody --属性
出自:phpmailer ::$altbody
文件:class.phpmailer .php
说明:该属性的设置是在邮件正文不支持html的备用显示
addaddress --方法
出自:phpmailer ::addaddress(),文件:class.phpmailer .php
说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例addaddress("to@163.com","to name"),但参数2可选,addaddress(to@163.com )也是可以的。
函数原型: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中引用<img src= "cid:img_01 ">
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连接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",or"smtp").中的一个
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
说明:设置每行最大字符数,超过改数后自动换行
*/
?>
*/
require_once ('common/email.class.php');
代码如下 | 复制代码 |
//这里以smiley_face@126.com邮箱为例,使用者需要自己修改 $smtps教程erver = "smtp.126.com";//smtp服务器 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true表示使用身份验证,否则不使用身份验证.
/* private variables */ /* constractor */ $this->sock = false; /* main function */ if ($cc != "") { if ($bcc != "") { $sent = true; /* private functions */ function smtp_send($helo, $from, $to, $header, $body = "") if (!$this->smtp_putcmd("", base64_encode($this->pass))) { if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) { if (!$this->smtp_putcmd("data")) { if (!$this->smtp_message($header, $body)) { if (!$this->smtp_eom()) { if (!$this->smtp_putcmd("quit")) { return true; function smtp_sockopen($address) function smtp_sockopen_relay() function smtp_sockopen_mx($address) function smtp_message($header, $body) return true; function smtp_eom() return $this->smtp_ok(); function smtp_ok() if (!preg_match("/^[23]/", $response)) { |
相关文章
- 这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
- PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
- 本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
- 这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2
Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25- 学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
- //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
- 这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
- <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25
解决PHPMailer错误SMTP Error: Could not connect to SMTP host的办法
PHPMailer发邮件时提示SMTP Error: Could not connect to SMTP host错误是smtp服务器的问题我们一起来看看关于SMTP Error: Could not connect to SMTP host问题的解...2016-11-25- 这篇文章主要为大家详细介绍了C# Email发送邮件功能,对方打开通知你,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 我经常听到这样一个问题:"我有一个从网站发来的合同。我如何给通过表单发送的电子邮件增加一个附件呢?" 首先我要说的是要做到这个没有什么简单的办法。你要很好的理解PH...2016-11-25
phpmailer发送邮件 SMTP Error: Could not authenticate 错误
今天在使用phpmailer发送邮件时居然提示SMTP Error: Could not authenticate,这个感觉是smtp设置的问题,下面我在网上找到了几种解决办法。 今天在使用phpmailer发...2016-11-25- header("Content-type:text/html;charset=utf-8"); include('phpmailer/class.phpmailer.php'); include('phpmailer/class.smtp.php'); $mail = new PHPMailer();...2016-11-25
phpmailer邮件发送实例(163邮箱 126邮箱 yahoo邮箱)
phpmailer是一个非常优秀的php邮箱发送插件了,他可以几乎实现任何邮箱登录发送,下面我介绍163邮箱 126邮箱 yahoo邮箱的发送方法。 准备工作: 我们必须注册一个邮...2016-11-25- 我们利用 phpmailer功能实现 邮件发送功能哦,这里还利用了模板呢,就是读取指定文件内容再发送给朋友。 <?php @session_start(); include(dirname(__FILE__).'....2016-11-25
- 本文为大家详细介绍下.net SMTP发送Email同时可带附件的具体实现思路及代码,想实现的朋友可以参考下哈,希望对大家有所帮助...2021-09-22
- 在C#中发送邮件的方式有2种,一种是使用webmail方式进行发送,另外一种就是采用netmail发送的方式,这篇文章介绍了c#使用webmail方式发送邮件示例,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了Yii2使用swiftmailer发送邮件的方法,结合实例形式分析了Yii2使用swiftmailer进行邮件发送的设置与代码实现技巧,需要的朋友可以参考下...2016-05-05