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 /** This filter is documented in wp-admin/options.php */
14 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
15 wp_die( __( 'This action has been disabled by the administrator.' ) );
18 * Fires to allow a plugin to do a complete takeover of Post by Email.
22 do_action( 'wp-mail.php' );
24 /** Get the POP3 class with which to access the mailbox. */
25 require_once( ABSPATH . WPINC . '/class-pop3.php' );
27 /** Only check at this interval for new messages. */
28 if ( !defined('WP_MAIL_INTERVAL') )
29 define('WP_MAIL_INTERVAL', 300); // 5 minutes
31 $last_checked = get_transient('mailserver_last_checked');
34 wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
36 set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
38 $time_difference = get_option('gmt_offset') * HOUR_IN_SECONDS;
44 if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
45 wp_die( esc_html( $pop3->ERROR ) );
47 $count = $pop3->pass( get_option('mailserver_pass') );
49 if( false === $count )
50 wp_die( esc_html( $pop3->ERROR ) );
54 wp_die( __('There doesn’t seem to be any new mail.') );
57 for ( $i = 1; $i <= $count; $i++ ) {
59 $message = $pop3->get($i);
66 $content_transfer_encoding = '';
68 $author_found = false;
69 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
70 foreach ($message as $line) {
72 if ( strlen($line) < 3 )
77 if ( preg_match('/Content-Type: /i', $line) ) {
78 $content_type = trim($line);
79 $content_type = substr($content_type, 14, strlen($content_type) - 14);
80 $content_type = explode(';', $content_type);
81 if ( ! empty( $content_type[1] ) ) {
82 $charset = explode('=', $content_type[1]);
83 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
85 $content_type = $content_type[0];
87 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
88 $content_transfer_encoding = trim($line);
89 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
90 $content_transfer_encoding = explode(';', $content_transfer_encoding);
91 $content_transfer_encoding = $content_transfer_encoding[0];
93 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
94 $boundary = trim($line);
95 $boundary = explode('"', $boundary);
96 $boundary = $boundary[1];
98 if (preg_match('/Subject: /i', $line)) {
99 $subject = trim($line);
100 $subject = substr($subject, 9, strlen($subject) - 9);
101 // Captures any text in the subject before $phone_delim as the subject
102 if ( function_exists('iconv_mime_decode') ) {
103 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
105 $subject = wp_iso_descrambler($subject);
107 $subject = explode($phone_delim, $subject);
108 $subject = $subject[0];
112 * Set the author using the email address (From or Reply-To, the last used)
113 * otherwise use the site admin.
115 if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
116 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
117 $author = $matches[0];
119 $author = trim($line);
120 $author = sanitize_email($author);
121 if ( is_email($author) ) {
122 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
123 $userdata = get_user_by('email', $author);
124 if ( ! empty( $userdata ) ) {
125 $post_author = $userdata->ID;
126 $author_found = true;
131 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
132 $ddate = trim($line);
133 $ddate = str_replace('Date: ', '', $ddate);
134 if (strpos($ddate, ',')) {
135 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
137 $date_arr = explode(' ', $ddate);
138 $date_time = explode(':', $date_arr[3]);
140 $ddate_H = $date_time[0];
141 $ddate_i = $date_time[1];
142 $ddate_s = $date_time[2];
144 $ddate_m = $date_arr[1];
145 $ddate_d = $date_arr[0];
146 $ddate_Y = $date_arr[2];
147 for ( $j = 0; $j < 12; $j++ ) {
148 if ( $ddate_m == $dmonths[$j] ) {
153 $time_zn = intval($date_arr[4]) * 36;
154 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
155 $ddate_U = $ddate_U - $time_zn;
156 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
157 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
162 // Set $post_status based on $author_found and on author's publish_posts capability
163 if ( $author_found ) {
164 $user = new WP_User($post_author);
165 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
167 // Author not found in DB, set status to pending. Author already set to admin.
168 $post_status = 'pending';
171 $subject = trim($subject);
173 if ( $content_type == 'multipart/alternative' ) {
174 $content = explode('--'.$boundary, $content);
175 $content = $content[2];
177 // Match case-insensitive content-transfer-encoding.
178 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
179 $content = explode($delim[0], $content);
180 $content = $content[1];
182 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
184 $content = trim($content);
187 * Filter the original content of the email.
189 * Give Post-By-Email extending plugins full access to the content, either
190 * the raw content, or the content of the last quoted-printable section.
194 * @param string $content The original email content.
196 $content = apply_filters( 'wp_mail_original_content', $content );
198 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
199 $content = quoted_printable_decode($content);
202 if ( function_exists('iconv') && ! empty( $charset ) ) {
203 $content = iconv($charset, get_option('blog_charset'), $content);
206 // Captures any text in the body after $phone_delim as the body
207 $content = explode($phone_delim, $content);
208 $content = empty( $content[1] ) ? $content[0] : $content[1];
210 $content = trim($content);
213 * Filter the content of the post submitted by email before saving.
217 * @param string $content The email content.
219 $post_content = apply_filters( 'phone_content', $content );
221 $post_title = xmlrpc_getposttitle($content);
223 if ($post_title == '') $post_title = $subject;
225 $post_category = array(get_option('default_email_category'));
227 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
228 $post_data = wp_slash($post_data);
230 $post_ID = wp_insert_post($post_data);
231 if ( is_wp_error( $post_ID ) )
232 echo "\n" . $post_ID->get_error_message();
234 // We couldn't post, for whatever reason. Better move forward to the next email.
235 if ( empty( $post_ID ) )
239 * Fires after a post submitted by email is published.
243 * @param int $post_ID The post ID.
245 do_action( 'publish_phone', $post_ID );
247 echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), esc_html($post_author)) . '</p>';
248 echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), esc_html($post_title)) . '</p>';
250 if(!$pop3->delete($i)) {
251 echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>';
255 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';