<?php
header("Content-type: text/html; charset=utf-8"); class mail {
private $server='';
private $username='';
private $password='';
private $marubox='';
private $email='';
public function __construct ($username,$password,$email_address,$mail_server,$server_type,$port,$ssl=false) {
if($server_type == 'imap') {
if($port=='') $port='143';
$str_connect = '{'.$mail_server.'/imap:'.$port.'}INBOX';
}else{
if($port=='') $port='110';
$str_connect = '{'.$mail_server.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
$this->server = $str_connect;
$this->username = $username;
$this->password = $password;
$this->email = $email_address;
}
public function connect() {
$this->marubox = @imap_open($this->server,$this->username,$this->password,0);
if(!$this->marubox) {
echo "Error: Connecting to mail server<br/>";
echo $this->server;
exit;
}
}
/**
* 获取邮件总数
*/
public function get_mail_total() {
if(!$this->marubox) return false;
$tmp = imap_num_msg($this->marubox);
return is_numeric($tmp) ? $tmp : false;
}
/**
* 获取新进邮件总数
*/
public function get_new_mail_total() {
if(!$this->marubox) return false;
$tmp = imap_num_recent($this->marubox);
return is_numeric($tmp) ? $tmp : false;
}
/**
* 标记邮件成已读
*/
public function mark_mail_read($mid) {
return imap_setflag_full($this->marubox, $mid, '\\Seen');
}
/**
* 标记邮件成未读
*/
public function mark_mail_un_read($mid) {
return imap_clearflag_full($this->marubox, $mid, '\\Seen');
}
/**
* 获取邮件的头部
*/
public function get_imap_header($mid) {
return imap_headerinfo($this->marubox,$mid);
}
/**
* 格式化头部信息 $headerinfo get_imap_header 的返回值
*/
public function get_header_info($mail_header) {
$sender=$mail_header->from[0];
$sender_replyto=$mail_header->reply_to[0];
if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster') {
$mail_details=array(
'from'=>strtolower($sender->mailbox).'@'.$sender->host,
'fromName'=>$this->_decode_mime_str($sender->personal),
'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
'toNameOth'=>$this->_decode_mime_str($sender_replyto->personal),
'subject'=>$this->_decode_mime_str($mail_header->subject),
'to'=>strtolower($this->_decode_mime_str($mail_header->toaddress))
);
}
return $mail_details;
}
/**
* 判断是否阅读了邮件 $headerinfo get_imap_header 的返回值
*/
public function is_unread($headerinfo) {
if (($headerinfo->Unseen == 'U') || ($headerinfo->Recent == 'N')) return true;
return false;
}
/**
* 删除邮件
*/
public function delete_mail($mid) {
if(!$this->marubox) return false;
return imap_delete($this->marubox, $mid, 0);
}
/**
* 获取附件
*/
public function get_attach($mid,$path) {
if(!$this->marubox) return false;
$struckture = imap_fetchstructure($this->marubox,$mid);
$ar="";
if($struckture->parts) {
foreach($struckture->parts as $key => $value) {
$enc=$struckture->parts[$key]->encoding;
if($struckture->parts[$key]->ifdparameters) {
$name=$struckture->parts[$key]->dparameters[0]->value;
$message = imap_fetchbody($this->marubox,$mid,$key+1);
switch ($enc) {
case 0:
$message = imap_8bit($message);
break;
case 1:
$message = imap_8bit ($message);
break;
case 2:
$message = imap_binary ($message);
break;
case 3:
$message = imap_base64 ($message);
break;
case 4:
$message = quoted_printable_decode($message);
break;
case 5:
$message = $message;
break;
}
$fp=fopen($path.$name,"w");
fwrite($fp,$message);
fclose($fp);
$ar=$ar.$name.",";
}
// Support for embedded attachments starts here
if(!empty($struckture->parts[$key]->parts)) {
foreach($struckture->parts[$key]->parts as $keyb => $valueb) {
$enc=$struckture->parts[$key]->parts[$keyb]->encoding;
if($struckture->parts[$key]->parts[$keyb]->ifdparameters) {
$name=$struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
$partnro = ($key+1).".".($keyb+1);
$message = imap_fetchbody($this->marubox,$mid,$partnro);
switch ($enc) {
case 0:
$message = imap_8bit($message);
break;
case 1:
$message = imap_8bit ($message);
break;
case 2:
$message = imap_binary ($message);
break;
case 3:
$message = imap_base64 ($message);
break;
case 4:
$message = quoted_printable_decode($message);
break;
case 5:
$message = $message;
break;
}
$fp=fopen($path.$name,"w");
fwrite($fp,$message);
fclose($fp);
$ar=$ar.$name.",";
}
}
}
}
}
$ar=substr($ar,0,(strlen($ar)-1));
return $ar;
}
/**
* 读取邮件主体
*/
public function get_body($mid) {
if(!$this->marubox) return false;
$body = $this->_get_part($this->marubox, $mid, "TEXT/HTML");
if ($body == "") $body = $this->_get_part($this->marubox, $mid, "TEXT/PLAIN");
if ($body == "") return "";
return $this->_auto_iconv($body);
}
private function _get_mime_type(&$structure) {
$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); if($structure->subtype) {
return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;
}
return "TEXT/PLAIN";
}
private function _get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
if(!$structure) $structure = imap_fetchstructure($stream, $msg_number);
if($structure) {
if($mime_type == $this->_get_mime_type($structure))
{
if(!$part_number)
{
$part_number = "1";
}
$text = imap_fetchbody($stream, $msg_number, $part_number);
//file_put_contents('D:/project/www/b/'.$msg_number.'.txt', $text);
if($structure->encoding == 3)
{
return imap_base64($text);
}
else if($structure->encoding == 4)
{
return imap_qprint($text);
}
else
{
return $text;
}
}
if($structure->type == 1) /* multipart */
{
while(list($index, $sub_structure) = each($structure->parts))
{
$prefix = false;
if($part_number)
{
$prefix = $part_number . '.';
}
$data = $this->_get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
if($data)
{
return $data;
}
}
}
}
return false;
}
/**
* 关闭 IMAP 流
*/
public function close_mailbox() {
if(!$this->marubox) return false;
imap_close($this->marubox,CL_EXPUNGE);
}
private function _decode_mime_str($string, $charset="UTF-8" ) {
$newString = '';
$elements=imap_mime_header_decode($string);
for($i=0;$i<count($elements);$i++) {
if($elements[$i]->charset == 'default') $elements[$i]->charset = 'iso-8859-1';
$newString .= iconv($elements[$i]->charset, $charset, $elements[$i]->text);
}
return $newString;
}
/**
* 对象销毁前关闭邮箱
*/
public function __destruct() {
$this->close_mailbox();
}
} //User name off the mail box
$username = '***';
//Password of mailbox
$password = '******';
//Email address of that mailbox some time the uname and email address are identical
$email_address = '***';
//Ip or name of the POP or IMAP mail server
$mail_server = 'pop.exmail.qq.com';
//if this server is imap or pop default is pop
$server_type = 'imap';
//Server port for pop or imap Default is 110 for pop and 143 for imap
$port = 143; $mail = new mail($username,$password,$email_address,$mail_server,$server_type,$port);
$mail->connect(); $mail_total = $mail->get_mail_total(); for ($i=$mail_total; $i>0; $i--) {
//附件读取这块,我没搞懂,如果哪位能修好,记得通知我。
$str = $mail->get_attach($i,"./");
$arr = explode(",",$str);
foreach($arr as $key=>$value) echo ($value == "") ? "" : "Atteched File :: " . $value . "<br>";
echo "<br>------------------------------------------------------------------------------------------<br>";
exit;
}

