• 使用代碼實現郵箱發送


      1 <?php
      2 
      3 class Smtp
      4 {
      5     /* Public Variables */
      6     public $smtp_port;
      7 
      8     public $time_out;
      9 
     10     public $host_name;
     11 
     12     public $log_file;
     13 
     14     public $relay_host;
     15 
     16     public $debug;
     17 
     18     public $auth;
     19 
     20     public $user;
     21 
     22     public $pass;
     23 
     24     /* Private Variables */
     25     private $sock;
     26 
     27     /* Constractor */
     28     function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
     29     {
     30         $this->debug = FALSE;
     31 
     32         $this->smtp_port = $smtp_port;
     33 
     34         $this->relay_host = $relay_host;
     35 
     36         $this->time_out = 30; //is used in fsockopen()
     37 
     38 
     39         $this->auth = $auth;//auth
     40 
     41         $this->user = $user;
     42 
     43         $this->pass = $pass;
     44 
     45 
     46         $this->host_name = "localhost"; //is used in HELO command
     47         $this->log_file = "";
     48 
     49         $this->sock = FALSE;
     50 
     51     }
     52 
     53     /* Main Function */
     54 
     55     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
     56     {
     57 
     58         $mail_from = $this->get_address($this->strip_comment($from));
     59 
     60         $body = preg_replace("/(^|(
    ))(.)/", "1.3", $body);
     61 
     62         $header = "MIME-Version:1.0
    ";
     63 
     64         if($mailtype=="HTML"){
     65 
     66             $header .= "Content-Type:text/html
    ";
     67 
     68         }
     69 
     70         $header .= "To: ".$to."
    ";
     71 
     72         if ($cc != "") {
     73 
     74             $header .= "Cc: ".$cc."
    ";
     75 
     76         }
     77 
     78         $header .= "From: $from<".$from.">
    ";
     79 
     80         $header .= "Subject: ".$subject."
    ";
     81 
     82         $header .= $additional_headers;
     83 
     84         $header .= "Date: ".date("r")."
    ";
     85 
     86         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")
    ";
     87 
     88         list($msec, $sec) = explode(" ", microtime());
     89 
     90         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">
    ";
     91 
     92         $TO = explode(",", $this->strip_comment($to));
     93 
     94         if ($cc != "") {
     95 
     96             $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
     97 
     98         }
     99 
    100         if ($bcc != "") {
    101 
    102             $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
    103 
    104         }
    105 
    106         $sent = TRUE;
    107 
    108         foreach ($TO as $rcpt_to) {
    109 
    110             $rcpt_to = $this->get_address($rcpt_to);
    111 
    112             if (!$this->smtp_sockopen($rcpt_to)) {
    113 
    114                 $this->log_write("Error: Cannot send email to ".$rcpt_to."
    ");
    115 
    116                 $sent = FALSE;
    117 
    118                 continue;
    119 
    120             }
    121 
    122             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
    123 
    124                 $this->log_write("E-mail has been sent to <".$rcpt_to.">
    ");
    125 
    126             } else {
    127 
    128                 $this->log_write("Error: Cannot send email to <".$rcpt_to.">
    ");
    129 
    130                 $sent = FALSE;
    131 
    132             }
    133 
    134             fclose($this->sock);
    135 
    136             $this->log_write("Disconnected from remote host
    ");
    137 
    138         }
    139 
    140         return $sent;
    141 
    142     }
    143 
    144     /* Private Functions */
    145 
    146     function smtp_send($helo, $from, $to, $header, $body = "")
    147     {
    148 
    149         if (!$this->smtp_putcmd("HELO", $helo)) {
    150 
    151             return $this->smtp_error("sending HELO command");
    152 
    153         }
    154 
    155         //auth
    156         if($this->auth){
    157 
    158             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
    159 
    160                 return $this->smtp_error("sending HELO command");
    161 
    162             }
    163 
    164             if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
    165 
    166                 return $this->smtp_error("sending HELO command");
    167 
    168             }
    169 
    170         }
    171 
    172 
    173         if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
    174 
    175             return $this->smtp_error("sending MAIL FROM command");
    176 
    177         }
    178 
    179         if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
    180 
    181             return $this->smtp_error("sending RCPT TO command");
    182 
    183         }
    184 
    185         if (!$this->smtp_putcmd("DATA")) {
    186 
    187             return $this->smtp_error("sending DATA command");
    188 
    189         }
    190 
    191         if (!$this->smtp_message($header, $body)) {
    192 
    193             return $this->smtp_error("sending message");
    194 
    195         }
    196 
    197         if (!$this->smtp_eom()) {
    198 
    199             return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
    200 
    201         }
    202 
    203         if (!$this->smtp_putcmd("QUIT")) {
    204 
    205             return $this->smtp_error("sending QUIT command");
    206 
    207         }
    208 
    209         return TRUE;
    210 
    211     }
    212 
    213     function smtp_sockopen($address)
    214     {
    215 
    216         if ($this->relay_host == "") {
    217 
    218             return $this->smtp_sockopen_mx($address);
    219 
    220         } else {
    221 
    222             return $this->smtp_sockopen_relay();
    223 
    224         }
    225 
    226     }
    227 
    228     function smtp_sockopen_relay()
    229     {
    230 
    231         $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."
    ");
    232 
    233         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
    234 
    235         if (!($this->sock && $this->smtp_ok())) {
    236 
    237             $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."
    ");
    238 
    239             $this->log_write("Error: ".$errstr." (".$errno.")
    ");
    240 
    241             return FALSE;
    242 
    243         }
    244 
    245         $this->log_write("Connected to relay host ".$this->relay_host."
    ");
    246 
    247         return TRUE;
    248 
    249     }
    250 
    251     function smtp_sockopen_mx($address)
    252     {
    253 
    254         $domain = preg_replace("/^.+@([^@]+)$/", "1", $address);
    255 
    256         if (!@getmxrr($domain, $MXHOSTS)) {
    257 
    258             $this->log_write("Error: Cannot resolve MX "".$domain.""
    ");
    259 
    260             return FALSE;
    261 
    262         }
    263 
    264         //专注与php学习 http://www.daixiaorui.com 欢迎您的访问
    265 
    266         foreach ($MXHOSTS as $host) {
    267 
    268             $this->log_write("Trying to ".$host.":".$this->smtp_port."
    ");
    269 
    270             $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
    271 
    272             if (!($this->sock && $this->smtp_ok())) {
    273 
    274                 $this->log_write("Warning: Cannot connect to mx host ".$host."
    ");
    275 
    276                 $this->log_write("Error: ".$errstr." (".$errno.")
    ");
    277 
    278                 continue;
    279 
    280             }
    281 
    282             $this->log_write("Connected to mx host ".$host."
    ");
    283 
    284             return TRUE;
    285 
    286         }
    287 
    288         $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")
    ");
    289 
    290         return FALSE;
    291 
    292     }
    293 
    294     function smtp_message($header, $body)
    295     {
    296 
    297         fputs($this->sock, $header."
    ".$body);
    298 
    299         $this->smtp_debug("> ".str_replace("
    ", "
    "."> ", $header."
    > ".$body."
    > "));
    300 
    301         return TRUE;
    302 
    303     }
    304 
    305     function smtp_eom()
    306     {
    307 
    308         fputs($this->sock, "
    .
    ");
    309 
    310         $this->smtp_debug(". [EOM]
    ");
    311 
    312         return $this->smtp_ok();
    313 
    314     }
    315 
    316     function smtp_ok()
    317     {
    318 
    319         $response = str_replace("
    ", "", fgets($this->sock, 512));
    320 
    321         $this->smtp_debug($response."
    ");
    322 
    323         if (!preg_match("/^[23]/", $response)) {
    324 
    325             fputs($this->sock, "QUIT
    ");
    326 
    327             fgets($this->sock, 512);
    328 
    329             $this->log_write("Error: Remote host returned "".$response.""
    ");
    330 
    331             return FALSE;
    332 
    333         }
    334 
    335         return TRUE;
    336 
    337     }
    338 
    339     function smtp_putcmd($cmd, $arg = "")
    340     {
    341 
    342         if ($arg != "") {
    343 
    344             if($cmd=="") $cmd = $arg;
    345 
    346             else $cmd = $cmd." ".$arg;
    347 
    348         }
    349 
    350         fputs($this->sock, $cmd."
    ");
    351 
    352         $this->smtp_debug("> ".$cmd."
    ");
    353 
    354         return $this->smtp_ok();
    355 
    356     }
    357 
    358     function smtp_error($string)
    359     {
    360 
    361         $this->log_write("Error: Error occurred while ".$string.".
    ");
    362 
    363         return FALSE;
    364 
    365     }
    366 
    367     function log_write($message)
    368     {
    369 
    370         $this->smtp_debug($message);
    371 
    372         if ($this->log_file == "") {
    373 
    374             return TRUE;
    375 
    376         }
    377 
    378         $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
    379 
    380         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
    381 
    382             $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""
    ");
    383 
    384             return FALSE;
    385 
    386         }
    387 
    388         flock($fp, LOCK_EX);
    389 
    390         fputs($fp, $message);
    391 
    392         fclose($fp);
    393 
    394 
    395         return TRUE;
    396 
    397     }
    398 
    399 
    400     function strip_comment($address)
    401     {
    402 
    403         $comment = "/([^()]*)/";
    404 
    405         while (preg_match($comment, $address)) {
    406 
    407             $address = preg_replace($comment, "", $address);
    408 
    409         }
    410 
    411         return $address;
    412 
    413     }
    414 
    415 
    416     function get_address($address)
    417     {
    418 
    419         $address = preg_replace("/([ 	
    ])+/", "", $address);
    420 
    421         $address = preg_replace("/^.*<(.+)>.*$/", "1", $address);
    422 
    423         return $address;
    424 
    425     }
    426 
    427     function smtp_debug($message)
    428     {
    429 
    430         if ($this->debug) {
    431 
    432             echo $message;
    433 
    434         }
    435 
    436     }
    437 
    438 }
     1 <?php
     2     // 端口:
     3     // 110:接受邮件服务器
     4     // 25:发送邮件服务器  
     5     // smtp:个人邮件   
     6     // smtp.163.com
     7 
     8 
     9 
    10 
    11     require_once "Smtp.class.php";
    12     //******************** 配置信息 ********************************
    13     $smtpserver = "smtp.163.com";//SMTP服务器
    14     $smtpserverport =25;//SMTP服务器端口
    15     $smtpusermail = "song3251289683@163.com";//SMTP服务器的用户邮箱
    16     $smtpemailto = "18735361909@163.com";//发送给谁
    17     $smtpuser = "song3251289683@163.com";//SMTP服务器的用户帐号,注:部分邮箱只需@前面的用户名
    18     $smtppass = "";//SMTP服务器的用户密码
    19     $mailtitle = "小貓咪  你最近不乖呀!";//邮件主题
    20     $mailcontent = "<h1>什麼玩意!</h1>";//邮件内容
    21     $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
    22     //************************ 配置信息 ****************************
    23     $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
    24     $smtp->debug = false;//是否显示发送的调试信息
    25     $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
    26 
    27     echo "<div style='300px; margin:36px auto;'>";
    28     if($state==""){
    29         echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
    30         echo "<a href='index.html'>点此返回</a>";
    31         exit();
    32     }
    33     echo "恭喜!邮件发送成功!!";
    34     echo "<a href='index.html'>点此返回</a>";
    35     echo "</div>";
    36 
    37 ?>
  • 相关阅读:
    OpenDataSource、OPENQUERY、OPENROWSET用法
    DROIDSLAM 论文阅读笔记
    android之路重新开启
    Android工程目录
    新入园
    获取输入框内容,数值类型转换问题
    基于.net平台remoting、DB2技术的大型分布式HIS系统架构及开发(项目架构师方向)
    基于ios开发点餐系统应用(附带源码)
    DEV控件及如何实现一些功能的应用(附带PPT讲解)
    基于J2EE+JBPM3.x/JBPM4.3+Flex流程设计器+Jquery+授权认证)企业普及版贝斯OA与工作流系统
  • 原文地址:https://www.cnblogs.com/songbao/p/11237753.html
Copyright © 2020-2023  润新知