/**
  * simulate WordPress call to PHPMailer
  * @param string|array $to Array or comma-separated list of email addresses to send message.
  * @param string $subject Email subject
  * @param string $message Message contents
  * @param string|array $headers Optional. Additional headers.
  * @param string|array $attachments Optional. Files to attach.
  * @return bool
  */
 public function wpmail($to, $subject, $message, $headers, $attachments)
 {
     $plugin = DisableEmailsPlugin::getInstance();
     // get the site domain and get rid of www.
     $sitename = strtolower($_SERVER['SERVER_NAME']);
     if (substr($sitename, 0, 4) == 'www.') {
         $sitename = substr($sitename, 4);
     }
     // set default From name and address
     $this->phpmailer->FromName = 'WordPress';
     $this->phpmailer->From = 'wordpress@' . $sitename;
     // let hookers change the function arguments if settings allow
     if ($plugin->options['wp_mail']) {
         extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')), EXTR_IF_EXISTS);
     }
     // set mail's subject and body
     $this->phpmailer->Subject = $subject;
     $this->phpmailer->Body = $message;
     // headers
     if (!empty($headers)) {
         if (!is_array($headers)) {
             // Explode the headers out, so this function can take both
             // string headers and an array of headers.
             $headers = explode("\n", str_replace("\r\n", "\n", $headers));
         }
         foreach ($headers as $header) {
             // check for pseudo-headers
             if (strpos($header, ':') === false) {
                 // TODO: handle multipart boundaries
                 //~ if ( false !== stripos( $header, 'boundary=' ) ) {
                 //~ $parts = preg_split('/boundary=/i', trim( $header ) );
                 //~ $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
                 //~ }
                 continue;
             }
             list($name, $content) = explode(':', trim($header), 2);
             $name = trim($name);
             $content = trim($content);
             switch (strtolower($name)) {
                 // Mainly for legacy -- process a From: header if it's there
                 case 'from':
                     $this->_setFrom($content);
                     break;
                 case 'cc':
                     $this->_addCC(explode(',', $content));
                     break;
                 case 'bcc':
                     $this->_addBCC(explode(',', $content));
                     break;
                 case 'content-type':
                     $this->_setContentType($content);
                     break;
                 default:
                     $this->phpmailer->AddCustomHeader("{$name}: {$content}");
                     break;
             }
         }
     }
     // attachments
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             try {
                 $this->phpmailer->AddAttachment($attachment);
             } catch (phpmailerException $e) {
                 continue;
             }
         }
     }
     if ($plugin->options['wp_mail_from']) {
         $this->phpmailer->From = apply_filters('wp_mail_from', $this->phpmailer->From);
     }
     if ($plugin->options['wp_mail_from_name']) {
         $this->phpmailer->FromName = apply_filters('wp_mail_from_name', $this->phpmailer->FromName);
     }
     if ($plugin->options['wp_mail_content_type']) {
         $this->phpmailer->ContentType = apply_filters('wp_mail_content_type', $this->phpmailer->ContentType);
     }
     if ($plugin->options['wp_mail_charset']) {
         $this->phpmailer->CharSet = apply_filters('wp_mail_charset', $this->phpmailer->CharSet);
     }
     if ($plugin->options['phpmailer_init']) {
         do_action('phpmailer_init', $this->phpmailer);
     }
     return true;
 }
Example #2
0
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
if (!defined('ABSPATH')) {
    exit;
}
define('DISABLE_EMAILS_PLUGIN_FILE', __FILE__);
define('DISABLE_EMAILS_PLUGIN_ROOT', dirname(__FILE__) . '/');
define('DISABLE_EMAILS_PLUGIN_NAME', basename(dirname(__FILE__)) . '/' . basename(__FILE__));
// options
define('DISABLE_EMAILS_OPTIONS', 'disable_emails');
include DISABLE_EMAILS_PLUGIN_ROOT . 'includes/class.DisableEmailsPlugin.php';
DisableEmailsPlugin::getInstance();
// replace standard WordPress wp_mail() if nobody else has already done it
if (!function_exists('wp_mail')) {
    function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
    {
        // create mock PHPMailer object to handle any filter and action hook listeners
        $mailer = new DisableEmailsPHPMailerMock();
        return $mailer->wpmail($to, $subject, $message, $headers, $attachments);
    }
    DisableEmailsPlugin::setActive();
}