唠叨几点:

1、这个类是国外的一个大神写的(receivemail.class.php),我拿来改了。核心部分我还改不来。

2、附件读取部分有问题,经测试,读取不到附件。

3、邮箱的每一份邮件,并没有一个唯一DI,读取邮件总数后递减for循环就可以开搞了。

5、邮件的读取很慢,尤其是 $server_type = 'imap'; 的时候,更慢。

6、邮件太多的情况下,php读取不能一次性读完,我的处理办法是,crontab去启动shell脚本,用shell脚本去启动php脚本。

php脚本每启动一次就统计总邮件数,for循环递减读取若干条邮件,读完就删除邮件,当邮件总数为 0 时返回 0 ,否则返回 0 。

shell脚本得到php脚本的返回值,判断是否要继续启动php脚本。当然我这个处理办法是根据我的公司业务需求来的。具体情况具体分析。

这样做也有个好处,因为php长时间读取邮件,那个邮件的连接资源极有可能会断掉。它不断掉,你连接数据库的资源也会断掉。

下面是shell的伪代码。

php读取邮件的更多相关文章

  1. python从任意文件读取邮件地址输出的代码

    如下的资料是关于python从任意文件读取邮件地址输出的代码. # This script takes whatever you throw at stdin and outputs email ad ...

  2. javamail 发送、读取邮件

    概述 1.邮件相关的标准 厂商所提供的 JavaMail 服务程序可以有选择地实现某些邮件协议,常见的邮件协议包括: SMTP(Simple Mail Transfer Protocol) :即简单邮 ...

  3. 使用 EWS(Exchange Web Service)协议读取邮件、发送邮件

    问题: 公司之前可以通过POP3协议收发邮件,因而在SoapUI中用JavaMail可以读取邮件,后来配置了Office 365,POP3协议端口不再开放,邮件全部读取失败,报login timeou ...

  4. java读取邮件

    package com.zz.mail; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  5. MailKit使用IMAP读取邮件找不到附件Attachments为空的解决方法

    今天发现有些邮件无法读取Attachments,邮件明明有附件,但使用Mailkit读取时,Attachments为空,我用的IMAP协议读取收件箱里的邮件,处理完后移动已删除: foreach (v ...

  6. 使用EWS API读取邮件

    #安装EwsManagedApi.msi,然后导入EWS模块 Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Ser ...

  7. C# Po3协议读取邮件内容遇到的问题

    背景:最近在做一个小工具,读取PO3协议邮件服务器的指定人员的邮件,东西做出来了,弄了一个While死循环,20秒执行一次, 结果运行了3天,周一来IT人员找上门来了,你的电脑什么情况,怎么一个小时下 ...

  8. python开发_email_读取邮件头信息

    在python中的类库中,python自带了email模块. 在email模块中,我们可以email类库实现对邮件的读取,和邮件的发送等功能. 本文先来谈谈在python中,利用email模块读取邮件 ...

  9. C#读取邮件附件的方法

    基于需求需要从邮件里读取附件,从网络搜索整理如下: 1 使用 Spire.Email 从官网下载安装并引用,地址:https://www.e-iceblue.com/Download/email-fo ...

随机推荐

  1. Asp.Net进阶/值类型与引用类型:复习

    什么是值类型? 值类型: 就是非类类型,委托类型,接口类型,string类型的类型称为值类型. 引用类型类型:就是类类型,委托类型,接口类型,string类型称为引用类型. 值类型与引用类型的赋值问题 ...

  2. JavaScript的书写格式及书写的注意点

    JavaScript书写格式: 1.行内样式: 写在标签内部 2.内嵌样式(内联样式) : 写在一对head标签中 3.外链样式: 写在一个单独的.js文件中, 再导入进来 JavaScript书写格 ...

  3. 转换属性transform

    transform: rotate(45deg);旋转 rotate(值) 值为正,表示元素顺时针旋转 值为负,表示元素逆时针旋转 transform: translate(200px,100px); ...

  4. c#使用正则表达式处理字符串

    正则表达式可以灵活而高效的处理文本,可以通过匹配快速分析大量的文本找到特定的字符串. 可以验证字符串是否符合某种预定义的格式,可以提取,编辑,替换或删除文本子字符串. 现在如下特定的字符串: stri ...

  5. netaddr网络地址工具python

    print("==========1==========") from netaddr import IPNetwork # IPNetwork('192.168.7.80/30' ...

  6. JavaScript: 自动类型转换

    我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...

  7. PM2 监控 Spring Boot 项目运行

    更多 PM2 的用法介绍请参考: PM2简易使用手册 - 掘金 由于 PM2 通常都用于 node 应用, 所以 exec_mode 应写为 fork, 其中最重要的是 args, -jar 和 ja ...

  8. p2.BTC-数据结构

    hash pointers:哈希指针,除了保存值的地址,还要存这整个区块的内容的hash值.这样就既能访问到值,还能确定访问的值有没有被篡改. 一 Blockchain Block chain is ...

  9. Deep learning_CNN_Review:A Survey of the Recent Architectures of Deep Convolutional Neural Networks——2019

    CNN综述文章 的翻译 [2019 CVPR] A Survey of the Recent Architectures of Deep Convolutional Neural Networks 翻 ...

  10. ARM的Semihosting技术(转)

    Semihosting技术将应用程序中的IO请求通过一定的通道传送到主机(host),由主机上的资源响应应用程序的IO请求, 而不是像在主机上执行本地应用程序一样,由应用程序所在的计算机响应应用程序I ...