For mail processing, protocols such as POP3 and SMTP should be familiar to everyone, and the IMAP we introduced today is actually a very commonly used mail processing protocol. It is similar to POP3 in that it focuses on receiving and processing mail. However, compared to POP3, the local operations of IMAP will be directly synchronized to the online mail server. POP3 generally does not synchronize directly. This is the biggest difference between the two. Regarding the specific content of these mail agreements, if there are students who don't know much about it, you can check the relevant information on the Internet.
No matter it is IMAP or POP3, it can be very simple to realize the function of a mail client after cooperating with SMTP. In this article, we will mainly learn some simple content of the IMAP extension in PHP.
Install extension
First, install the IMAP extension. This extension is released with the PHP source package. You can directly add --with-imap and --with-imap-ssl to configure when compiling. If it is a later installation, you can directly find the imap directory in the ext directory of the source package and then you can go in and perform the ordinary extension installation steps.
However, it should be noted that IMAP extension requires some components to be installed in the operating system environment.
yum install -y libc-client-devel libc-client
ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.a
ln -s /usr/lib64/libkrb5.so /usr/lib/libkrb5.so
We need to install libc-client-devel, and then establish two soft connections. Otherwise, problems may occur during the installation of the extended code.
Connect to QQ mailbox
Next, we try to connect to the QQ mailbox.
$host = "{imap.qq.com:993/imap2/ssl}INBOX";
$username = "xxxx"; // 不用带 @qq.com
$password = "xxxxxx"; // 开通 imap 后获得的授权登录码
$mbox = imap_open($host, $username, $password);
Very simple function, imap_open() is used to open the handle of the connected mailbox. The three parameters are also very intuitive, host specifies the domain name address of the mailbox, and can directly specify which folder in the mailbox to connect to. Here we directly enter the inbox. When connecting to the QQ mailbox, the user name can use the QQ number directly, without the need to bring @qq.com behind. The password is the authorized login code obtained after we select account management in the QQ mailbox settings and activate the imap function.
View mailbox information
After successfully connecting to the mailbox, you can view some related information.
$rowsCount = imap_num_msg($mbox);
echo $rowsCount, PHP_EOL;
// 37
imap_num_msg() returns the number of messages in the mailbox, which is actually the number of our mails.
$list = imap_list($mbox, "{imap.qq.com}", "*");
var_dump($list);
// array(6) {
// [0]=>
// string(18) "{imap.qq.com}INBOX"
// [1]=>
// string(26) "{imap.qq.com}Sent Messages"
// [2]=>
// string(19) "{imap.qq.com}Drafts"
// [3]=>
// string(29) "{imap.qq.com}Deleted Messages"
// [4]=>
// string(17) "{imap.qq.com}Junk"
// [5]=>
// string(51) "{imap.qq.com}&UXZO1mWHTvZZOQ-/xxxxxx@139.com"
// }
The imap_list() function returns the folder information in the mailbox. For example, we have INBOX inbox, Sent Messages sent mail, Drafts draft box, Deleted Messages deleted mail, Junk trash box, and another one is mine The mailbox folder of 139 bound in the mailbox is also displayed.
$chk = (array) imap_mailboxmsginfo($mbox);
var_dump($chk);
// array(8) {
// ["Unread"]=>
// int(34)
// ["Deleted"]=>
// int(0)
// ["Nmsgs"]=>
// int(37)
// ["Size"]=>
// int(951128)
// ["Date"]=>
// string(37) "Wed, 16 Dec 2020 14:31:50 +0800 (CST)"
// ["Driver"]=>
// string(4) "imap"
// ["Mailbox"]=>
// string(54) "{imap.qq.com:993/imap/notls/ssl/user="149844827"}INBOX"
// ["Recent"]=>
// int(0)
// }
imap_mailboxmsginfo() returns the information in the current mailbox folder. As can be seen from the returned fields, we have 34 Unread unread emails. 37 new mails, including size, acquisition time, mailbox information, etc.
Read operation mail
The last is our highlight, how to download and read emails and perform some simple operations.
$all = imap_search($mbox, "ALL");
var_dump($all);
// array(37) {
// [0]=>
// int(1)
// [1]=>
// int(2)
// [2]=>
// int(3)
// [3]=>
// int(4)
// [4]=>
// int(5)
// ……
// ……
foreach ($all as $m) {
$headers = imap_fetchheader($mbox, $m);
$rawBody = imap_fetchbody($mbox, $m, FT_UID);
$headers = iconv_mime_decode_headers($headers, 0, "UTF-8");
var_dump($headers);
if (isset($headers['Content-Transfer-Encoding']) && $headers['Content-Transfer-Encoding'] == 'base64') {
$rawBody = imap_base64($rawBody);
}
var_dump($rawBody);
if ($m == 1) {
imap_mail_copy($mbox, $m, "Drafts"); // 拷贝到草稿箱
imap_setflag_full($mbox, $m, "Seen"); // 设置为已读
}
if ($m == 2) {
imap_delete($mbox, $m); // 删除
imap_expunge($mbox);
}
if ($m == 3) {
imap_mail_move($mbox, $m, "Junk"); // 移动
imap_expunge($mbox);
}
}
imap_search() is used to search for mails. Its second parameter is a specified string. For example, this ALL returns all mail information. It can also be specified as DELETED, UNSEEN and many other contents. You can refer to the relevant documents for the specific parameter list. This function gets all the mail numbers of the mail message. In fact, it can be seen that it is a number from 1 to 37.
Read mail
imap_fetchheader() and imap_fetchbody() read the header information and content information of the mail according to the mail number, respectively. If they are printed normally, their content is encoded, which means that we can't visually see the specific content information. Therefore, a UTF-8 decoding process is required for the header file, and the email content is decoded by checking the corresponding encoding type according to the Content-Transfer-Encoding field in the header information. Here we only demonstrate the base64 encoding. In fact, there are other encoding formats. Interested students can check the information for a deeper understanding.
// 第一封邮件
// headers
// array(13) {
// ["From"]=>
// string(29) "QQ邮箱团队 <10000@qq.com>"
// ["To"]=>
// string(29) "xxx <xxxxxxx@qq.com>"
// ["Subject"]=>
// string(53) "更安全、更高效、更强大,尽在QQ邮箱APP"
// ["Date"]=>
// string(31) "Wed, 16 Dec 2020 10:08:54 +0800"
// ["Message-ID"]=>
// string(38) "<app_popularize.1608084534.3423313103>"
// ["X-QQ-STYLE"]=>
// string(1) "1"
// ["X-QQ-SYSID"]=>
// string(9) "100000010"
// ["X-QQ-MIME"]=>
// string(21) "TCMime 1.0 by Tencent"
// ["X-QQ-Mailer"]=>
// string(10) "QQMail 2.x"
// ["X-QQ-mid"]=>
// string(30) "mmnez10417t1608084534tfekjqwx0"
// ["Content-Type"]=>
// string(26) "text/html; charset="utf-8""
// ["Content-Transfer-Encoding"]=>
// string(6) "base64"
// ["Mime-Version"]=>
// string(3) "1.0"
// }
The above content is the header information of the first email. From the content, you can see that Subject is the title of the email. This is an email sent by the QQ mailbox system. From and To are the email addresses of the sender and recipient respectively. The other important thing is that Content-Type and Content-Transfer-Encoding correspond to document type, character set encoding, and conversion encoding type, respectively.
The headers of different emails will be different, we just show the simplest one here.
// rawBody
// string(5850) "
// <!DOCTYPE html>
// <html>
// <head>
// <meta charset="UTF-8">
// <title>imap</title>
// <style>
// @media screen and (min-width: 700px) {
// .bottomErweima {
// display: block !important;
// }
// #btn {
// display: none !important;
// }
// .footer {
// display: none !important;
// }
// }
// /* vivo手机width: 980px 同时 aspect-ratio小于1的,处于700px-1000px的手机*/
// @media screen and (min-width: 700px) and (max-width: 1000px) and (max-aspect-ratio:1/1){
// .bottomErweima {
// display: none !important;
// }
// #btn {
// display: block !important;
// }
// .footer {
// display: block !important;
// }
// }
// </style>
// </head>
// <body style="width: 100%;margin: 0;padding: 0;position: relative;">
// <div id="email-box" style="max-width: 550px;margin: 0 auto;">
// <div class="email_container">
// <div class="head" style="background: #f3f3f3;">
// <span class="content" style="font-size: 14px;color: #000000;line-height: 26px;display: block;padding: 40px 20px;">QQ邮箱APP,让高效触手可及。在这里,你可以登录多个邮箱账号、便捷存储微信邮件、多窗口编辑邮件......还有更多功能,等你探索!</span>
// </div>
// ……
// ……
The content of the mail is simply HTML format after parsing with imap_base64(). This directly corresponds to the Content-Type in the header information. In fact, imap_base64() is no different from base64_decode(). It is no problem if you use base64_decode() directly. Of course, the premise is to determine whether base64 is used in Content-Transfer-Encoding to encode the email content. Some emails may not even have this field.
Copy, move, delete mail
imap_mail_copy() is used to copy emails. Here, we copy the first email to the draft box, and then use imap_setflag_full() to mark this email as read. It can be seen from the parameters that Seem means read. Of course, it also has other parameters, such as Deleted, Draft and the like.
The imap_delete() function is used to delete emails, and imap_mail_move() is used to move files. Calling these two functions requires imap_expunge() to synchronize operations online.
After completing the operation, you can directly check whether the online mail has changed accordingly.
Summarize
Let’s simply learn about IMAP, because when I was learning this piece of content, I found that there are already many encapsulated classes on the Internet that can be copied and used directly. In addition, its functions are very rich, and there are many functions that have not been introduced, such as operating accessories and other functions. I believe that everyone will gradually come into contact with them in their own learning and use process.
Test code:
Reference documents:
https://www.php.net/manual/zh/book.imap.php
Searchable on their respective media platforms [Hardcore Project Manager]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。