20080511

WBOY
Release: 2016-06-23 14:36:25
Original
919 people have browsed it

 

写了一个网站,反馈页面要用到 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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template