-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclass-post-by-email-mailserver-imap.php
192 lines (174 loc) · 4.61 KB
/
class-post-by-email-mailserver-imap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Post By Email
*
* @package PostByEmail
* @author Kat Hagan <[email protected]>
* @license GPL-2.0+
* @link https://github.com/codebykat/wp-post-by-email/
* @copyright 2013-2015 Kat Hagan / Automattic
*/
/**
* Mailserver class that uses the built-in IMAP bindings.
* http://php.net/manual/en/book.imap.php
*
* @package PostByEmail
* @author Kat Hagan <[email protected]>
*/
class Post_By_Email_Mailserver_IMAP extends Post_By_Email_Mailserver {
/**
* Establishes the connection to the mailserver.
*
* @since 1.1
*
* @param array $options Options array
*
* @return object
*/
public function open_mailbox_connection( $options ) {
$ssl = $options['secure'] ? '/ssl' : '/notls';
$this->protocol = $options['protocol'];
$mailbox = '{' . $options['hostspec'] . ':' . $options['port']
. '/' . $options['protocol'] . $ssl . '}INBOX';
$username = $options['username'];
$password = $options['password'];
$this->connection = imap_open( $mailbox, $username, $password );
$errors = imap_errors();
foreach ( $errors as $error ) {
if ( 'SECURITY PROBLEM: insecure server advertised AUTH=PLAIN' === $error ) {
continue; // this happens with no SSL, it's fine
}
// implicit else: something actually went wrong
// throw new Exception( 'Error connecting to mail server.' );
}
if ( ! $this->connection )
return false;
return true;
}
/**
* Closes the connection to the mailserver.
*
* @since 1.1
*/
public function close_connection() {
if ( ! $this->connection )
return true;
if ( ! imap_ping( $this->connection ) ) {
return true;
}
$result = imap_close( $this->connection );
$this->connection = null;
return $result;
}
/**
* Retrieve the list of new message IDs from the server.
*
* @since 1.1
*
* @return array Array of message UIDs
*/
public function get_messages() {
if ( ! $this->connection )
return;
$messages = imap_search( $this->connection, 'UNSEEN', SE_UID );
if ( ! $messages ) {
return array();
}
return $messages;
}
/**
* Retrieve message headers.
*
* @since 1.1
*
* @param int $uid Message UID
*
* @return object
*/
public function get_message_headers( $uid ) {
if ( ! $this->connection ) {
return;
}
$headers_raw = imap_fetchheader( $this->connection, $uid, FT_UID );
$headers_parsed = imap_rfc822_parse_headers( $headers_raw );
$headers = get_object_vars($headers_parsed);
// standardize headers
$headers['From'] = $headers['fromaddress'];
$headers['To'] = $headers['toaddress'];
$headers['Reply_To'] = $headers['reply_toaddress'];
$headers['Sender'] = $headers['senderaddress'];
// "from", "to", "reply_to", and "sender" are still objects
foreach ( array( 'from', 'to', 'reply_to', 'sender' ) as $key ) {
$first = $headers[ $key ][0];
$headers[ $key ] = $first->mailbox . '@' . $first->host;
}
return $headers;
}
/**
* Mark a list of messages read on the server.
*
* @since 1.1
*
* @param array $uids UIDs of messages to mark read
* @param bool $delete Whether to delete read messages
*
* @throws Horde_Imap_Client_Exception
*/
public function mark_as_read( $uids, $delete=false ) {
if ( ! $this->connection ) {
return;
}
$flag = '\Seen';
if ( $delete || ( 'POP3' === $this->protocol ) ) {
$flag = '\Deleted';
}
return imap_setflag_full( $this->connection, implode( ',', $uids ), '\Seen', ST_UID );
}
/**
* Get the content of a message from the mailserver.
*
* @since 1.1
*
* @param int Message UID
*
* @return string Message content
*/
public function get_message_body( $uid ) {
if ( ! $this->connection ) {
return;
}
// imap_body() will only return a verbatim copy of the message body.
// To extract single parts of a multipart MIME-encoded message you have to use
// imap_fetchstructure() to analyze its structure and imap_fetchbody() to extract
// a copy of a single body component.
$body = imap_body( $this->connection, $uid, FT_UID | FT_PEEK );
// print $body;
// die();
return $body;
}
/**
* Get a message's attachments from the mail server.
*
* @since 1.1
*
* @param int Message UID
*
* @return array Attachments
*/
public function get_attachments( $uid ) {
return array();
}
/**
* Get a single attachment from the mail server.
*
* @since 1.1
*
* @param int Message UID
* @param int Attachment's MIME ID
*
* @return string Decoded attachment data
*/
public function get_single_attachment( $uid, $mime_id ) {
return '';
}
}