PHPMailer中文说明

来源:互联网 发布:网络暴力的案例有哪些 编辑:程序博客网 时间:2024/04/30 16:34

A开头:

$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连接[/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",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
说明:设置每行最大字符数,超过改数后自动换行

class.phpmailer.php文件

[php] view plaincopyprint?
  1. <?php
  2. /*~ class.phpmailer.php 说明:phpmailer对象
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 2.0.4 |
  6. | Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
  7. | Info: http://phpmailer.sourceforge.net |
  8. | Support: http://sourceforge.net/projects/phpmailer/ |
  9. | ------------------------------------------------------------------------- |
  10. | Author: Andy Prevost (project admininistrator) |
  11. | Author: Brent R. Matzelle (original founder) |
  12. | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
  13. | Copyright (c) 2001-2003, Brent R. Matzelle |
  14. | ------------------------------------------------------------------------- |
  15. | License: Distributed under the Lesser General Public License (LGPL) |
  16. | http://www.gnu.org/copyleft/lesser.html |
  17. | This program is distributed in the hope that it will be useful - WITHOUT |
  18. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  19. | FITNESS FOR A PARTICULAR PURPOSE. |
  20. | ------------------------------------------------------------------------- |
  21. | We offer a number of paid services (www.codeworxtech.com): |
  22. | - Web Hosting on highly optimized fast and secure servers |
  23. | - Technology Consulting |
  24. | - Oursourcing (highly qualified programmers and graphic designers) |
  25. '---------------------------------------------------------------------------'
  26. /**
  27. * PHPMailer - PHP email transport class
  28. * @package PHPMailer
  29. * @author Andy Prevost
  30. * @copyright 2004 - 2009 Andy Prevost
  31. */
  32. class PHPMailer {
  33. /////////////////////////////////////////////////
  34. // PROPERTIES, PUBLIC
  35. /////////////////////////////////////////////////
  36. /**
  37. * Email priority (1 = High, 3 = Normal, 5 = low).
  38. * @var int
  39. * 说明:设置邮件投递优先等级。1=紧急,3=普通,5=不急
  40. */
  41. var $Priority = 3;
  42. /**
  43. * Sets the CharSet of the message.
  44. * @var string
  45. * 说明:邮件编码,默认为iso-8859-1
  46. */
  47. var $CharSet = 'iso-8859-1';
  48. /**
  49. * Sets the Content-type of the message.
  50. * @var string
  51. * 说明:文档的类型,默认为"text/plain"
  52. */
  53. var $ContentType ='text/plain';
  54. /**
  55. * Sets the Encoding of the message. Options for this are "8bit",
  56. * "7bit", "binary", "base64", and "quoted-printable".
  57. * @var string
  58. * 说明:设置邮件的编码方式,可选:"8bit","7bit","binary","base64",和"quoted-printable".
  59. */
  60. var $Encoding ='8bit';
  61. /**
  62. * Holds the most recent mailer error message.
  63. * @var string
  64. * 说明:返回邮件SMTP中的最后一个错误信息
  65. */
  66. var $ErrorInfo = '';
  67. /**
  68. * Sets the From email address for the message.
  69. * @var string
  70. * 说明:发件人E-mail地址
  71. */
  72. var $From ='root@localhost';
  73. /**
  74. * Sets the From name of the message.
  75. * @var string
  76. * 说明:发件人称呼
  77. */
  78. var $FromName = 'Root User';
  79. /**
  80. * Sets the Sender email (Return-Path) of the message. If not empty,
  81. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  82. * @var string
  83. * 说明:SetstheSenderemail(Return-Path)ofthemessage.Ifnotempty,willbesentvia-ftosendmailoras'MAILFROM'insmtpmode.
  84. */
  85. var $Sender = '';
  86. /**
  87. * Sets the Subject of the message.
  88. * @var string
  89. * 说明:设置信件的主题
  90. */
  91. var $Subject ='';
  92. /**
  93. * Sets the Body of the message. This can be either an HTML or text body.
  94. * If HTML then run IsHTML(true).
  95. * @var string
  96. * 说明:邮件内容,HTML或Text格式
  97. */
  98. var $Body ='';
  99. /**
  100. * Sets the text-only body of the message. This automatically sets the
  101. * email to multipart/alternative. This body can be read by mail
  102. * clients that do not have HTML email capability such as mutt. Clients
  103. * that can read HTML will view the normal Body.
  104. * @var string
  105. * 该属性的设置是在邮件正文不支持HTML的备用显示
  106. */
  107. var $AltBody ='';
  108. /**
  109. * Sets word wrapping on the body of the message to a given number of
  110. * characters.
  111. * @var int
  112. * 说明:设置每行最大字符数,超过改数后自动换行
  113. */
  114. var $WordWrap = 0;
  115. /**
  116. * Method to send mail: ("mail", "sendmail", or "smtp").
  117. * @var string
  118. * 说明:发件方式,("mail","sendmail",or"smtp").中的一个
  119. */
  120. var $Mailer = 'mail';
  121. /**
  122. * Sets the path of the sendmail program.
  123. * @var string
  124. * 说明:设置发件程序的目录
  125. */
  126. var $Sendmail ='/usr/sbin/sendmail';
  127. /**
  128. * Path to PHPMailer plugins. This is now only useful if the SMTP class
  129. * is in a different directory than the PHP include path.
  130. * @var string
  131. * 说明:设置phpmailer的插件目录,仅在smtpclass不在phpmailer目录下有效
  132. */
  133. var $PluginDir ='';
  134. /**
  135. * Holds PHPMailer version.
  136. * @var string
  137. * 说明:返回Phpmailer的版本
  138. */
  139. var $Version = "2.0.4";
  140. /**
  141. * Sets the email address that a reading confirmation will be sent.
  142. * @var string
  143. * 回执?
  144. */
  145. var $ConfirmReadingTo ='';
  146. /**
  147. * Sets the hostname to use in Message-Id and Received headers
  148. * and as default HELO string. If empty, the value returned
  149. * by SERVER_NAME is used or 'localhost.localdomain'.
  150. * @var string
  151. * 说明:设置在Message-Id和andReceivedheaders中的hostname并同时被$Helo使用。如果为空,默认为SERVER_NAME或'localhost.localdomain"
  152. */
  153. var $Hostname = '';
  154. /**
  155. * Sets the message ID to be used in the Message-Id header.
  156. * If empty, a unique id will be generated.
  157. * @var string
  158. */
  159. var $MessageID ='';
  160. /////////////////////////////////////////////////
  161. // PROPERTIES FOR SMTP
  162. /////////////////////////////////////////////////
  163. /**
  164. * Sets the SMTP hosts. All hosts must be separated by a
  165. * semicolon. You can also specify a different port
  166. * for each host by using this format: [hostname:port]
  167. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  168. * Hosts will be tried in order.
  169. * @var string
  170. * 说明:设置SMTP服务器,格式为:主机名[端口号],如smtp1.example.com:25和smtp2.example.com都是合法的
  171. */
  172. var $Host = 'localhost';
  173. /**
  174. * Sets the default SMTP server port.
  175. * @var int
  176. * 说明:设置SMTP的端口号
  177. */
  178. var $Port = 25;
  179. /**
  180. * Sets the SMTP HELO of the message (Default is $Hostname).
  181. * @var string
  182. * 说明:设置SMTPHelo,默认是$Hostname(SetstheSMTPHELOofthemessage(Defaultis$Hostname).)
  183. */
  184. var $Helo = '';
  185. /**
  186. * Sets connection prefix.
  187. * Options are "", "ssl" or "tls"
  188. * @var string
  189. */
  190. var $SMTPSecure ="";
  191. /**
  192. * Sets SMTP authentication. Utilizes the Username and Password variables.
  193. * @var bool
  194. * 说明:设置SMTP是否需要认证,使用Username和Password变量
  195. */
  196. var $SMTPAuth = false;
  197. /**
  198. * Sets SMTP username.
  199. * @var string
  200. * 说明:设置SMTP用户名
  201. */
  202. var $Username ='';
  203. /**
  204. * Sets SMTP password.
  205. * @var string
  206. * 说明:设置SMTP的密码
  207. */
  208. var $Password = '';
  209. /**
  210. * Sets the SMTP server timeout in seconds. This function will not
  211. * work with the win32 version.
  212. * @var int
  213. * 说明:设置SMTP服务器的超时(单位:秒)。注意:在win32下,该属性无效
  214. */
  215. var $Timeout = 10;
  216. /**
  217. * Sets SMTP class debugging on or off.
  218. * @var bool
  219. * 说明:设置SMTP是否调试输出?
  220. */
  221. var $SMTPDebug = false;
  222. /**
  223. * Prevents the SMTP connection from being closed after each mail
  224. * sending. If this is set to true then to close the connection
  225. * requires an explicit call to SmtpClose().
  226. * @var bool
  227. * 说明:在每次发件后不关闭连接。如果为true,则,必须使用SmtpClose()来关闭连接
  228. */
  229. var $SMTPKeepAlive = false;
  230. /**
  231. * Provides the ability to have the TO field process individual
  232. * emails, instead of sending to entire TO addresses
  233. * @var bool
  234. */
  235. var $SingleTo = false;
  236. /////////////////////////////////////////////////
  237. // PROPERTIES, PRIVATE
  238. /////////////////////////////////////////////////
  239. var $smtp = NULL;
  240. var $to = array();
  241. var $cc = array();
  242. var $bcc = array();
  243. var $ReplyTo =array();
  244. var $attachment = array();
  245. var $CustomHeader =array();
  246. var $message_type = '';
  247. var $boundary =array();
  248. var $language = array();
  249. var $error_count = 0;
  250. var $LE = "/n";
  251. var $sign_cert_file ="";
  252. var $sign_key_file = "";
  253. var $sign_key_pass ="";
  254. /////////////////////////////////////////////////
  255. // METHODS, VARIABLES
  256. /////////////////////////////////////////////////
  257. /**
  258. * Sets message type to HTML.
  259. * @param bool $bool
  260. * @return void
  261. * 说明:设置信件是否是HTML格式
  262. */
  263. function IsHTML($bool) {
  264. if($bool == true) {
  265. $this->ContentType = 'text/html';
  266. } else {
  267. $this->ContentType = 'text/plain';
  268. }
  269. }
  270. /**
  271. * Sets Mailer to send message using SMTP.
  272. * @return void
  273. * 说明:是否使用SMTP来发件
  274. */
  275. function IsSMTP() {
  276. $this->Mailer = 'smtp';
  277. }
  278. /**
  279. * Sets Mailer to send message using PHP mail() function.
  280. * @return void
  281. * 说明:设置是否使用php的mail函数发件
  282. */
  283. function IsMail() {
  284. $this->Mailer = 'mail';
  285. }
  286. /**
  287. * Sets Mailer to send message using the $Sendmail program.
  288. * @return void
  289. * 说明:是否使用$Sendmail程序来发件
  290. */
  291. function IsSendmail() {
  292. $this->Mailer = 'sendmail';
  293. }
  294. /**
  295. * Sets Mailer to send message using the qmail MTA.
  296. * @return void
  297. * 说明:设置是否使用qmailMTA来发件
  298. */
  299. function IsQmail() {
  300. $this->Sendmail = '/var/qmail/bin/sendmail';
  301. $this->Mailer = 'sendmail';
  302. }
  303. /////////////////////////////////////////////////
  304. // METHODS, RECIPIENTS
  305. /////////////////////////////////////////////////
  306. /**
  307. * Adds a "To" address.
  308. * @param string $address
  309. * @param string $name
  310. * @return void
  311. * 增加收件人。参数1为收件人邮箱,参数2为收件人称呼。
  312. * 例AddAddress("xiaoxiaoxiaoyu@xiaoxiaoyu.cn","xiaoxiaoyu"),但参数2可选,AddAddress(xiaoxiaoxiaoyu@xiaoxiaoyu.cn)也是可以的。
  313. */
  314. function AddAddress($address,$name ='') {
  315. $cur = count($this->to);
  316. $this->to[$cur][0] = trim($address);
  317. $this->to[$cur][1] =$name;
  318. }
  319. /**
  320. * Adds a "Cc" address. Note: this function works
  321. * with the SMTP mailer on win32, not with the "mail"
  322. * mailer.
  323. * @param string $address
  324. * @param string $name
  325. * @return void
  326. * 增加一个抄送。
  327. * 参数1为地址,参数2为名称注意此方法只支持在win32下使用SMTP,不支持mail函数
  328. */
  329. function AddCC($address,$name ='') {
  330. $cur = count($this->cc);
  331. $this->cc[$cur][0] = trim($address);
  332. $this->cc[$cur][1] =$name;
  333. }
  334. /**
  335. * Adds a "Bcc" address. Note: this function works
  336. * with the SMTP mailer on win32, not with the "mail"
  337. * mailer.
  338. * @param string $address
  339. * @param string $name
  340. * @return void
  341. * 增加一个密送。
  342. * 参数1为地址,参数2为名称。注意此方法只支持在win32下使用SMTP,不支持mail函数
  343. */
  344. function AddBCC($address,$name ='') {
  345. $cur = count($this->bcc);
  346. $this->bcc[$cur][0] = trim($address);
  347. $this->bcc[$cur][1] =$name;
  348. }
  349. /**
  350. * Adds a "Reply-To" address.
  351. * @param string $address
  352. * @param string $name
  353. * @return void
  354. * 说明:增加回复标签,如"Reply-to"
  355. * 参数1地址,参数2名称
  356. */
  357. function AddReplyTo($address,$name ='') {
  358. $cur = count($this->ReplyTo);
  359. $this->ReplyTo[$cur][0] = trim($address);
  360. $this->ReplyTo[$cur][1] =$name;
  361. }
  362. /////////////////////////////////////////////////
  363. // METHODS, MAIL SENDING
  364. /////////////////////////////////////////////////
  365. /**
  366. * Creates message and assigns Mailer. If the message is
  367. * not sent successfully then it returns false. Use the ErrorInfo
  368. * variable to view description of the error.
  369. * @return bool
  370. * 说明:创建邮件并制定发件程序。如果发件不成功,则返回false,请使用ErrorInfo来查看错误信息
  371. */
  372. function Send() {
  373. $header = '';
  374. $body = '';
  375. $result = true;
  376. if((count($this->to) +count($this->cc) +count($this->bcc)) < 1) {
  377. $this->SetError($this->Lang('provide_address'));
  378. return false;
  379. }
  380. /* Set whether the message is multipart/alternative */
  381. if(!emptyempty($this->AltBody)) {
  382. $this->ContentType = 'multipart/alternative';
  383. }
  384. $this->error_count = 0; // reset errors
  385. $this->SetMessageType();
  386. $header .= $this->CreateHeader();
  387. $body = $this->CreateBody();
  388. if($body =='') {
  389. return false;
  390. }
  391. /* Choose the mailer */
  392. switch($this->Mailer) {
  393. case 'sendmail':
  394. $result = $this->SendmailSend($header,$body);
  395. break;
  396. case 'smtp':
  397. $result = $this->SmtpSend($header,$body);
  398. break;
  399. case 'mail':
  400. $result = $this->MailSend($header,$body);
  401. break;
  402. default:
  403. $result = $this->MailSend($header,$body);
  404. break;
  405. //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
  406. //$result = false;
  407. //break;
  408. }
  409. return $result;
  410. }
  411. /**
  412. * Sends mail using the $Sendmail program.
  413. * @access private
  414. * @return bool
  415. */
  416. function SendmailSend($header,$body) {
  417. if ($this->Sender !='') {
  418. $sendmail = sprintf("%s -oi -f %s -t",escapeshellcmd($this->Sendmail),escapeshellarg($this->Sender));
  419. } else {
  420. $sendmail = sprintf("%s -oi -t",escapeshellcmd($this->Sendmail));
  421. }
  422. if(!@$mail = popen($sendmail,'w')) {
  423. $this->SetError($this->Lang('execute') .$this->Sendmail);
  424. return false;
  425. }
  426. fputs($mail, $header);
  427. fputs($mail, $body);
  428. $result = pclose($mail);
  429. if (version_compare(phpversion(), '4.2.3') == -1) {
  430. $result = $result >> 8 & 0xFF;
  431. }
  432. if($result != 0) {
  433. $this->SetError($this->Lang('execute') .$this->Sendmail);
  434. return false;
  435. }
  436. return true;
  437. }
  438. /**
  439. * Sends mail using the PHP mail() function.
  440. * @access private
  441. * @return bool
  442. */
  443. function MailSend($header,$body) {
  444. $to = '';
  445. for($i = 0; $i < count($this->to);$i++) {
  446. if($i != 0) {$to .=', '; }
  447. $to .= $this->AddrFormat($this->to[$i]);
  448. }
  449. $toArr = split(',',$to);
  450. $params = sprintf("-oi -f %s",$this->Sender);
  451. if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
  452. $old_from = ini_get('sendmail_from');
  453. ini_set('sendmail_from',$this->Sender);
  454. if ($this->SingleTo === true &&count($toArr) > 1) {
  455. foreach ($toArr as $key => $val) {
  456. $rt = @mail($val,$this->EncodeHeader($this->SecureHeader($this->Subject)),$body,$header, $params);
  457. }
  458. } else {
  459. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)),$body,$header, $params);
  460. }
  461. } else {
  462. if ($this->SingleTo === true &&count($toArr) > 1) {
  463. foreach ($toArr as $key => $val) {
  464. $rt = @mail($val,$this->EncodeHeader($this->SecureHeader($this->Subject)),$body,$header, $params);
  465. }
  466. } else {
  467. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)),$body,$header);
  468. }
  469. }
  470. if (isset($old_from)) {
  471. ini_set('sendmail_from',$old_from);
  472. }
  473. if(!$rt) {
  474. $this->SetError($this->Lang('instantiate'));
  475. return false;
  476. }
  477. return true;
  478. }
  479. /**
  480. * Sends mail via SMTP using PhpSMTP (Author:
  481. * Chris Ryan). Returns bool. Returns false if there is a
  482. * bad MAIL FROM, RCPT, or DATA input.
  483. * @access private
  484. * @return bool
  485. */
  486. function SmtpSend($header,$body) {
  487. include_once($this->PluginDir .'class.smtp.php');
  488. $error = '';
  489. $bad_rcpt = array();
  490. if(!$this->SmtpConnect()) {
  491. return false;
  492. }
  493. $smtp_from = ($this->Sender =='') ?$this->From : $this->Sender;
  494. if(!$this->smtp->Mail($smtp_from)) {
  495. $error = $this->Lang('from_failed') .$smtp_from;
  496. $this->SetError($error);
  497. $this->smtp->Reset();
  498. return false;
  499. }
  500. /* Attempt to send attach all recipients */
  501. for($i = 0;$i <count($this->to);$i++) {
  502. if(!$this->smtp->Recipient($this->to[$i][0])) {
  503. $bad_rcpt[] = $this->to[$i][0];
  504. }
  505. }
  506. for($i = 0; $i < count($this->cc);$i++) {
  507. if(!$this->smtp->Recipient($this->cc[$i][0])) {
  508. $bad_rcpt[] = $this->cc[$i][0];
  509. }
  510. }
  511. for($i = 0;$i <count($this->bcc);$i++) {
  512. if(!$this->smtp->Recipient($this->bcc[$i][0])) {
  513. $bad_rcpt[] = $this->bcc[$i][0];
  514. }
  515. }
  516. if(count($bad_rcpt) > 0) {// Create error message
  517. for($i = 0; $i < count($bad_rcpt);$i++) {
  518. if($i != 0) {
  519. $error .= ', ';
  520. }
  521. $error .= $bad_rcpt[$i];
  522. }
  523. $error = $this->Lang('recipients_failed') .$error;
  524. $this->SetError($error);
  525. $this->smtp->Reset();
  526. return false;
  527. }
  528. if(!$this->smtp->Data($header .$body)) {
  529. $this->SetError($this->Lang('data_not_accepted'));
  530. $this->smtp->Reset();
  531. return false;
  532. }
  533. if($this->SMTPKeepAlive == true) {
  534. $this->smtp->Reset();
  535. } else {
  536. $this->SmtpClose();
  537. }
  538. return true;
  539. }
  540. /**
  541. * Initiates a connection to an SMTP server. Returns false if the
  542. * operation failed.
  543. * @access private
  544. * @return bool
  545. */
  546. function SmtpConnect() {
  547. if($this->smtp == NULL) {
  548. $this->smtp = new SMTP();
  549. }
  550. $this->smtp->do_debug = $this->SMTPDebug;
  551. $hosts = explode(';',$this->Host);
  552. $index = 0;
  553. $connection = ($this->smtp->Connected());
  554. /* Retry while there is no connection */
  555. while($index <count($hosts) &&$connection == false) {
  556. $hostinfo = array();
  557. if(eregi('^(.+):([0-9]+)$',$hosts[$index],$hostinfo)) {
  558. $host = $hostinfo[1];
  559. $port = $hostinfo[2];
  560. } else {
  561. $host = $hosts[$index];
  562. $port = $this->Port;
  563. }
  564. if($this->smtp->Connect(((!emptyempty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host,$port,$this->Timeout)) {
  565. if ($this->Helo != '') {
  566. $this->smtp->Hello($this->Helo);
  567. } else {
  568. $this->smtp->Hello($this->ServerHostname());
  569. }
  570. $connection = true;
  571. if($this->SMTPAuth) {
  572. if(!$this->smtp->Authenticate($this->Username,$this->Password)) {
  573. $this->SetError($this->Lang('authenticate'));
  574. $this->smtp->Reset();
  575. $connection = false;
  576. }
  577. }
  578. }
  579. $index++;
  580. }
  581. if(!$connection) {
  582. $this->SetError($this->Lang('connect_host'));
  583. }
  584. return $connection;
  585. }
  586. /**
  587. * Closes the active SMTP session if one exists.
  588. * @return void
  589. * 说明:如果有活动的SMTP则关闭它。
  590. */
  591. function SmtpClose() {
  592. if($this->smtp != NULL) {
  593. if($this->smtp->Connected()) {
  594. $this->smtp->Quit();
  595. $this->smtp->Close();
  596. }
  597. }
  598. }
  599. /**
  600. * Sets the language for all class error messages. Returns false
  601. * if it cannot load the language file. The default language type
  602. * is English.
  603. * @param string $lang_type Type of language (e.g. Portuguese: "br")
  604. * @param string $lang_path Path to the language file directory
  605. * @access public
  606. * @return bool
  607. * 说明:设置phpmailer错误信息的语言类型,如果无法加载语言文件,则返回false,默认为english
  608. */
  609. function SetLanguage($lang_type,$lang_path ='language/') {
  610. if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {
  611. include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  612. } elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {
  613. include($lang_path.'phpmailer.lang-en.php');
  614. } else {
  615. $PHPMAILER_LANG = array();
  616. $PHPMAILER_LANG["provide_address"] ='You must provide at least one ' .
  617. $PHPMAILER_LANG["mailer_not_supported"] =' mailer is not supported.';
  618. $PHPMAILER_LANG["execute"] ='Could not execute: ';
  619. $PHPMAILER_LANG["instantiate"] ='Could not instantiate mail function.';
  620. $PHPMAILER_LANG["authenticate"] ='SMTP Error: Could not authenticate.';
  621. $PHPMAILER_LANG["from_failed"] ='The following From address failed: ';
  622. $PHPMAILER_LANG["recipients_failed"] ='SMTP Error: The following ' .
  623. $PHPMAILER_LANG["data_not_accepted"] ='SMTP Error: Data not accepted.';
  624. $PHPMAILER_LANG["connect_host"] ='SMTP Error: Could not connect to SMTP host.';
  625. $PHPMAILER_LANG["file_access"] ='Could not access file: ';
  626. $PHPMAILER_LANG["file_open"] ='File Error: Could not open file: ';
  627. $PHPMAILER_LANG["encoding"] ='Unknown encoding: ';
  628. $PHPMAILER_LANG["signing"] ='Signing Error: ';
  629. }
  630. $this->language = $PHPMAILER_LANG;
  631. return true;
  632. }
  633. /////////////////////////////////////////////////
  634. // METHODS, MESSAGE CREATION
  635. /////////////////////////////////////////////////
  636. /**
  637. * Creates recipient headers.
  638. * @access private
  639. * @return string
  640. */
  641. function AddrAppend($type,$addr) {
  642. $addr_str = $type . ': ';
  643. $addr_str .= $this->AddrFormat($addr[0]);
  644. if(count($addr) > 1) {
  645. for($i = 1;$i <count($addr);$i++) {
  646. $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
  647. }
  648. }
  649. $addr_str .= $this->LE;
  650. return $addr_str;
  651. }
  652. /**
  653. * Formats an address correctly.
  654. * @access private
  655. * @return string
  656. */
  657. function AddrFormat($addr) {
  658. if(emptyempty($addr[1])) {
  659. $formatted = $this->SecureHeader($addr[0]);
  660. } else {
  661. $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]),'phrase') ." <" . $this->SecureHeader($addr[0]) .">";
  662. }
  663. return $formatted;
  664. }
  665. /**
  666. * Wraps message for use with mailers that do not
  667. * automatically perform wrapping and for quoted-printable.
  668. * Original written by philippe.
  669. * @access private
  670. * @return string
  671. */
  672. function WrapText($message,$length,$qp_mode = false) {
  673. $soft_break = ($qp_mode) ? sprintf(" =%s",$this->LE) :$this->LE;
  674. // If utf-8 encoding is used, we will need to make sure we don't
  675. // split multibyte characters when we wrap
  676. $is_utf8 = (strtolower($this->CharSet) =="utf-8");
  677. $message = $this->FixEOL($message);
  678. if (substr($message, -1) ==$this->LE) {
  679. $message = substr($message, 0, -1);
  680. }
  681. $line = explode($this->LE,$message);
  682. $message = '';
  683. for ($i=0 ;$i <count($line);$i++) {
  684. $line_part = explode(' ',$line[$i]);
  685. $buf = '';
  686. for ($e = 0; $e<count($line_part); $e++) {
  687. $word = $line_part[$e];
  688. if ($qp_mode and (strlen($word) > $length)) {
  689. $space_left = $length -strlen($buf) - 1;
  690. if ($e != 0) {
  691. if ($space_left > 20) {
  692. $len = $space_left;
  693. if ($is_utf8) {
  694. $len = $this->UTF8CharBoundary($word,$len);
  695. } elseif (substr($word,$len - 1, 1) =="=") {
  696. $len--;
  697. } elseif (substr($word,$len - 2, 1) =="=") {
  698. $len -= 2;
  699. }
  700. $part = substr($word, 0,$len);
  701. $word = substr($word,$len);
  702. $buf .= ' ' . $part;
  703. $message .= $buf . sprintf("=%s",$this->LE);
  704. } else {
  705. $message .= $buf .$soft_break;
  706. }
  707. $buf = '';
  708. }
  709. while (strlen($word) > 0) {
  710. $len = $length;
  711. if ($is_utf8) {
  712. $len = $this->UTF8CharBoundary($word,$len);
  713. } elseif (substr($word,$len - 1, 1) =="=") {
  714. $len--;
  715. } elseif (substr($word,$len - 2, 1) =="=") {
  716. $len -= 2;
  717. }
  718. $part = substr($word, 0,$len);
  719. $word = substr($word,$len);
  720. if (strlen($word) > 0) {
  721. $message .= $part . sprintf("=%s",$this->LE);
  722. } else {
  723. $buf = $part;
  724. }
  725. }
  726. } else {
  727. $buf_o = $buf;
  728. $buf .= ($e == 0) ?$word : (' ' .$word);
  729. if (strlen($buf) >$lengthand $buf_o != '') {
  730. $message .= $buf_o . $soft_break;
  731. $buf = $word;
  732. }
  733. }
  734. }
  735. $message .= $buf .$this->LE;
  736. }
  737. return $message;
  738. }
  739. /**
  740. * Finds last character boundary prior to maxLength in a utf-8
  741. * quoted (printable) encoded string.
  742. * Original written by Colin Brown.
  743. * @access private
  744. * @param string $encodedText utf-8 QP text
  745. * @param int $maxLength find last character boundary prior to this length
  746. * @return int
  747. */
  748. function UTF8CharBoundary($encodedText,$maxLength) {
  749. $foundSplitPos = false;
  750. $lookBack = 3;
  751. while (!$foundSplitPos) {
  752. $lastChunk = substr($encodedText,$maxLength -$lookBack, $lookBack);
  753. $encodedCharPos = strpos($lastChunk,"=");
  754. if ($encodedCharPos !== false) {
  755. // Found start of encoded character byte within $lookBack block.
  756. // Check the encoded byte value (the 2 chars after the '=')
  757. $hex = substr($encodedText,$maxLength -$lookBack + $encodedCharPos + 1, 2);
  758. $dec = hexdec($hex);
  759. if ($dec < 128) {// Single byte character.
  760. // If the encoded char was found at pos 0, it will fit
  761. // otherwise reduce maxLength to start of the encoded char
  762. $maxLength = ($encodedCharPos == 0) ?$maxLength :
  763. $maxLength - ($lookBack -$encodedCharPos);
  764. $foundSplitPos = true;
  765. } elseif ($dec >= 192) {// First byte of a multi byte character
  766. // Reduce maxLength to split at start of character
  767. $maxLength = $maxLength - ($lookBack -$encodedCharPos);
  768. $foundSplitPos = true;
  769. } elseif ($dec < 192) {// Middle byte of a multi byte character, look further back
  770. $lookBack += 3;
  771. }
  772. } else {
  773. // No encoded character found
  774. $foundSplitPos = true;
  775. }
  776. }
  777. return $maxLength;
  778. }
  779. /**
  780. * Set the body wrapping.
  781. * @access private
  782. * @return void
  783. */
  784. function SetWordWrap() {
  785. if($this->WordWrap < 1) {
  786. return;
  787. }
  788. switch($this->message_type) {
  789. case 'alt':
  790. /* fall through */
  791. case 'alt_attachments':
  792. $this->AltBody = $this->WrapText($this->AltBody,$this->WordWrap);
  793. break;
  794. default:
  795. $this->Body = $this->WrapText($this->Body,$this->WordWrap);
  796. break;
  797. }
  798. }
  799. /**
  800. * Assembles message header.
  801. * @access private
  802. * @return string
  803. */
  804. function CreateHeader() {
  805. $result = '';
  806. /* Set the boundaries */
  807. $uniq_id = md5(uniqid(time()));
  808. $this->boundary[1] = 'b1_' .$uniq_id;
  809. $this->boundary[2] = 'b2_' . $uniq_id;
  810. $result .= $this->HeaderLine('Date',$this->RFCDate());
  811. if($this->Sender == '') {
  812. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  813. } else {
  814. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  815. }
  816. /* To be created automatically by mail() */
  817. if($this->Mailer !='mail') {
  818. if(count($this->to) > 0) {
  819. $result .= $this->AddrAppend('To',$this->to);
  820. } elseif (count($this->cc) == 0) {
  821. $result .= $this->HeaderLine('To','undisclosed-recipients:;');
  822. }
  823. }
  824. $from = array();
  825. $from[0][0] = trim($this->From);
  826. $from[0][1] = $this->FromName;
  827. $result .= $this->AddrAppend('From',$from);
  828. /* sendmail and mail() extract Cc from the header before sending */
  829. if((($this->Mailer =='sendmail') || ($this->Mailer =='mail')) && (count($this->cc) > 0)) {
  830. $result .= $this->AddrAppend('Cc',$this->cc);
  831. }
  832. /* sendmail and mail() extract Bcc from the header before sending */
  833. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  834. $result .= $this->AddrAppend('Bcc',$this->bcc);
  835. }
  836. if(count($this->ReplyTo) > 0) {
  837. $result .= $this->AddrAppend('Reply-To',$this->ReplyTo);
  838. }
  839. /* mail() sets the subject itself */
  840. if($this->Mailer !='mail') {
  841. $result .= $this->HeaderLine('Subject',$this->EncodeHeader($this->SecureHeader($this->Subject)));
  842. }
  843. if($this->MessageID !='') {
  844. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  845. } else {
  846. $result .= sprintf("Message-ID:

class.smtp.php 文件

[php] view plaincopyprint?
  1. <?php
  2. /*~ class.smtp.php 说明:SMTP功能的对象
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 2.0.4 |
  6. | Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
  7. | Info: http://phpmailer.sourceforge.net |
  8. | Support: http://sourceforge.net/projects/phpmailer/ |
  9. | ------------------------------------------------------------------------- |
  10. | Author: Andy Prevost (project admininistrator) |
  11. | Author: Brent R. Matzelle (original founder) |
  12. | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
  13. | Copyright (c) 2001-2003, Brent R. Matzelle |
  14. | ------------------------------------------------------------------------- |
  15. | License: Distributed under the Lesser General Public License (LGPL) |
  16. | http://www.gnu.org/copyleft/lesser.html |
  17. | This program is distributed in the hope that it will be useful - WITHOUT |
  18. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  19. | FITNESS FOR A PARTICULAR PURPOSE. |
  20. | ------------------------------------------------------------------------- |
  21. | We offer a number of paid services (www.codeworxtech.com): |
  22. | - Web Hosting on highly optimized fast and secure servers |
  23. | - Technology Consulting |
  24. | - Oursourcing (highly qualified programmers and graphic designers) |
  25. '---------------------------------------------------------------------------'
  26. /**
  27. * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  28. * commands except TURN which will always return a not implemented
  29. * error. SMTP also provides some utility methods for sending mail
  30. * to an SMTP server.
  31. * @package PHPMailer
  32. * @author Chris Ryan
  33. */
  34. class SMTP
  35. {
  36. /**
  37. * SMTP server port
  38. * @var int
  39. * 说明:设置SMTP端口
  40. */
  41. var $SMTP_PORT = 25;
  42. /**
  43. * SMTP reply line ending
  44. * @var string
  45. * 说明:SMTP回复结束的分隔符(SMTP reply line ending?)
  46. */
  47. var $CRLF = "/r/n";
  48. /**
  49. * Sets whether debugging is turned on
  50. * @var bool
  51. * 说明:SMTP调试输出
  52. */
  53. var $do_debug; # the level of debug to perform
  54. /**
  55. * Sets VERP use on/off (default is off)
  56. * @var bool
  57. */
  58. var $do_verp = false;
  59. /**#@+
  60. * @access private
  61. */
  62. var $smtp_conn; # the socket to the server
  63. var $error; # errorif any on the last call
  64. var $helo_rply; # the reply the server sent to usfor HELO
  65. /**#@-*/
  66. /**
  67. * Initialize the class so that the data is in a known state.
  68. * @access public
  69. * @return void
  70. * 说明:初始化一个对象以便数据处于一个已知的状态
  71. */
  72. function SMTP() {
  73. $this->smtp_conn = 0;
  74. $this->error = null;
  75. $this->helo_rply = null;
  76. $this->do_debug = 0;
  77. }
  78. /*************************************************************
  79. * CONNECTION FUNCTIONS *
  80. ***********************************************************/
  81. /**
  82. * Connect to the server specified on the port specified.
  83. * If the port is not specified use the default SMTP_PORT.
  84. * If tval is specified then a connection will try and be
  85. * established with the server for that number of seconds.
  86. * If tval is not specified the default is 30 seconds to
  87. * try on the connection.
  88. *
  89. * SMTP CODE SUCCESS: 220
  90. * SMTP CODE FAILURE: 421
  91. * @access public
  92. * @return bool
  93. * 说明:建立一个SMTP连接[/color]Mailer.html
  94. */
  95. function Connect($host,$port=0,$tval=30) {
  96. # set the error val to null so there is no confusion
  97. $this->error = null;
  98. # make sure we are __not__ connected
  99. if($this->connected()) {
  100. # ok we are connected! what should we do?
  101. # for now we will just give an error saying we
  102. # are already connected
  103. $this->error = array("error" =>"Already connected to a server");
  104. return false;
  105. }
  106. if(emptyempty($port)) {
  107. $port = $this->SMTP_PORT;
  108. }
  109. #connect to the smtp server
  110. $this->smtp_conn = fsockopen($host, # the host of the server
  111. $port, # the port to use
  112. $errno, # error number if any
  113. $errstr, # error message if any
  114. $tval); # give up after ? secs
  115. # verify we connected properly
  116. if(emptyempty($this->smtp_conn)) {
  117. $this->error = array("error" =>"Failed to connect to server",
  118. "errno" => $errno,
  119. "errstr" => $errstr);
  120. if($this->do_debug >= 1) {
  121. echo "SMTP -> ERROR: " .$this->error["error"] .
  122. ": $errstr ($errno)" . $this->CRLF;
  123. }
  124. return false;
  125. }
  126. # sometimes the SMTP server takes a little longer to respond
  127. # so we will give it a longer timeout for the first read
  128. // Windows still does not have support for this timeout function
  129. if(substr(PHP_OS, 0, 3) !="WIN")
  130. socket_set_timeout($this->smtp_conn, $tval, 0);
  131. # get any announcement stuff
  132. $announce = $this->get_lines();
  133. # set the timeout of any socket functions at 1/10 of a second
  134. //if(function_exists("socket_set_timeout"))
  135. // socket_set_timeout($this->smtp_conn, 0, 100000);
  136. if($this->do_debug >= 2) {
  137. echo "SMTP -> FROM SERVER:" .$this->CRLF .$announce;
  138. }
  139. return true;
  140. }
  141. /**
  142. * Performs SMTP authentication. Must be run after running the
  143. * Hello() method. Returns true if successfully authenticated.
  144. * @access public
  145. * @return bool
  146. * 说明:开始SMTP认证,必须在Hello()之后调用,如果认证成功,返回true,
  147. * 参数1用户名,参数2密码
  148. */
  149. function Authenticate($username,$password) {
  150. // Start authentication
  151. fputs($this->smtp_conn,"AUTH LOGIN" .$this->CRLF);
  152. $rply = $this->get_lines();
  153. $code = substr($rply,0,3);
  154. if($code != 334) {
  155. $this->error =
  156. array("error" =>"AUTH not accepted from server",
  157. "smtp_code" => $code,
  158. "smtp_msg" => substr($rply,4));
  159. if($this->do_debug >= 1) {
  160. echo "SMTP -> ERROR: " .$this->error["error"] .
  161. ": " . $rply . $this->CRLF;
  162. }
  163. return false;
  164. }
  165. // Send encoded username
  166. fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  167. $rply = $this->get_lines();
  168. $code = substr($rply,0,3);
  169. if($code != 334) {
  170. $this->error =
  171. array("error" =>"Username not accepted from server",
  172. "smtp_code" => $code,
  173. "smtp_msg" => substr($rply,4));
  174. if($this->do_debug >= 1) {
  175. echo "SMTP -> ERROR: " .$this->error["error"] .
  176. ": " . $rply . $this->CRLF;
  177. }
  178. return false;
  179. }
  180. // Send encoded password
  181. fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  182. $rply = $this->get_lines();
  183. $code = substr($rply,0,3);
  184. if($code != 235) {
  185. $this->error =
  186. array("error" =>"Password not accepted from server",
  187. "smtp_code" => $code,
  188. "smtp_msg" => substr($rply,4));
  189. if($this->do_debug >= 1) {
  190. echo "SMTP -> ERROR: " .$this->error["error"] .
  191. ": " . $rply . $this->CRLF;
  192. }
  193. return false;
  194. }
  195. return true;
  196. }
  197. /**
  198. * Returns true if connected to a server otherwise false
  199. * @access private
  200. * @return bool
  201. */
  202. function Connected() {
  203. if(!emptyempty($this->smtp_conn)) {
  204. $sock_status = socket_get_status($this->smtp_conn);
  205. if($sock_status["eof"]) {
  206. # hmm this is an odd situation... the socket is
  207. # valid but we are not connected anymore
  208. if($this->do_debug >= 1) {
  209. echo "SMTP -> NOTICE:" .$this->CRLF .
  210. "EOF caught while checking if connected";
  211. }
  212. $this->Close();
  213. return false;
  214. }
  215. return true; # everything looks good
  216. }
  217. return false;
  218. }
  219. /**
  220. * Closes the socket and cleans up the state of the class.
  221. * It is not considered good to use this function without
  222. * first trying to use QUIT.
  223. * @access public
  224. * @return void
  225. * 说明:关闭一个SMTP连接
  226. */
  227. function Close() {
  228. $this->error = null; # so there is no confusion
  229. $this->helo_rply = null;
  230. if(!emptyempty($this->smtp_conn)) {
  231. # close the connection and cleanup
  232. fclose($this->smtp_conn);
  233. $this->smtp_conn = 0;
  234. }
  235. }
  236. /***************************************************************
  237. * SMTP COMMANDS *
  238. *************************************************************/
  239. /**
  240. * Issues a data command and sends the msg_data to the server
  241. * finializing the mail transaction. $msg_data is the message
  242. * that is to be send with the headers. Each header needs to be
  243. * on a single line followed by a <CRLF> with the message headers
  244. * and the message body being seperated by and additional <CRLF>.
  245. *
  246. * Implements rfc 821: DATA <CRLF>
  247. *
  248. * SMTP CODE INTERMEDIATE: 354
  249. * [data]
  250. * <CRLF>.<CRLF>
  251. * SMTP CODE SUCCESS: 250
  252. * SMTP CODE FAILURE: 552,554,451,452
  253. * SMTP CODE FAILURE: 451,554
  254. * SMTP CODE ERROR : 500,501,503,421
  255. * @access public
  256. * @return bool
  257. * 说明:向服务器发送一个数据命令和消息信息(sendsthemsg_datatotheserver)
  258. */
  259. function Data($msg_data) {
  260. $this->error = null; # so no confusion is caused
  261. if(!$this->connected()) {
  262. $this->error = array(
  263. "error" => "Called Data() without being connected");
  264. return false;
  265. }
  266. fputs($this->smtp_conn,"DATA" .$this->CRLF);
  267. $rply = $this->get_lines();
  268. $code = substr($rply,0,3);
  269. if($this->do_debug >= 2) {
  270. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  271. }
  272. if($code != 354) {
  273. $this->error =
  274. array("error" =>"DATA command not accepted from server",
  275. "smtp_code" => $code,
  276. "smtp_msg" => substr($rply,4));
  277. if($this->do_debug >= 1) {
  278. echo "SMTP -> ERROR: " .$this->error["error"] .
  279. ": " . $rply . $this->CRLF;
  280. }
  281. return false;
  282. }
  283. # the server is ready to accept data!
  284. # according to rfc 821 we should not send more than 1000
  285. # including the CRLF
  286. # characters on a single line so we will break the data up
  287. # into lines by /r and/or /n thenif needed we willbreak
  288. # each of those into smaller lines to fit within the limit.
  289. # in addition we will be looking for lines that start with
  290. # a period '.' and appendand additional period'.' to that
  291. # line. NOTE: this does not count towards are limit.
  292. # normalize the line breaks so we know the explode works
  293. $msg_data = str_replace("/r/n","/n",$msg_data);
  294. $msg_data = str_replace("/r","/n",$msg_data);
  295. $lines = explode("/n",$msg_data);
  296. # we need to find a good way to determine is headers are
  297. # in the msg_data or if it is a straight msg body
  298. # currently I am assuming rfc 822 definitions of msg headers
  299. # and if the first field of the first line (':' sperated)
  300. # does not contain a space then it _should_ be a header
  301. # and we can process all lines before a blank"" lineas
  302. # headers.
  303. $field = substr($lines[0],0,strpos($lines[0],":"));
  304. $in_headers = false;
  305. if(!emptyempty($field) && !strstr($field," ")) {
  306. $in_headers = true;
  307. }
  308. $max_line_length = 998; # used below; set herefor ease in change
  309. while(list(,$line) = @each($lines)) {
  310. $lines_out = null;
  311. if($line =="" &&$in_headers) {
  312. $in_headers = false;
  313. }
  314. # ok we need to break this line up into several
  315. # smaller lines
  316. while(strlen($line) >$max_line_length) {
  317. $pos = strrpos(substr($line,0,$max_line_length)," ");
  318. # Patch to fix DOS attack
  319. if(!$pos) {
  320. $pos = $max_line_length - 1;
  321. }
  322. $lines_out[] = substr($line,0,$pos);
  323. $line = substr($line,$pos + 1);
  324. # if we are processing headers we need to
  325. # add a LWSP-char to the front of the new line
  326. # rfc 822 on long msg headers
  327. if($in_headers) {
  328. $line = "/t" . $line;
  329. }
  330. }
  331. $lines_out[] = $line;
  332. # now send the lines to the server
  333. while(list(,$line_out) = @each($lines_out)) {
  334. if(strlen($line_out) > 0)
  335. {
  336. if(substr($line_out, 0, 1) ==".") {
  337. $line_out = "." . $line_out;
  338. }
  339. }
  340. fputs($this->smtp_conn,$line_out .$this->CRLF);
  341. }
  342. }
  343. # ok all the message data has been sent so lets get this
  344. # over with aleady
  345. fputs($this->smtp_conn,$this->CRLF ."." . $this->CRLF);
  346. $rply = $this->get_lines();
  347. $code = substr($rply,0,3);
  348. if($this->do_debug >= 2) {
  349. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  350. }
  351. if($code != 250) {
  352. $this->error =
  353. array("error" => "DATA not accepted from server",
  354. "smtp_code" => $code,
  355. "smtp_msg" => substr($rply,4));
  356. if($this->do_debug >= 1) {
  357. echo "SMTP -> ERROR: " .$this->error["error"] .
  358. ": " . $rply .$this->CRLF;
  359. }
  360. return false;
  361. }
  362. return true;
  363. }
  364. /**
  365. * Expand takes the name and asks the server to list all the
  366. * people who are members of the _list_. Expand will return
  367. * back and array of the result or false if an error occurs.
  368. * Each value in the array returned has the format of:
  369. * [ <full-name> <sp> ] <path>
  370. * The definition of <path> is defined in rfc 821
  371. *
  372. * Implements rfc 821: EXPN <SP> <string> <CRLF>
  373. *
  374. * SMTP CODE SUCCESS: 250
  375. * SMTP CODE FAILURE: 550
  376. * SMTP CODE ERROR : 500,501,502,504,421
  377. * @access public
  378. * @return string array
  379. * 说明:返回邮件列表中所有用户。成功则返回数组,否则返回 false(Expandtakesthenameandaskstheservertolistallthepeoplewhoaremembersofthe_list_.Expandwillreturnbackandarrayoftheresultorfalseifanerroroccurs.)
  380. */
  381. function Expand($name) {
  382. $this->error = null; # so no confusion is caused
  383. if(!$this->connected()) {
  384. $this->error = array(
  385. "error" => "Called Expand() without being connected");
  386. return false;
  387. }
  388. fputs($this->smtp_conn,"EXPN " .$name .$this->CRLF);
  389. $rply = $this->get_lines();
  390. $code = substr($rply,0,3);
  391. if($this->do_debug >= 2) {
  392. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  393. }
  394. if($code != 250) {
  395. $this->error =
  396. array("error" =>"EXPN not accepted from server",
  397. "smtp_code" => $code,
  398. "smtp_msg" => substr($rply,4));
  399. if($this->do_debug >= 1) {
  400. echo "SMTP -> ERROR: " .$this->error["error"] .
  401. ": " . $rply . $this->CRLF;
  402. }
  403. return false;
  404. }
  405. # parse the reply and place in our array to return to user
  406. $entries = explode($this->CRLF,$rply);
  407. while(list(,$l) = @each($entries)) {
  408. $list[] = substr($l,4);
  409. }
  410. return $list;
  411. }
  412. /**
  413. * Sends the HELO command to the smtp server.
  414. * This makes sure that we and the server are in
  415. * the same known state.
  416. *
  417. * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  418. *
  419. * SMTP CODE SUCCESS: 250
  420. * SMTP CODE ERROR : 500, 501, 504, 421
  421. * @access public
  422. * @return bool
  423. * 说明:向SMTP服务器发送HELO命令
  424. */
  425. function Hello($host="") {
  426. $this->error = null; # so no confusion is caused
  427. if(!$this->connected()) {
  428. $this->error = array(
  429. "error" => "Called Hello() without being connected");
  430. return false;
  431. }
  432. # if a hostname for the HELO was not specified determine
  433. # a suitable one to send
  434. if(emptyempty($host)) {
  435. # we need to determine some sort of appopiate default
  436. # to send to the server
  437. $host = "localhost";
  438. }
  439. // Send extended hello first (RFC 2821)
  440. if(!$this->SendHello("EHLO",$host))
  441. {
  442. if(!$this->SendHello("HELO",$host))
  443. return false;
  444. }
  445. return true;
  446. }
  447. /**
  448. * Sends a HELO/EHLO command.
  449. * @access private
  450. * @return bool
  451. */
  452. function SendHello($hello,$host) {
  453. fputs($this->smtp_conn, $hello . " " . $host .$this->CRLF);
  454. $rply = $this->get_lines();
  455. $code = substr($rply,0,3);
  456. if($this->do_debug >= 2) {
  457. echo "SMTP -> FROM SERVER: " .$this->CRLF .$rply;
  458. }
  459. if($code != 250) {
  460. $this->error =
  461. array("error" =>$hello ." not accepted from server",
  462. "smtp_code" => $code,
  463. "smtp_msg" => substr($rply,4));
  464. if($this->do_debug >= 1) {
  465. echo "SMTP -> ERROR: " .$this->error["error"] .
  466. ": " . $rply . $this->CRLF;
  467. }
  468. return false;
  469. }
  470. $this->helo_rply = $rply;
  471. return true;
  472. }
  473. /**
  474. * Gets help information on the keyword specified. If the keyword
  475. * is not specified then returns generic help, ussually contianing
  476. * A list of keywords that help is available on. This function
  477. * returns the results back to the user. It is up to the user to
  478. * handle the returned data. If an error occurs then false is
  479. * returned with $this->error set appropiately.
  480. *
  481. * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
  482. *
  483. * SMTP CODE SUCCESS: 211,214
  484. * SMTP CODE ERROR : 500,501,502,504,421
  485. * @access public
  486. * @return string
  487. * 说明:如果有关键词,得到关键词的帮助信息
  488. */
  489. function Help($keyword="") {
  490. $this->error = null; # to avoid confusion
  491. if(!$this->connected()) {
  492. $this->error = array(
  493. "error" => "Called Help() without being connected");
  494. return false;
  495. }
  496. $extra = "";
  497. if(!emptyempty($keyword)) {
  498. $extra = " " .$keyword;
  499. }
  500. fputs($this->smtp_conn,"HELP" .$extra .$this->CRLF);
  501. $rply = $this->get_lines();
  502. $code = substr($rply,0,3);
  503. if($this->do_debug >= 2) {
  504. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  505. }
  506. if($code != 211 &&$code != 214) {
  507. $this->error =
  508. array("error" =>"HELP not accepted from server",
  509. "smtp_code" => $code,
  510. "smtp_msg" => substr($rply,4));
  511. if($this->do_debug >= 1) {
  512. echo "SMTP -> ERROR: " .$this->error["error"] .
  513. ": " . $rply . $this->CRLF;
  514. }
  515. return false;
  516. }
  517. return $rply;
  518. }
  519. /**
  520. * Starts a mail transaction from the email address specified in
  521. * $from. Returns true if successful or false otherwise. If True
  522. * the mail transaction is started and then one or more Recipient
  523. * commands may be called followed by a Data command.
  524. *
  525. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  526. *
  527. * SMTP CODE SUCCESS: 250
  528. * SMTP CODE SUCCESS: 552,451,452
  529. * SMTP CODE SUCCESS: 500,501,421
  530. * @access public
  531. * @return bool
  532. * 说明:从$from中一个邮件地址开始处理,返回true或false。如果是true,则开始发件
  533. */
  534. function Mail($from) {
  535. $this->error = null; # so no confusion is caused
  536. if(!$this->connected()) {
  537. $this->error = array(
  538. "error" => "Called Mail() without being connected");
  539. return false;
  540. }
  541. $useVerp = ($this->do_verp ?"XVERP" :"");
  542. fputs($this->smtp_conn,"MAIL FROM:<" .$from .">" . $useVerp . $this->CRLF);
  543. $rply = $this->get_lines();
  544. $code = substr($rply,0,3);
  545. if($this->do_debug >= 2) {
  546. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  547. }
  548. if($code != 250) {
  549. $this->error =
  550. array("error" => "MAIL not accepted from server",
  551. "smtp_code" => $code,
  552. "smtp_msg" => substr($rply,4));
  553. if($this->do_debug >= 1) {
  554. echo "SMTP -> ERROR: " .$this->error["error"] .
  555. ": " . $rply .$this->CRLF;
  556. }
  557. return false;
  558. }
  559. return true;
  560. }
  561. /**
  562. * Sends the command NOOP to the SMTP server.
  563. *
  564. * Implements from rfc 821: NOOP <CRLF>
  565. *
  566. * SMTP CODE SUCCESS: 250
  567. * SMTP CODE ERROR : 500, 421
  568. * @access public
  569. * @return bool
  570. * 说明:向SMTP服务器发送一个NOOP命令
  571. */
  572. function Noop() {
  573. $this->error = null; # so no confusion is caused
  574. if(!$this->connected()) {
  575. $this->error = array(
  576. "error" => "Called Noop() without being connected");
  577. return false;
  578. }
  579. fputs($this->smtp_conn,"NOOP" .$this->CRLF);
  580. $rply = $this->get_lines();
  581. $code = substr($rply,0,3);
  582. if($this->do_debug >= 2) {
  583. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  584. }
  585. if($code != 250) {
  586. $this->error =
  587. array("error" =>"NOOP not accepted from server",
  588. "smtp_code" => $code,
  589. "smtp_msg" => substr($rply,4));
  590. if($this->do_debug >= 1) {
  591. echo "SMTP -> ERROR: " .$this->error["error"] .
  592. ": " . $rply . $this->CRLF;
  593. }
  594. return false;
  595. }
  596. return true;
  597. }
  598. /**
  599. * Sends the quit command to the server and then closes the socket
  600. * if there is no error or the $close_on_error argument is true.
  601. *
  602. * Implements from rfc 821: QUIT <CRLF>
  603. *
  604. * SMTP CODE SUCCESS: 221
  605. * SMTP CODE ERROR : 500
  606. * @access public
  607. * @return bool
  608. * 说明:向服务器发送Quit命令,如果没有错误发生。那么关闭sock,不然$close_on_error为true
  609. */
  610. function Quit($close_on_error=true) {
  611. $this->error = null; # so there is no confusion
  612. if(!$this->connected()) {
  613. $this->error = array(
  614. "error" => "Called Quit() without being connected");
  615. return false;
  616. }
  617. # send the quit command to the server
  618. fputs($this->smtp_conn,"quit" .$this->CRLF);
  619. # get any good-bye messages
  620. $byemsg = $this->get_lines();
  621. if($this->do_debug >= 2) {
  622. echo "SMTP -> FROM SERVER:" .$this->CRLF .$byemsg;
  623. }
  624. $rval = true;
  625. $e = null;
  626. $code = substr($byemsg,0,3);
  627. if($code != 221) {
  628. # use e as a tmpvar cause Close will overwrite$this->error
  629. $e = array("error" =>"SMTP server rejected quit command",
  630. "smtp_code" => $code,
  631. "smtp_rply" => substr($byemsg,4));
  632. $rval = false;
  633. if($this->do_debug >= 1) {
  634. echo "SMTP -> ERROR: " .$e["error"] .": " .
  635. $byemsg . $this->CRLF;
  636. }
  637. }
  638. if(emptyempty($e) || $close_on_error) {
  639. $this->Close();
  640. }
  641. return $rval;
  642. }
  643. /**
  644. * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  645. * Returns true if the recipient was accepted false if it was rejected.
  646. *
  647. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  648. *
  649. * SMTP CODE SUCCESS: 250,251
  650. * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  651. * SMTP CODE ERROR : 500,501,503,421
  652. * @access public
  653. * @return bool
  654. * 说明:使用To向SMTP发送RCPT命令,参数为:$to
  655. */
  656. function Recipient($to) {
  657. $this->error = null; # so no confusion is caused
  658. if(!$this->connected()) {
  659. $this->error = array(
  660. "error" => "Called Recipient() without being connected");
  661. return false;
  662. }
  663. fputs($this->smtp_conn,"RCPT TO:<" .$to .">" . $this->CRLF);
  664. $rply = $this->get_lines();
  665. $code = substr($rply,0,3);
  666. if($this->do_debug >= 2) {
  667. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  668. }
  669. if($code != 250 && $code != 251) {
  670. $this->error =
  671. array("error" => "RCPT not accepted from server",
  672. "smtp_code" => $code,
  673. "smtp_msg" => substr($rply,4));
  674. if($this->do_debug >= 1) {
  675. echo "SMTP -> ERROR: " .$this->error["error"] .
  676. ": " . $rply .$this->CRLF;
  677. }
  678. return false;
  679. }
  680. return true;
  681. }
  682. /**
  683. * Sends the RSET command to abort and transaction that is
  684. * currently in progress. Returns true if successful false
  685. * otherwise.
  686. *
  687. * Implements rfc 821: RSET <CRLF>
  688. *
  689. * SMTP CODE SUCCESS: 250
  690. * SMTP CODE ERROR : 500,501,504,421
  691. * @access public
  692. * @return bool
  693. * 说明:发送RSET命令从而取消处理中传输。成功则返回true,否则为false
  694. */
  695. function Reset() {
  696. $this->error = null; # so no confusion is caused
  697. if(!$this->connected()) {
  698. $this->error = array(
  699. "error" => "Called Reset() without being connected");
  700. return false;
  701. }
  702. fputs($this->smtp_conn,"RSET" .$this->CRLF);
  703. $rply = $this->get_lines();
  704. $code = substr($rply,0,3);
  705. if($this->do_debug >= 2) {
  706. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  707. }
  708. if($code != 250) {
  709. $this->error =
  710. array("error" =>"RSET failed",
  711. "smtp_code" => $code,
  712. "smtp_msg" => substr($rply,4));
  713. if($this->do_debug >= 1) {
  714. echo "SMTP -> ERROR: " .$this->error["error"] .
  715. ": " . $rply . $this->CRLF;
  716. }
  717. return false;
  718. }
  719. return true;
  720. }
  721. /**
  722. * Starts a mail transaction from the email address specified in
  723. * $from. Returns true if successful or false otherwise. If True
  724. * the mail transaction is started and then one or more Recipient
  725. * commands may be called followed by a Data command. This command
  726. * will send the message to the users terminal if they are logged
  727. * in.
  728. *
  729. * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
  730. *
  731. * SMTP CODE SUCCESS: 250
  732. * SMTP CODE SUCCESS: 552,451,452
  733. * SMTP CODE SUCCESS: 500,501,502,421
  734. * @access public
  735. * @return bool
  736. * 说明:从指定的邮件地址开始一个邮件传输
  737. */
  738. function Send($from) {
  739. $this->error = null; # so no confusion is caused
  740. if(!$this->connected()) {
  741. $this->error = array(
  742. "error" => "Called Send() without being connected");
  743. return false;
  744. }
  745. fputs($this->smtp_conn,"SEND FROM:" .$from .$this->CRLF);
  746. $rply = $this->get_lines();
  747. $code = substr($rply,0,3);
  748. if($this->do_debug >= 2) {
  749. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  750. }
  751. if($code != 250) {
  752. $this->error =
  753. array("error" =>"SEND not accepted from server",
  754. "smtp_code" => $code,
  755. "smtp_msg" => substr($rply,4));
  756. if($this->do_debug >= 1) {
  757. echo "SMTP -> ERROR: " .$this->error["error"] .
  758. ": " . $rply . $this->CRLF;
  759. }
  760. return false;
  761. }
  762. return true;
  763. }
  764. /**
  765. * Starts a mail transaction from the email address specified in
  766. * $from. Returns true if successful or false otherwise. If True
  767. * the mail transaction is started and then one or more Recipient
  768. * commands may be called followed by a Data command. This command
  769. * will send the message to the users terminal if they are logged
  770. * in and send them an email.
  771. *
  772. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  773. *
  774. * SMTP CODE SUCCESS: 250
  775. * SMTP CODE SUCCESS: 552,451,452
  776. * SMTP CODE SUCCESS: 500,501,502,421
  777. * @access public
  778. * @return bool
  779. * 说明:从指定的邮件地址开始一个邮件传输
  780. */
  781. function SendAndMail($from) {
  782. $this->error = null; # so no confusion is caused
  783. if(!$this->connected()) {
  784. $this->error = array(
  785. "error" => "Called SendAndMail() without being connected");
  786. return false;
  787. }
  788. fputs($this->smtp_conn,"SAML FROM:" .$from .$this->CRLF);
  789. $rply = $this->get_lines();
  790. $code = substr($rply,0,3);
  791. if($this->do_debug >= 2) {
  792. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  793. }
  794. if($code != 250) {
  795. $this->error =
  796. array("error" => "SAML not accepted from server",
  797. "smtp_code" => $code,
  798. "smtp_msg" => substr($rply,4));
  799. if($this->do_debug >= 1) {
  800. echo "SMTP -> ERROR: " .$this->error["error"] .
  801. ": " . $rply .$this->CRLF;
  802. }
  803. return false;
  804. }
  805. return true;
  806. }
  807. /**
  808. * Starts a mail transaction from the email address specified in
  809. * $from. Returns true if successful or false otherwise. If True
  810. * the mail transaction is started and then one or more Recipient
  811. * commands may be called followed by a Data command. This command
  812. * will send the message to the users terminal if they are logged
  813. * in or mail it to them if they are not.
  814. *
  815. * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
  816. *
  817. * SMTP CODE SUCCESS: 250
  818. * SMTP CODE SUCCESS: 552,451,452
  819. * SMTP CODE SUCCESS: 500,501,502,421
  820. * @access public
  821. * @return bool
  822. * 说明:从指定的邮件地址开始一个邮件传输
  823. */
  824. function SendOrMail($from) {
  825. $this->error = null; # so no confusion is caused
  826. if(!$this->connected()) {
  827. $this->error = array(
  828. "error" => "Called SendOrMail() without being connected");
  829. return false;
  830. }
  831. fputs($this->smtp_conn,"SOML FROM:" .$from .$this->CRLF);
  832. $rply = $this->get_lines();
  833. $code = substr($rply,0,3);
  834. if($this->do_debug >= 2) {
  835. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  836. }
  837. if($code != 250) {
  838. $this->error =
  839. array("error" =>"SOML not accepted from server",
  840. "smtp_code" => $code,
  841. "smtp_msg" => substr($rply,4));
  842. if($this->do_debug >= 1) {
  843. echo "SMTP -> ERROR: " .$this->error["error"] .
  844. ": " . $rply . $this->CRLF;
  845. }
  846. return false;
  847. }
  848. return true;
  849. }
  850. /**
  851. * This is an optional command for SMTP that this class does not
  852. * support. This method is here to make the RFC821 Definition
  853. * complete for this class and __may__ be implimented in the future
  854. *
  855. * Implements from rfc 821: TURN <CRLF>
  856. *
  857. * SMTP CODE SUCCESS: 250
  858. * SMTP CODE FAILURE: 502
  859. * SMTP CODE ERROR : 500, 503
  860. * @access public
  861. * @return bool
  862. * 说明:这是一个可选的SMTP参数,目前phpmailer并不支持他,可能未来支持
  863. */
  864. function Turn() {
  865. $this->error = array("error" =>"This method, TURN, of the SMTP ".
  866. "is not implemented");
  867. if($this->do_debug >= 1) {
  868. echo "SMTP -> NOTICE: " .$this->error["error"] .$this->CRLF;
  869. }
  870. return false;
  871. }
  872. /**
  873. * Verifies that the name is recognized by the server.
  874. * Returns false if the name could not be verified otherwise
  875. * the response from the server is returned.
  876. *
  877. * Implements rfc 821: VRFY <SP> <string> <CRLF>
  878. *
  879. * SMTP CODE SUCCESS: 250,251
  880. * SMTP CODE FAILURE: 550,551,553
  881. * SMTP CODE ERROR : 500,501,502,421
  882. * @access public
  883. * @return int
  884. * 说明:通过服务器检查用户名是否经过验证
  885. */
  886. function Verify($name) {
  887. $this->error = null; # so no confusion is caused
  888. if(!$this->connected()) {
  889. $this->error = array(
  890. "error" => "Called Verify() without being connected");
  891. return false;
  892. }
  893. fputs($this->smtp_conn,"VRFY " .$name .$this->CRLF);
  894. $rply = $this->get_lines();
  895. $code = substr($rply,0,3);
  896. if($this->do_debug >= 2) {
  897. echo "SMTP -> FROM SERVER:" .$this->CRLF .$rply;
  898. }
  899. if($code != 250 && $code != 251) {
  900. $this->error =
  901. array("error" => "VRFY failed on name '$name'",
  902. "smtp_code" => $code,
  903. "smtp_msg" => substr($rply,4));
  904. if($this->do_debug >= 1) {
  905. echo "SMTP -> ERROR: " .$this->error["error"] .
  906. ": " . $rply .$this->CRLF;
  907. }
  908. return false;
  909. }
  910. return $rply;
  911. }
  912. /*******************************************************************
  913. * INTERNAL FUNCTIONS *
  914. ******************************************************************/
  915. /**
  916. * Read in as many lines as possible
  917. * either before eof or socket timeout occurs on the operation.
  918. * With SMTP we can tell if we have more lines to read if the
  919. * 4th character is '-' symbol. If it is a space then we don't
  920. * need to read anything else.
  921. * @access private
  922. * @return string
  923. */
  924. function get_lines() {
  925. $data = "";
  926. while($str = @fgets($this->smtp_conn,515)) {
  927. if($this->do_debug >= 4) {
  928. echo "SMTP -> get_lines(): /$data was /"$data/"" .
  929. $this->CRLF;
  930. echo "SMTP -> get_lines(): /$str is /"$str/"" .
  931. $this->CRLF;
  932. }
  933. $data .= $str;
  934. if($this->do_debug >= 4) {
  935. echo "SMTP -> get_lines(): /$data is /"$data/"" .$this->CRLF;
  936. }
  937. # if the 4th character is a space then we are done reading
  938. # so just break the loop
  939. if(substr($str,3,1) ==" ") {break; }
  940. }
  941. return $data;
  942. }
  943. }
  944. ?>

0 0
原创粉丝点击