php 做注册邮件发送成功

01.$smtpserver = "smtp.163.com";//SMTP服务器  
02.        $smtpserverport =25;//SMTP服务器端口  
03.        $smtpusermail = "haolijun206@163.com";//SMTP服务器的用户邮箱  
04.        $smtpemailto = $email;//发送给谁  
05.        $smtpuser = "haolijun206@163.com";//SMTP服务器的用户帐号  
06.        $smtppass = "523132661";//SMTP服务器的用户密码  
07.        $mailsubject = "第一企业,注册成功";//邮件主题  
08.        $mailbody = file_get_contents("email.html");//读取html文件  
09.        $mailbody = str_replace("{name}",$username,$mailbody);//替换变量  
10.        $mailbody = str_replace("{email}",$email,$mailbody);  
11.        $mailbody = str_replace("{password}",$password,$mailbody);  
12.        $mailbody = str_replace("{mobile}",$mobile,$mailbody);  
13.        $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件  
14.          
15.        //邮件格式(HTML/TXT),TXT为文本邮件  
16.        ##########################################  
17.        $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.  
18.        //$smtp->debug = false;//是否显示发送的调试信息  
19.        $smtp->sendmail($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);  
20.以上是接受表单提交值的部分  
21.  
22.  
23.一下是邮件服务器的类,直接调用就行了email.class.php  
24.  
25.<?  
26.class smtp  
27.{  
28./* Public Variables */  
29.var $smtp_port;  
30.var $time_out;  
31.var $host_name;  
32.var $log_file;  
33.var $relay_host;  
34.var $debug;  
35.var $auth;  
36.var $user;  
37.var $pass;  
38.  
39./* Private Variables */  
40.var $sock;  
41.  
42./* Constractor */  
43.function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)  
44.{  
45.$this->debug = FALSE;  
46.$this->smtp_port = $smtp_port;  
47.$this->relay_host = $relay_host;  
48.$this->time_out = 30; //is used in fsockopen()  
49.#  
50.$this->auth = $auth;//auth  
51.$this->user = $user;  
52.$this->pass = $pass;  
53.#  
54.$this->host_name = "localhost"; //is used in HELO command  
55.$this->log_file ="";  
56.  
57.$this->sock = FALSE;  
58.}  
59.  
60./* Main Function */  
61.function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")  
62.{  
63.$mail_from = $this->get_address($this->strip_comment($from));  
64.$body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);  
65.$header = '';  
66.$header .= "MIME-Version:1.0\r\n";  
67.if($mailtype=="HTML"){  
68.$header .= "Content-Type:text/html\r\n";  
69.}  
70.$header .= "To: ".$to."\r\n";  
71.if ($cc != "") {  
72.$header .= "Cc: ".$cc."\r\n";  
73.}  
74.$header .= "From: $from<".$from.">\r\n";  
75.$header .= "Subject: ".$subject."\r\n";  
76.$header .= $additional_headers;  
77.$header .= "Date: ".date("r")."\r\n";  
78.$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";  
79.list($msec, $sec) = explode(" ", microtime());  
80.$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";  
81.$TO = explode(",", $this->strip_comment($to));  
82.  
83.if ($cc != "") {  
84.$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));  
85.}  
86.  
87.if ($bcc != "") {  
88.$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));  
89.}  
90.  
91.$sent = TRUE;  
92.foreach ($TO as $rcpt_to) {  
93.$rcpt_to = $this->get_address($rcpt_to);  
94.if (!$this->smtp_sockopen($rcpt_to)) {  
95.$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");  
96.$sent = FALSE;  
97.continue;  
98.}  
99.if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {  
100.$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");  
101.} else {  
102.$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");  
103.$sent = FALSE;  
104.}  
105.fclose($this->sock);  
106.$this->log_write("Disconnected from remote host\n");  
107.}  
108.//echo "<br>";  
109.//echo $header;  
110.return $sent;  
111.}  
112.  
113./* Private Functions */  
114.  
115.function smtp_send($helo, $from, $to, $header, $body = "")  
116.{  
117.if (!$this->smtp_putcmd("HELO", $helo)) {  
118.return $this->smtp_error("sending HELO command");  
119.}  
120.#auth  
121.if($this->auth){  
122.if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {  
123.return $this->smtp_error("sending HELO command");  
124.}  
125.  
126.if (!$this->smtp_putcmd("", base64_encode($this->pass))) {  
127.return $this->smtp_error("sending HELO command");  
128.}  
129.}  
130.#  
131.if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {  
132.return $this->smtp_error("sending MAIL FROM command");  
133.}  
134.  
135.if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {  
136.return $this->smtp_error("sending RCPT TO command");  
137.}  
138.  
139.if (!$this->smtp_putcmd("DATA")) {  
140.return $this->smtp_error("sending DATA command");  
141.}  
142.  
143.if (!$this->smtp_message($header, $body)) {  
144.return $this->smtp_error("sending message");  
145.}  
146.  
147.if (!$this->smtp_eom()) {  
148.return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");  
149.}  
150.  
151.if (!$this->smtp_putcmd("QUIT")) {  
152.return $this->smtp_error("sending QUIT command");  
153.}  
154.  
155.return TRUE;  
156.}  
157.  
158.function smtp_sockopen($address)  
159.{  
160.if ($this->relay_host == "") {  
161.return $this->smtp_sockopen_mx($address);  
162.} else {  
163.return $this->smtp_sockopen_relay();  
164.}  
165.}  
166.  
167.function smtp_sockopen_relay()  
168.{  
169.$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");  
170.$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);  
171.if (!($this->sock && $this->smtp_ok())) {  
172.$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");  
173.$this->log_write("Error: ".$errstr." (".$errno.")\n");  
174.return FALSE;  
175.}  
176.$this->log_write("Connected to relay host ".$this->relay_host."\n");  
177.return TRUE;;  
178.}  
179.  
180.function smtp_sockopen_mx($address)  
181.{  
182.$domain = ereg_replace("^.+@([^@]+){1}quot;, "\\1", $address);  
183.if (!@getmxrr($domain, $MXHOSTS)) {  
184.$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");  
185.return FALSE;  
186.}  
187.foreach ($MXHOSTS as $host) {  
188.$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");  
189.$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);  
190.if (!($this->sock && $this->smtp_ok())) {  
191.$this->log_write("Warning: Cannot connect to mx host ".$host."\n");  
192.$this->log_write("Error: ".$errstr." (".$errno.")\n");  
193.continue;  
194.}  
195.$this->log_write("Connected to mx host ".$host."\n");  
196.return TRUE;  
197.}  
198.$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");  
199.return FALSE;  
200.}  
201.  
202.function smtp_message($header, $body)  
203.{  
204.fputs($this->sock, $header."\r\n".$body);  
205.$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));  
206.  
207.return TRUE;  
208.}  
209.  
210.function smtp_eom()  
211.{  
212.fputs($this->sock, "\r\n.\r\n");  
213.$this->smtp_debug(". [EOM]\n");  
214.  
215.return $this->smtp_ok();  
216.}  
217.  
218.function smtp_ok()  
219.{  
220.$response = str_replace("\r\n", "", fgets($this->sock, 512));  
221.$this->smtp_debug($response."\n");  
222.  
223.if (!ereg("^[23]", $response)) {  
224.fputs($this->sock, "QUIT\r\n");  
225.fgets($this->sock, 512);  
226.$this->log_write("Error: Remote host returned \"".$response."\"\n");  
227.return FALSE;  
228.}  
229.return TRUE;  
230.}  
231.  
232.function smtp_putcmd($cmd, $arg = "")  
233.{  
234.if ($arg != "") {  
235.if($cmd=="") $cmd = $arg;  
236.else $cmd = $cmd." ".$arg;  
237.}  
238.  
239.fputs($this->sock, $cmd."\r\n");  
240.$this->smtp_debug("> ".$cmd."\n");  
241.  
242.return $this->smtp_ok();  
243.}  
244.  
245.function smtp_error($string)  
246.{  
247.$this->log_write("Error: Error occurred while ".$string.".\n");  
248.return FALSE;  
249.}  
250.  
251.function log_write($message)  
252.{  
253.$this->smtp_debug($message);  
254.  
255.if ($this->log_file == "") {  
256.return TRUE;  
257.}  
258.  
259.$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;  
260.if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {  
261.$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");  
262.return FALSE;  
263.}  
264.flock($fp, LOCK_EX);  
265.fputs($fp, $message);  
266.fclose($fp);  
267.  
268.return TRUE;  
269.}  
270.  
271.function strip_comment($address)  
272.{  
273.$comment = "\\([^()]*\\)";  
274.while (ereg($comment, $address)) {  
275.$address = ereg_replace($comment, "", $address);  
276.}  
277.  
278.return $address;  
279.}  
280.  
281.function get_address($address)  
282.{  
283.$address = ereg_replace("([ \t\r\n])+", "", $address);  
284.$address = ereg_replace("^.*<(.+)>.*{1}quot;, "\\1", $address);  
285.  
286.return $address;  
287.}  
288.  
289.function smtp_debug($message)  
290.{  
291.if ($this->debug) {  
292.//echo $message."<br>";  
293.}  
294.}  
295.  
296.function get_attach_type($image_tag) { //  
297.  
298.$filedata = array();  
299.  
300.$img_file_con=fopen($image_tag,"r");  
301.unset($image_data);  
302.while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))  
303.$image_data.=$tem_buffer;  
304.fclose($img_file_con);  
305.  
306.$filedata['context'] = $image_data;  
307.$filedata['filename']= basename($image_tag);  
308.$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));  
309.switch($extension){  
310.case ".gif":  
311.$filedata['type'] = "image/gif";  
312.break;  
313.case ".gz":  
314.$filedata['type'] = "application/x-gzip";  
315.break;  
316.case ".htm":  
317.$filedata['type'] = "text/html";  
318.break;  
319.case ".html":  
320.$filedata['type'] = "text/html";  
321.break;  
322.case ".jpg":  
323.$filedata['type'] = "image/jpeg";  
324.break;  
325.case ".tar":  
326.$filedata['type'] = "application/x-tar";  
327.break;  
328.case ".txt":  
329.$filedata['type'] = "text/plain";  
330.break;  
331.case ".zip":  
332.$filedata['type'] = "application/zip";  
333.break;  
334.default:  
335.$filedata['type'] = "application/octet-stream";  
336.break;  
337.}  
338.  
339.  
340.return $filedata;  
341.}  
342.  
343.}  
344.?>  

 

posted @ 2013-05-21 10:46  王翔(kingfly)  阅读(1897)  评论(0编辑  收藏  举报