$emails = array( array('account' => 'haodai1688@163.com', 'pwd' => '*', 'server' => 'imap.163.com'), array('account' => 'haodaiwan@163.com', 'pwd' => '*', 'server' => 'imap.163.com'), array('account' => 'asmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'bsmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'csmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'dsmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'esmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'fsmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'jsmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'hsmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'ismschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'ksmschina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'sms_test@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'smstestchina@yeah.net', 'pwd' => '*', 'server' => 'imap.yeah.net'), array('account' => 'vesxza@163.com', 'pwd' => '*', 'server' => 'imap.163.com'), array('account' => 'mm663305@163.com', 'pwd' => '*', 'server' => 'imap.163.com'), array('account' => '14828@163.com', 'pwd' => '*', 'server' => 'imap.163.com'), array('account' => '15850060264@163.com', 'pwd' => '*', 'server' => 'imap.163.com') ); //读取发件箱的所有邮件 foreach ($emails as $key => $value) { $imap_stream = imap_open('{' . $value['server'] . ':993/imap/ssl}&XfJT0ZAB-', $value['account'], $value['pwd']); $msg_num = imap_num_msg($imap_stream); //已发送的邮件的数量 while ($msg_num > 0) { $data = array(); $headers = imap_headerinfo($imap_stream, $msg_num); /* 邮件主题内容 */ $subject = imap_mime_header_decode($headers->subject); $data['subject_text'] = mysql_real_escape_string($subject[0]->text); /* 发件人的地址 */ $data['fromaddress'] = mysql_real_escape_string($headers->fromaddress); /* 收件人的地址 */ $data['toaddress'] = mysql_real_escape_string($headers->toaddress); /* 发送邮件的时间 */ $data['totime'] = date('Y-m-d H:i:s', strtotime($headers->MailDate)); /* 邮件正文的内容,todo:需要完善编码判断,以及附件的读取判断 */ $data['msg'] = mysql_real_escape_string(base64_decode(imap_fetchbody($imap_stream, $msg_num, 1))); /* 邮件的header头内容 */ $data['header'] = mysql_real_escape_string(imap_fetchbody($imap_stream, 1, 0)); /* 邮件的body区域的内容 */ $data['body'] = mysql_real_escape_string(imap_body($imap_stream, $msg_num)); /* 提取电话号码 */ if (preg_match_all('/\d{4,}/', $data['subject_text'], $matches, PREG_PATTERN_ORDER)) { $data['phone'] = implode(',', $matches[0]); } else { preg_match_all('/\d{4,}/', $data['msg'], $matches, PREG_PATTERN_ORDER); $data['phone'] = implode(',', $matches[0]); } $msg_num--; addContent($data); } imap_close($imap_stream); echo $value['account'] . '---完成', PHP_EOL; } //写入数据到mysql的过程 function addContent($data) { static $mysql_conn = ''; if (!is_resource($mysql_conn)) { $mysql_conn = mysql_connect('127.0.0.1', 'root', 'root'); mysql_select_db("remail", $mysql_conn); mysql_query('SET NAMES UTF8'); } $sql = 'INSERT INTO mail_detail(subject,from_address,to_address,to_time,msg,header,body,phone) values (\'' . $data['subject_text'] . '\',\'' . $data['fromaddress'] . '\',\'' . $data['toaddress'] . '\',\'' . $data['totime'] . '\',\'' . $data['msg'] . '\',\'' . $data['header'] . '\',\'' . $data['body'] . '\',\'' . $data['phone'] . '\');'; mysql_query($sql); }
PHP遍历邮箱获取邮件内容
原创
©著作权归作者所有:来自51CTO博客作者990653058的原创作品,谢绝转载,否则将追究法律责任
下一篇:PHP实现文件内容的分页读取
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
PHP发送邮件
发送邮件是网站的常用功能,用户激活、找回密码等场景常需要发送邮件
发送邮件 php swift