3 * Gets the email message from the user's mailbox to add as
4 * a WordPress post. Mailbox connection information must be
5 * configured under Settings > Writing
10 /** Make sure that the WordPress bootstrap has run before continuing. */
11 require(dirname(__FILE__) . '/wp-load.php');
13 /** Get the POP3 class with which to access the mailbox. */
14 require_once( ABSPATH . WPINC . '/class-pop3.php' );
16 $time_difference = absint(get_option('gmt_offset')) * 3600;
22 if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) ||
23 ! $pop3->user(get_option('mailserver_login')) ||
24 ( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) {
26 wp_die( ( 0 === $count ) ? __("There doesn't seem to be any new mail.") : wp_specialchars($pop3->ERROR) );
29 for ( $i = 1; $i <= $count; $i++ ) {
31 $message = $pop3->get($i);
38 $content_transfer_encoding = '';
40 $author_found = false;
41 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
42 foreach ($message as $line) {
44 if ( strlen($line) < 3 )
49 if ( preg_match('/Content-Type: /i', $line) ) {
50 $content_type = trim($line);
51 $content_type = substr($content_type, 14, strlen($content_type) - 14);
52 $content_type = explode(';', $content_type);
53 if ( ! empty( $content_type[1] ) ) {
54 $charset = explode('=', $content_type[1]);
55 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
57 $content_type = $content_type[0];
59 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
60 $content_transfer_encoding = trim($line);
61 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
62 $content_transfer_encoding = explode(';', $content_transfer_encoding);
63 $content_transfer_encoding = $content_transfer_encoding[0];
65 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
66 $boundary = trim($line);
67 $boundary = explode('"', $boundary);
68 $boundary = $boundary[1];
70 if (preg_match('/Subject: /i', $line)) {
71 $subject = trim($line);
72 $subject = substr($subject, 9, strlen($subject) - 9);
73 // Captures any text in the subject before $phone_delim as the subject
74 if ( function_exists('iconv_mime_decode') ) {
75 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
77 $subject = wp_iso_descrambler($subject);
79 $subject = explode($phone_delim, $subject);
80 $subject = $subject[0];
83 // Set the author using the email address (From or Reply-To, the last used)
84 // otherwise use the site admin
85 if ( preg_match('/(From|Reply-To): /', $line) ) {
86 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
87 $author = $matches[0];
89 $author = trim($line);
90 $author = sanitize_email($author);
91 if ( is_email($author) ) {
92 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
93 $userdata = get_user_by_email($author);
94 if ( empty($userdata) ) {
95 $author_found = false;
97 $post_author = $userdata->ID;
101 $author_found = false;
105 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
106 $ddate = trim($line);
107 $ddate = str_replace('Date: ', '', $ddate);
108 if (strpos($ddate, ',')) {
109 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
111 $date_arr = explode(' ', $ddate);
112 $date_time = explode(':', $date_arr[3]);
114 $ddate_H = $date_time[0];
115 $ddate_i = $date_time[1];
116 $ddate_s = $date_time[2];
118 $ddate_m = $date_arr[1];
119 $ddate_d = $date_arr[0];
120 $ddate_Y = $date_arr[2];
121 for ( $j = 0; $j < 12; $j++ ) {
122 if ( $ddate_m == $dmonths[$j] ) {
127 $time_zn = intval($date_arr[4]) * 36;
128 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
129 $ddate_U = $ddate_U - $time_zn;
130 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
131 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
136 // Set $post_status based on $author_found and on author's publish_posts capability
137 if ( $author_found ) {
138 $user = new WP_User($post_author);
139 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
141 // Author not found in DB, set status to pending. Author already set to admin.
142 $post_status = 'pending';
145 $subject = trim($subject);
147 if ( $content_type == 'multipart/alternative' ) {
148 $content = explode('--'.$boundary, $content);
149 $content = $content[2];
150 // match case-insensitive content-transfer-encoding
151 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
152 $content = explode($delim[0], $content);
153 $content = $content[1];
155 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
157 $content = trim($content);
159 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
160 $content = quoted_printable_decode($content);
163 if ( function_exists('iconv') && ! empty( $charset ) ) {
164 $content = iconv($charset, get_option('blog_charset'), $content);
167 // Captures any text in the body after $phone_delim as the body
168 $content = explode($phone_delim, $content);
169 $content = empty( $content[1] ) ? $content[0] : $content[1];
171 $content = trim($content);
173 $post_content = apply_filters('phone_content', $content);
175 $post_title = xmlrpc_getposttitle($content);
177 if ($post_title == '') $post_title = $subject;
179 $post_category = array(get_option('default_email_category'));
181 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
182 $post_data = add_magic_quotes($post_data);
184 $post_ID = wp_insert_post($post_data);
185 if ( is_wp_error( $post_ID ) )
186 echo "\n" . $post_ID->get_error_message();
188 // We couldn't post, for whatever reason. Better move forward to the next email.
189 if ( empty( $post_ID ) )
192 do_action('publish_phone', $post_ID);
194 echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), wp_specialchars($post_author)) . '</p>';
195 echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), wp_specialchars($post_title)) . '</p>';
197 if(!$pop3->delete($i)) {
198 echo '<p>' . sprintf(__('Oops: %s'), wp_specialchars($pop3->ERROR)) . '</p>';
202 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';