[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/ -> wp-mail.php (source)

   1  <?php
   2  /**
   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
   6   *
   7   * @package WordPress
   8   */
   9  
  10  /** Make sure that the WordPress bootstrap has run before continuing. */
  11  require  __DIR__ . '/wp-load.php';
  12  
  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.' ), 403 );
  16  }
  17  
  18  $mailserver_url = get_option( 'mailserver_url' );
  19  
  20  if ( empty( $mailserver_url ) || 'mail.example.com' === $mailserver_url ) {
  21      wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
  22  }
  23  
  24  /**
  25   * Fires to allow a plugin to do a complete takeover of Post by Email.
  26   *
  27   * @since 2.9.0
  28   */
  29  do_action( 'wp-mail.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  30  
  31  /** Get the POP3 class with which to access the mailbox. */
  32  require_once ABSPATH . WPINC . '/class-pop3.php';
  33  
  34  /** Only check at this interval for new messages. */
  35  if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
  36      define( 'WP_MAIL_INTERVAL', 5 * MINUTE_IN_SECONDS );
  37  }
  38  
  39  $last_checked = get_transient( 'mailserver_last_checked' );
  40  
  41  if ( $last_checked ) {
  42      wp_die(
  43          sprintf(
  44              // translators: %s human readable rate limit.
  45              __( 'Email checks are rate limited to once every %s.' ),
  46              human_time_diff( time() - WP_MAIL_INTERVAL, time() )
  47          ),
  48          __( 'Slow down, no need to check for new mails so often!' ),
  49          429
  50      );
  51  }
  52  
  53  set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
  54  
  55  $time_difference = (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
  56  
  57  $phone_delim = '::';
  58  
  59  $pop3 = new POP3();
  60  
  61  if ( ! $pop3->connect( get_option( 'mailserver_url' ), get_option( 'mailserver_port' ) ) || ! $pop3->user( get_option( 'mailserver_login' ) ) ) {
  62      wp_die( esc_html( $pop3->ERROR ) );
  63  }
  64  
  65  $count = $pop3->pass( get_option( 'mailserver_pass' ) );
  66  
  67  if ( false === $count ) {
  68      wp_die( esc_html( $pop3->ERROR ) );
  69  }
  70  
  71  if ( 0 === $count ) {
  72      $pop3->quit();
  73      wp_die( __( 'There does not seem to be any new mail.' ) );
  74  }
  75  
  76  // Always run as an unauthenticated user.
  77  wp_set_current_user( 0 );
  78  
  79  $blog_charset = get_option( 'blog_charset' );
  80  
  81  for ( $i = 1; $i <= $count; $i++ ) {
  82  
  83      $message = $pop3->get( $i );
  84  
  85      $bodysignal                = false;
  86      $charset                   = '';
  87      $content                   = '';
  88      $content_type              = '';
  89      $content_transfer_encoding = '';
  90      $boundary                  = '';
  91      $subject                   = '';
  92      $post_author               = 1;
  93      $author_found              = false;
  94      $post_date                 = '';
  95      $post_date_gmt             = '';
  96  
  97      foreach ( $message as $line ) {
  98          // Body signal.
  99          if ( strlen( $line ) < 3 ) {
 100              $bodysignal = true;
 101          }
 102          if ( $bodysignal ) {
 103              $content .= $line;
 104          } else {
 105              if ( preg_match( '/Content-Type: /i', $line ) ) {
 106                  $content_type = trim( $line );
 107                  $content_type = substr( $content_type, 14, strlen( $content_type ) - 14 );
 108                  $content_type = explode( ';', $content_type );
 109                  if ( ! empty( $content_type[1] ) ) {
 110                      $charset = explode( '=', $content_type[1] );
 111                      $charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : '';
 112                  }
 113                  $content_type = $content_type[0];
 114              }
 115              if ( preg_match( '/Content-Transfer-Encoding: /i', $line ) ) {
 116                  $content_transfer_encoding = trim( $line );
 117                  $content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
 118                  $content_transfer_encoding = explode( ';', $content_transfer_encoding );
 119                  $content_transfer_encoding = $content_transfer_encoding[0];
 120              }
 121              if ( 'multipart/alternative' === $content_type && str_contains( $line, 'boundary="' ) && '' === $boundary ) {
 122                  $boundary = trim( $line );
 123                  $boundary = explode( '"', $boundary );
 124                  $boundary = $boundary[1];
 125              }
 126              if ( preg_match( '/Subject: /i', $line ) ) {
 127                  $subject = trim( $line );
 128                  $subject = substr( $subject, 9, strlen( $subject ) - 9 );
 129                  // Captures any text in the subject before $phone_delim as the subject.
 130                  if ( function_exists( 'iconv_mime_decode' ) ) {
 131                      $subject = iconv_mime_decode( $subject, 2, $blog_charset );
 132                  } else {
 133                      $subject = wp_iso_descrambler( $subject );
 134                  }
 135                  $subject = explode( $phone_delim, $subject );
 136                  $subject = $subject[0];
 137              }
 138  
 139              /*
 140               * Set the author using the email address (From or Reply-To, the last used)
 141               * otherwise use the site admin.
 142               */
 143              if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
 144                  if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches ) ) {
 145                      $author = $matches[0];
 146                  } else {
 147                      $author = trim( $line );
 148                  }
 149                  $author = sanitize_email( $author );
 150                  if ( is_email( $author ) ) {
 151                      $userdata = get_user_by( 'email', $author );
 152                      if ( ! empty( $userdata ) ) {
 153                          $post_author  = $userdata->ID;
 154                          $author_found = true;
 155                      }
 156                  }
 157              }
 158  
 159              if ( preg_match( '/Date: /i', $line ) ) { // Of the form '20 Mar 2002 20:32:37 +0100'.
 160                  $ddate = str_replace( 'Date: ', '', trim( $line ) );
 161                  // Remove parenthesized timezone string if it exists, as this confuses strtotime().
 162                  $ddate           = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );
 163                  $ddate_timestamp = strtotime( $ddate );
 164                  $post_date       = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference );
 165                  $post_date_gmt   = gmdate( 'Y-m-d H:i:s', $ddate_timestamp );
 166              }
 167          }
 168      }
 169  
 170      // Set $post_status based on $author_found and on author's publish_posts capability.
 171      if ( $author_found ) {
 172          $user        = new WP_User( $post_author );
 173          $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
 174      } else {
 175          // Author not found in DB, set status to pending. Author already set to admin.
 176          $post_status = 'pending';
 177      }
 178  
 179      $subject = trim( $subject );
 180  
 181      if ( 'multipart/alternative' === $content_type ) {
 182          $content = explode( '--' . $boundary, $content );
 183          $content = $content[2];
 184  
 185          // Match case-insensitive Content-Transfer-Encoding.
 186          if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
 187              $content = explode( $delim[0], $content );
 188              $content = $content[1];
 189          }
 190          $content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
 191      }
 192      $content = trim( $content );
 193  
 194      /**
 195       * Filters the original content of the email.
 196       *
 197       * Give Post-By-Email extending plugins full access to the content, either
 198       * the raw content, or the content of the last quoted-printable section.
 199       *
 200       * @since 2.8.0
 201       *
 202       * @param string $content The original email content.
 203       */
 204      $content = apply_filters( 'wp_mail_original_content', $content );
 205  
 206      if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) {
 207          $content = quoted_printable_decode( $content );
 208      }
 209  
 210      if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
 211          $content = iconv( $charset, $blog_charset, $content );
 212      }
 213  
 214      // Captures any text in the body after $phone_delim as the body.
 215      $content = explode( $phone_delim, $content );
 216      $content = empty( $content[1] ) ? $content[0] : $content[1];
 217  
 218      $content = trim( $content );
 219  
 220      /**
 221       * Filters the content of the post submitted by email before saving.
 222       *
 223       * @since 1.2.0
 224       *
 225       * @param string $content The email content.
 226       */
 227      $post_content = apply_filters( 'phone_content', $content );
 228  
 229      $post_title = xmlrpc_getposttitle( $content );
 230  
 231      if ( '' === trim( $post_title ) ) {
 232          $post_title = $subject;
 233      }
 234  
 235      $post_category = array( get_option( 'default_email_category' ) );
 236  
 237      $post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
 238      $post_data = wp_slash( $post_data );
 239  
 240      $post_ID = wp_insert_post( $post_data );
 241      if ( is_wp_error( $post_ID ) ) {
 242          echo "\n" . $post_ID->get_error_message();
 243      }
 244  
 245      // The post wasn't inserted or updated, for whatever reason. Better move forward to the next email.
 246      if ( empty( $post_ID ) ) {
 247          continue;
 248      }
 249  
 250      /**
 251       * Fires after a post submitted by email is published.
 252       *
 253       * @since 1.2.0
 254       *
 255       * @param int $post_ID The post ID.
 256       */
 257      do_action( 'publish_phone', $post_ID );
 258  
 259      echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
 260      echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
 261  
 262      if ( ! $pop3->delete( $i ) ) {
 263          echo '<p>' . sprintf(
 264              /* translators: %s: POP3 error. */
 265              __( 'Oops: %s' ),
 266              esc_html( $pop3->ERROR )
 267          ) . '</p>';
 268          $pop3->reset();
 269          exit;
 270      } else {
 271          echo '<p>' . sprintf(
 272              /* translators: %s: The message ID. */
 273              __( 'Mission complete. Message %s deleted.' ),
 274              '<strong>' . $i . '</strong>'
 275          ) . '</p>';
 276      }
 277  }
 278  
 279  $pop3->quit();


Generated : Tue Oct 13 08:20:33 2026 Cross-referenced by PHPXref