php 发送邮箱实例代码
<?php class pop3 { public $server = "pop3.126.com"; //服务器名 public $server_port = 110; //服务器端口 public $timeout = 30; //超过多少时间就算连接失败 public $connection = 0; //保持与主机的连接 public $state = "DISCONNECTED"; //保存当前的状态 public $debug = 0; //是否显示错误信息 public $err_str = ""; //服务器返回的错误信息 public $err_no; //服务器返回的错误号 public $respones; //保存服务器返回的信息 public $apop; //说明需要使用加密方式进行密码验证 public $messages; //邮件数 public $size; //邮件的总大小 public $mail_list; //保存各个邮件的大小及在服务器上的序列号 public $head = array(); //邮件头的内容数组 public $body = array(); //邮件体的内容数组 function POP3($server, $server_port, $timeout) { $this->server = $server; $this->server_port = $server_port; $this->timeout = $timeout; $this->debug = TRUE; } function open() { if ($this->server == "") { $this->err_str = "无效的主机名!"; return FALSE; } if ($this->debug) echo "正在打开 $this->server,$this->server_port,$this->timeout"; if (!$this->connection = @fsockopen($this->server, $this->server_port, $err_no, $err_str, $this->timeout)) { $this->err_str = "连接到POP服务器失败,错误信息:" . $err_str . "错误号:" . $err_no; return FALSE; } else { $this->getresponse(); if ($this->debug) $this->outdebug($this->response); if (substr($this->respones, 0, 3) != "+OK") { $this->err_str = "服务器返回无效信息:" . $this->respones . "请检查pop服务器是否正确"; return FALSE; } $this->state = "AUTHORIZATION"; return TRUE; } } function getresponse() { for ($this->respones;;) { if (feof($this->connection)) return FALSE; $this->respones.= fgets($this->connection, 100); $length = strlen($this->respones); if ($length >= 2 && substr($this->respones, $length - 2, 2) == "rn") { $this->respones = strtok($this->respones, "rn"); return TRUE; } } } function outdebug($message) { echo htmlspecialchars($message) . "n"; } function command($command, $length, $code) { if ($this->connection == 0) { $this->outdebug("没有连接到任何服务器,请检查网络连接!"); return FALSE; } if ($this->debug) $this->outdebug(">>> $command"); if (!fput($this->connection, "$command rn")) { $this->outdebug("'无法发送命令'.$command"); return FALSE; } else { $this->getresponse(); if ($this->debug) $this->outdebug($this->respones); if (substr($this->respones, 0, $length) != $code) { $this->outdebug("$command.'命令服务器返回信息无效'.$this->response"); return FALSE; } else return TRUE; } } function login($user, $pass) { if ($this->state != "AUTHORIZATION") { $this->outdebug("没有连接到任何服务器或状态不对!"); return FALSE; } if (!$this->apop) { if (!$this->command("USER $user", 3, "+OK")) return FALSE; if (!$this->command("PASS $pass", 3, "+OK")) return FALSE; } else { if (!$this->command(" APOP $user" . md5($this->greeting . $pass) , 3, "+OK")) return FALSE; } $this->state = "TRANSACTION"; return TRUE; } function stat_sum() { if ($this->state != "TRANSACTION") { $this->outdebug("还没有连接服务器或没有成功登陆"); return FALSE; } if (!$this->command("STAT", 3, "+OK")) return FALSE; else { $this->respones = strtok($this->respones, " "); $this->messages = strtok(" "); $this->size = strtok(" "); return TRUE; } } function listmail($mess = null, $uni_id = null) { if ($this->state != "TRANSACTION") { $this->outdebug("还没有连接服务器或没有成功登陆"); return FALSE; } if ($uni_id) $command = "UIDL"; else $command = "LIST"; if ($mess) $command.= $mess; if (!$this->command($command, 3, "+OK")) return FALSE; else { $i = 0; $this->mail_list = array(); $this->getresponse(); while ($this->respones != ".") { $i++; if ($this->debug) $this->outdebug($this->respones); if (uni_id) { $this->mail_list[$i][num] = strtok($this->respones, " "); $this->mail_list[$i][size] = strtok(" "); } else { $this->mail_list[$i][num] = intval(strtok($this->respones, " ")); $this->mail_list[$i][size] = intval(strtok(" ")); } $this->getresponse(); } return TRUE; } } function getmail($num, $line = - 1) { if ($this->state != "TRANSACTION") { $this->outdebug("还没有连接服务器或没有成功登陆"); return FALSE; } if ($line < 0) $command = "RETR $num"; else $command = "TOP $num $line"; if (!$this->command($command, 3, "+OK")) return FALSE; else { $this->getresponse(); $is_head = TRUE; while ($this->respones != ".") { if ($this->debug) $this->outdebug($this->respones); if (substr($this->respones, 0, 1) != ".") { $this->respones = substr($this->respones, 1, strlen($this->respones) - 1); } if (trim($this->respones) == "") $is_head = FALSE; if ($is_head) $this->head[] = $this->respones; else $this->body[] = $this->respones; $this->getresponse(); } return TRUE; } } function dele($num) { if ($this->state != "TRANSACTION") { $this->outdebug(",不能删除远程信件,还没有连接服务器或没有成功登陆"); return FALSE; } if (!num) { $this->outdebug("删除的邮件参数不对"); return FALSE; } if ($this->command("DELE $num", 3, "+OK")) return TRUE; else return FALSE; } function close() { if ($this->connection != 0) { if ($this->state == "TRANSACTION") $this->command("QUIT", 3, "+OK"); fclose($this->connection); $this->connection == 0; $this->state = "DISCONNECTED"; } } } //发送邮件类调用方法 $host = "pop3.126.com"; $user = " "; $pass = " "; $rec = new pop3($host, 110, 20); if (!$rec->open()) die($rec->err_str); echo "open"; if (!$rec->login($user, $pass)) die($rec->err_str); echo "login"; if (!$rec->stat()) die($rec->err_str); echo "共有" . $rec->messages . "封信件,共" . $rec->size . "字节大小"; if ($rec->messages > 0) { if (!$rec->listmail()) die($rec->err_str); echo "有以下信件:"; for ($i = 1; $i <= count($rec->mail_list); $i++) { echo "信件" . $rec->mail_list[$i][num] . "大小" . $rec->mail[$i][size] . ""; } $rec->getmail(1); echo "邮件头的内容:"; for ($i = 0; $ihead; $i++) echo htmlspecialchars($rec->head[$i]) . "n"; for ($i = 0; $ibody; $i++) echo htmlspecialchars($rec->body[$i]) . "n"; } $rec->close();
本文链接:http://www.phprm.com/code/6ea83fe22d06e2064f64eab4f3b1a8e5.html
收藏随意^^请保留教程地址.