找回密码
 注册

QQ登录

只需一步,快速开始

查看: 217|回复: 0

Reading Emails with PHP

[复制链接]
发表于 2014-2-11 22:24:15 | 显示全部楼层 |阅读模式
Last week I got to do a fun little side project which we called My Slow Low. The site is a simple photo collection of slow/low carb meals for those that are out of ideas on what to eat, but want to stick to their diet. While conceptualizing how we would build the site, the idea of emailing in photos came up. We didn’t want the hassle of account management and were trying to go for a more “mobile capable” option. This was my first time coding for IMAP with PHP and I figured some others could use a jump start from what I’ve learned.
PHP already has a nice IMAP extension which needs to be installed and enabled before going further. The core functionality is all there, but the specifics on how to use it aren’t necessarily all that clear.
Here’s a PHP class I put together to do some basic operations on an IMAP Inbox. It’s a bit tailored to this project, but could be easily revised to fit other needs or extended to be more full featured.
  1. <?php

  2. class Email_reader {

  3. // imap server connection
  4. public $conn;

  5. // inbox storage and inbox message count
  6. private $inbox;
  7. private $msg_cnt;

  8. // email login credentials
  9. private $server = 'yourserver.com';
  10. private $user   = 'email@yourserver.com';
  11. private $pass   = 'yourpassword';
  12. private $port   = 143; // adjust according to server settings

  13. // connect to the server and get the inbox emails
  14. function __construct() {
  15. $this->connect();
  16. $this->inbox();
  17. }

  18. // close the server connection
  19. function close() {
  20. $this->inbox = array();
  21. $this->msg_cnt = 0;

  22. imap_close($this->conn);
  23. }

  24. // open the server connection
  25. // the imap_open function parameters will need to be changed for the particular server
  26. // these are laid out to connect to a Dreamhost IMAP server
  27. function connect() {
  28. $this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
  29. }

  30. // move the message to a new folder
  31. function move($msg_index, $folder='INBOX.Processed') {
  32. // move on server
  33. imap_mail_move($this->conn, $msg_index, $folder);
  34. imap_expunge($this->conn);

  35. // re-read the inbox
  36. $this->inbox();
  37. }

  38. // get a specific message (1 = first email, 2 = second email, etc.)
  39. function get($msg_index=NULL) {
  40. if (count($this->inbox) <= 0) {
  41. return array();
  42. }
  43. elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
  44. return $this->inbox[$msg_index];
  45. }

  46. return $this->inbox[0];
  47. }

  48. // read the inbox
  49. function inbox() {
  50. $this->msg_cnt = imap_num_msg($this->conn);

  51. $in = array();
  52. for($i = 1; $i <= $this->msg_cnt; $i++) {
  53. $in[] = array(
  54. 'index'     => $i,
  55. 'header'    => imap_headerinfo($this->conn, $i),
  56. 'body'      => imap_body($this->conn, $i),
  57. 'structure' => imap_fetchstructure($this->conn, $i)
  58. );
  59. }

  60. $this->inbox = $in;
  61. }

  62. }

  63. ?>
复制代码


A fair amount of this is self-explanatory or commented inline, but I will go over the inbox() method because it is the core functionality. The IMAP inbox is much like an array with a numbered key starting at 1. In the inbox() method, I store that index so that the email can be moved, deleted, or read again later.
Next, the header is stored with the function imap_headerinfo(). This pulls down an object from the server containing information like the Subject, From: address, To: address, and text encoding type.
Using imap_body(), the body text of the email is retrieved. What’s returned isn’t overly clean as it’s just the raw body with boundaries included (see: multipart messages). If the received email is in HTML, there will be a plain text and HTML version included. It’s certainly possible to parse through this data like any email client does, but it’s definitely a little bit messy.
Lastly, ‘structure’ is retrieved with the imap_fetchstructure() function. This is very important if you are trying to access attachments as I was with My Slow Low. In my next post, I’ll go further into the details of saving an attachment from an email and share some more about how I implemented the email processor for My Slow Low.


来自圈子: Demo俱乐部
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-12 03:48 , Processed in 0.023472 second(s), 19 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表