您的位置:首页 > 编程语言 > PHP开发

PHP SMTP发送邮件

2008-12-10 15:36 399 查看
<?php

2class smtp

3{

4/* Public Variables */

5var $smtp_port;

6var $time_out;

7var $host_name;

8var $log_file;

9var $relay_host;

10var $debug;

11var $auth;

12var $user;

13var $pass;

14/* Private Variables */

15var $sock;

16/* Constractor */

17function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)

18{

19$this->debug = FALSE;

20$this->smtp_port = $smtp_port;

21$this->relay_host = $relay_host;

22$this->time_out = 30; //is used in fsockopen()

23#

24$this->auth = $auth;//auth

25$this->user = $user;

26$this->pass = $pass;

27#

28$this->host_name = "localhost"; //is used in HELO command

29$this->log_file = "";

30$this->sock = FALSE;

31}

32/* Main Function */

33function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

34{

35$mail_from = $this->get_address($this->strip_comment($from));

36$body = ereg_replace("(^(\r\n))(\.)", "\1.\3", $body);

37$header = "MIME-Version:1.0\r\n";

38if($mailtype=="HTML"){

39$header .= "Content-Type:text/html\r\n";

40}

41$header .= "To: ".$to."\r\n";

42if ($cc != "") {

43$header .= "Cc: ".$cc."\r\n";

44}

45$header .= "From: $from<".$from.">\r\n";

46$header .= "Subject: ".$subject."\r\n";

47$header .= $additional_headers;

48$header .= "Date: ".date("r")."\r\n";

49$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";

50list($msec, $sec) = explode(" ", microtime());

51$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";

52$TO = explode(",", $this->strip_comment($to));

53if ($cc != "") {

54$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));

55}

56if ($bcc != "") {

57$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));

58}

59$sent = TRUE;

60foreach ($TO as $rcpt_to) {

61$rcpt_to = $this->get_address($rcpt_to);

62if (!$this->smtp_sockopen($rcpt_to)) {

63$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");

64$sent = FALSE;

65continue;

66}

67if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {

68$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");

69} else {

70$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");

71$sent = FALSE;

72}

73fclose($this->sock);

74$this->log_write("Disconnected from remote host\n");

75}

76return $sent;

77}

78/* Private Functions */

79function smtp_send($helo, $from, $to, $header, $body = "")

80{

81if (!$this->smtp_putcmd("HELO", $helo)) {

82return $this->smtp_error("sending HELO command");

83}

84#auth

85if($this->auth){

86if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {

87return $this->smtp_error("sending HELO command");

88}

89if (!$this->smtp_putcmd("", base64_encode($this->pass))) {

90return $this->smtp_error("sending HELO command");

91}

92}

93#

94if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {

95return $this->smtp_error("sending MAIL FROM command");

96}

97if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {

98return $this->smtp_error("sending RCPT TO command");

99}

100if (!$this->smtp_putcmd("DATA")) {

101return $this->smtp_error("sending DATA command");

102}

103if (!$this->smtp_message($header, $body)) {

104return $this->smtp_error("sending message");

105}

106if (!$this->smtp_eom()) {

107return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");

108}

109if (!$this->smtp_putcmd("QUIT")) {

110return $this->smtp_error("sending QUIT command");

111}

112return TRUE;

113}

114function smtp_sockopen($address)

115{

116if ($this->relay_host == "") {

117return $this->smtp_sockopen_mx($address);

118} else {

119return $this->smtp_sockopen_relay();

120}

121}

122function smtp_sockopen_relay()

123{

124$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");

125$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);

126if (!($this->sock && $this->smtp_ok())) {

127$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");

128$this->log_write("Error: ".$errstr." (".$errno.")\n");

129return FALSE;

130}

131$this->log_write("Connected to relay host ".$this->relay_host."\n");

132return TRUE;

133}

134

135function smtp_sockopen_mx($address)

136{

137$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);

138if (!@getmxrr($domain, $MXHOSTS)) {

139$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");

140return FALSE;

141}

142foreach ($MXHOSTS as $host) {

143$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");

144$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);

145if (!($this->sock && $this->smtp_ok())) {

146$this->log_write("Warning: Cannot connect to mx host ".$host."\n");

147$this->log_write("Error: ".$errstr." (".$errno.")\n");

148continue;

149}

150$this->log_write("Connected to mx host ".$host."\n");

151return TRUE;

152}

153$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");

154return FALSE;

155}

156

157function smtp_message($header, $body)

158{

159fputs($this->sock, $header."\r\n".$body);

160$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));

161return TRUE;

162}

163

164function smtp_eom()

165{

166fputs($this->sock, "\r\n.\r\n");

167$this->smtp_debug(". [EOM]\n");

168return $this->smtp_ok();

169}

170

171function smtp_ok()

172{

173$response = str_replace("\r\n", "", fgets($this->sock, 512));

174$this->smtp_debug($response."\n");

175if (!ereg("^[23]", $response)) {

176fputs($this->sock, "QUIT\r\n");

177fgets($this->sock, 512);

178$this->log_write("Error: Remote host returned \"".$response."\"\n");

179return FALSE;

180}

181return TRUE;

182}

183

184function smtp_putcmd($cmd, $arg = "")

185{

186if ($arg != "") {

187if($cmd=="") $cmd = $arg;

188else $cmd = $cmd." ".$arg;

189}

190fputs($this->sock, $cmd."\r\n");

191$this->smtp_debug("> ".$cmd."\n");

192return $this->smtp_ok();

193}

194

195function smtp_error($string)

196{

197$this->log_write("Error: Error occurred while ".$string.".\n");

198return FALSE;

199}

200

201function log_write($message)

202{

203$this->smtp_debug($message);

204if ($this->log_file == "") {

205return TRUE;

206}

207$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;

208if (!@file_exists($this->log_file) ||($fp = @fopen($this->log_file, "a"))) {

209$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");

210return FALSE;;

211}

212flock($fp, LOCK_EX);

213fputs($fp, $message);

214fclose($fp);

215return TRUE;

216}

217

218function strip_comment($address)

219{

220$comment = "\([^()]*\)";

221while (ereg($comment, $address)) {

222$address = ereg_replace($comment, "", $address);

223}

224return $address;

225}

226

227function get_address($address)

228{

229$address = ereg_replace("([ \t\r\n])+", "", $address);

230$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);

231return $address;

232}

233

234function smtp_debug($message)

235{

236if ($this->debug) {

237echo $message;

238}

239}

240}

241?>
<?

2require (FILE_DIR."smtp.php");

3$smtpserver = "smtp.xxx.com";//SMTP服务器

4$smtpserverport =25;//SMTP服务器端口

5$smtpusermail = "xxxxx@xxx.com";//SMTP服务器的用户邮箱

6$smtpemailto = "aaaaa@xxx.com";//发送给谁

7$smtpuser = "xxxxx";//SMTP服务器的用户帐号

8$smtppass = "xxxxx";//SMTP服务器的用户密码

9$mailsubject = "Test Subject";//邮件主题

10$mailbody = "<h1>This is a test mail</h1>";//邮件内容

11$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件

12$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.

13$smtp->debug = TRUE;//是否显示发送的调试信息

14$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

15?>

来源:http://blog.163.com/gu__zeng/blog/static/137631252008850025353/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: