写了一个网站,反馈页面要用到 php 发邮件,无奈网站空间的 php 没有配置可用的邮件服务器,发现 php 也可通过
socket 裸发邮件。配一个可用的帐号,下面函数就可用了。
1 function send_mail( $to , $subject = ' 未标题 ' , $body ){
2 $loc_host = smtp.126.com ;
3 $smtp_acc = youraccount ;
4 $smtp_pass = yourpassword ;
5 $smtp_host = smtp.126.com ;
6 $from = admin@126.com ;
7
8 $headers = content-type: text/plain; charset=\ gb2312\ \r\ncontent-transfer-encoding:base64 ;
9 $lb = \r\n ;
10 $hdr = explode ( $lb , $headers );
11 if ( $body ){
12 $bdy = preg_replace ( /^\./ , .. , explode ( $lb , $body ));
13 }
14
15 $smtp = array (
16 array ( ehlo . $loc_host . $lb , 220, 250 , helo error: ) ,
17 array ( auth login . $lb , 334 , auth error: ) ,
18 array ( base64_encode ( $smtp_acc ) . $lb , 334 , authentification error: ) ,
19 array ( base64_encode ( $smtp_pass ) . $lb , 235 , authentification error: )
20 );
21
22 $smtp [] = array ( mail from: . $lb , 250 , mail from error: );
23 $smtp [] = array ( rcpt to: . $lb , 250 , rcpt to error: );
24 $smtp [] = array ( data . $lb , 354 , data error: );
25
26 $smtp [] = array ( from: . $from . $lb , , );
27 $smtp [] = array ( to: . $to . $lb , , );
28 $smtp [] = array ( subject: . $subject . $lb , , );
29
30 foreach ( $hdr as $h ){
31 $smtp [] = array ( $h . $lb , , );
32 }
33
34 $smtp [] = array ( $lb , , );
35
36 if ( $bdy ){
37 foreach ( $bdy as $b ){
38 $smtp [] = array ( base64_encode ( $b . $lb ) . $lb , , );
39 }
40 }
41 $smtp [] = array ( . . $lb , 250 , data(end) error: );
42 $smtp [] = array ( quit . $lb , 221 , quit error: );
43
44 $fp = (@ fsockopen ( $smtp_host , 25 ));
45 if ( ! $fp ) echo error: cannot connect to . $smtp_host .
;
46 while ( $result = @ fgets ( $fp , 1024 )){
47 if ( substr ( $result , 3 , 1 ) == ){ break ; }
48 }
49 $result_str = ;
50 foreach ( $smtp as $req ){
51 @ fputs ( $fp , $req [ 0 ]);
52 if ( $req [ 1 ]){
53 while ( $result = @ fgets ( $fp , 1024 )){
54 if ( substr ( $result , 3 , 1 ) == ){ break ; }
55 }
56 if ( ! strstr ( $req [ 1 ] , substr ( $result , 0 , 3 ))){
57 $result_str .= $reg [ 2 ] . $result .
;
58 }
59 }
60 }
61 @ fclose ( $fp );
62 return $result_str ;
63 }
64
65