这是用开源项目PHPMailer实现邮件发送 ,先下载文件,我这里下载得是5.1得版本,然后把三个class.***.php文件放到项目文件下,我首先用得是gmail,但是失败了,问题是认证失败,后来用了QQ邮箱,发送成功。 require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录 $mail = new PHPMailer(); //建立邮件发送类 $address ="youbinliu@126.com"; $mail->IsSMTP(); // 使用SMTP方式发送 $mail->Host = "smtp.qq.com"; // 您的企业邮局域名 $mail->SMTPAuth = true; // 启用SMTP验证功能 $mail->Username = "843831601@qq.com"; // 邮局用户名(请填写完整的email地址) $mail->Password = "***********"; // 邮局密码 $mail->Port=25; $mail->From = "843831601@qq.com"; //邮件发送者email地址 $mail->FromName = "liuyoubin"; $mail->AddAddress("$address", "a");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名") //$mail->AddReplyTo("", "");
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件 //$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
$mail->Subject = "PHPMailer测试邮件"; //邮件标题 $mail->Body = "Hello,这是测试邮件"; //邮件内容 $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略
if(!$mail->Send()) { echo "邮件发送失败. "; echo "错误原因: " . $mail->errorinfo; exit; }
echo "邮件发送成功"; ?>
附件: phpmailer 中文使用说明(简易版) a开头: $altbody--属性 出自:phpmailer::$altbody 文件:class.phpmailer.php 说明:该属性的设置是在邮件正文不支持html的备用显示 addaddress--方法 出自:phpmailer::addaddress(),文件:class.phpmailer.php 说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例 addaddress("eb163@eb163.com","eb163"),但参数2可选,addaddress(eb163@eb163.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中引用 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 说明:设置每行最大字符数,超过改数后自动换行
|