html2text() public method

This is used by msgHTML(). Note - older versions of this function used a bundled advanced converter which was been removed for license reasons in #232. Example usage: Use default conversion $plain = $mail->html2text($html); Use your own custom converter $plain = $mail->html2text($html, function($html) { $converter = new MyHtml2text($html); return $converter->get_text(); });
public html2text ( string $html, boolean | callable $advanced = false ) : string
$html string The HTML text to convert
$advanced boolean | callable Any boolean value to use the internal converter, or provide your own callable for custom conversion.
return string
Example #1
0
File: Mailer.php Project: acp3/core
 /**
  * @return string
  */
 private function getTextSignature()
 {
     if (!empty($this->mailSignature)) {
         return "\n-- \n" . $this->phpMailer->html2text($this->mailSignature, true);
     }
     return '';
 }
Example #2
0
 public static function sendEmail($to, $subject, $contents, $from)
 {
     $mail = new \PHPMailer();
     //Setup the body first since we need it regardless of sending method.
     $eol = PHP_EOL;
     $body = '<html>' . $eol;
     $body .= '<body style=\'font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\'>' . $eol;
     $body .= $contents;
     $body .= '</body>' . $eol;
     $body .= '</html>' . $eol;
     // If the mailer couldn't instantiate there's a good chance the user has an incomplete update & we should fallback to php mail()
     // @todo Log this failure.
     if (!defined('PHPMAILER_ENABLED') || PHPMAILER_ENABLED !== true || !$mail instanceof \PHPMailer) {
         $headers = 'From: ' . $from . $eol;
         $headers .= 'Reply-To: ' . $from . $eol;
         $headers .= 'Return-Path: ' . $from . $eol;
         $headers .= 'X-Mailer: newznab' . $eol;
         $headers .= 'MIME-Version: 1.0' . $eol;
         $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
         $headers .= $eol;
         return mail($to, $subject, $body, $headers);
     }
     // Check to make sure the user has their settings correct.
     if (PHPMAILER_USE_SMTP == true) {
         if (!defined('PHPMAILER_SMTP_HOST') || PHPMAILER_SMTP_HOST === '' || (!defined('PHPMAILER_SMTP_PORT') || PHPMAILER_SMTP_PORT === '')) {
             throw new \phpmailerException('You opted to use SMTP but the PHPMAILER_SMTP_HOST and/or PHPMAILER_SMTP_PORT is/are not defined correctly! Either fix the missing/incorrect values or change PHPMAILER_USE_SMTP to false in the www/settings.php');
         }
         // If the user enabled SMTP & Auth but did not setup credentials, throw an exception.
         if (defined('PHPMAILER_SMTP_AUTH') && PHPMAILER_SMTP_AUTH == true) {
             if (!defined('PHPMAILER_SMTP_USER') || PHPMAILER_SMTP_USER === '' || (!defined('PHPMAILER_SMTP_PASSWORD') || PHPMAILER_SMTP_PASSWORD === '')) {
                 throw new \phpmailerException('You opted to use SMTP and SMTP Auth but the PHPMAILER_SMTP_USER and/or PHPMAILER_SMTP_PASSWORD is/are not defined correctly. Please set them in www/settings.php');
             }
         }
     }
     //Finally we can send the mail.
     $mail->isHTML(true);
     if (PHPMAILER_USE_SMTP) {
         $mail->isSMTP();
         $mail->Host = PHPMAILER_SMTP_HOST;
         $mail->Port = PHPMAILER_SMTP_PORT;
         $mail->SMTPSecure = PHPMAILER_SMTP_SECURE;
         if (PHPMAILER_SMTP_AUTH) {
             $mail->SMTPAuth = true;
             $mail->Username = PHPMAILER_SMTP_USER;
             $mail->Password = PHPMAILER_SMTP_PASSWORD;
         }
     }
     $settings = new Settings();
     $site_email = $settings->getSetting('email');
     $fromEmail = PHPMAILER_FROM_EMAIL === '' ? $site_email : PHPMAILER_FROM_EMAIL;
     $fromName = PHPMAILER_FROM_NAME === '' ? $settings->title : PHPMAILER_FROM_NAME;
     $replyTo = PHPMAILER_REPLYTO === '' ? $site_email : PHPMAILER_REPLYTO;
     PHPMAILER_BCC !== '' ? $mail->addBCC(PHPMAILER_BCC) : null;
     $mail->setFrom($fromEmail, $fromName);
     $mail->addAddress($to);
     $mail->addReplyTo($replyTo);
     $mail->Subject = $subject;
     $mail->Body = $body;
     $mail->AltBody = $mail->html2text($body, true);
     $sent = $mail->send();
     if (!$sent) {
         //@todo Log failed email send attempt.
         throw new \phpmailerException('Unable to send mail. Error: ' . $mail->ErrorInfo);
     }
     return $sent;
 }
/**
 * Send Email:
 * Looks for pending threads, sends to memebers if member has sent email
 *
 * @throws Exception
 * @throws phpmailerException
 */
function wpmg_cron_send_email()
{
    //To debug, adjust settings here.
    $args = array('post_type' => 'mg_threads', 'post_status' => 'draft', 'perm' => 'readable', 'meta_key' => 'mg_thread_email_status', 'meta_value' => 'Pending');
    //All pending emails
    $query = new WP_Query($args);
    $threads = $query->get_posts();
    if (count($threads) > 0) {
        foreach ($threads as $emailParsed) {
            //Single Thread Information generated
            $thread_id = $emailParsed->ID;
            $group_id = get_post_meta($thread_id, 'mg_thread_email_group_id', true);
            $senderEmail = get_post_meta($thread_id, 'mg_thread_email_from', true);
            $is_active_group = get_post_meta($group_id, 'mg_group_status', true);
            $thread_subject = get_post_meta($thread_id, 'mg_thread_email_subject', true);
            //Filter Out specific automated emails
            $test_out_office = "/out of the office/i";
            if (preg_match($test_out_office, $thread_subject)) {
                update_post_meta($thread_id, 'mg_thread_email_status', 'Out of Office');
                break;
            }
            $test_auto = "/automatic reply/i";
            if (preg_match($test_auto, $thread_subject)) {
                update_post_meta($thread_id, 'mg_thread_email_status', 'Automatic Reply');
                break;
            }
            //Checks to see if group is active and valid
            if ($is_active_group == 2 && is_numeric($group_id) && $group_id > 0) {
                /* get sender user details */
                $senderUser = get_user_by("email", $senderEmail);
                $senderUserId = $senderUser->ID;
                $senderEmail = $senderUser->user_email;
                //Checks if user is valid
                if (is_numeric($senderUserId)) {
                    /* get other users from the sender user group */
                    $args = array('meta_query' => array('relation' => 'AND', array('key' => 'mg_user_group_sub_arr', 'value' => '"' . $group_id . '"', 'compare' => 'LIKE'), array('key' => 'mg_user_status', 'value' => 1)));
                    $user_query = new WP_User_Query($args);
                    //Checks to see if sender is in group
                    $in_group = false;
                    foreach ($user_query->get_results() as $memberstoSent) {
                        if ($senderUserId == $memberstoSent->ID) {
                            $in_group = true;
                            break;
                        } else {
                            $in_group = false;
                        }
                    }
                    if ($user_query->get_total() > 0 && $in_group == true) {
                        //Get group information to build email
                        $groupTitle = get_the_title($group_id);
                        $groupEmail = get_post_meta($group_id, 'mg_group_email', true);
                        $mail_type = get_post_meta($group_id, 'mg_group_mail_type', true);
                        //Email information
                        $has_parent = get_post_meta($thread_id, 'mg_thread_parent_id', true);
                        $body = get_post_meta($thread_id, 'mg_thread_email_content', true);
                        //Generates footer for email from group listing
                        $footerText = nl2br(stripslashes(get_post_meta($group_id, 'mg_group_footer_text', true)));
                        if (empty($has_parent)) {
                            $footerText = str_replace("{%grouptitle%}", $groupTitle, $footerText);
                            $footerText = str_replace("{%site_url%}", get_site_url(), $footerText);
                            $footerText = str_replace("{%archive_url%}", get_permalink($group_id), $footerText);
                            $footerText = str_replace("{%profile_url%}", get_admin_url("", "profile.php"), $footerText);
                            //Needs development to add unsusbscribe to footer
                            //							$footerText = str_replace( "{%unsubscribe_url%}", get_bloginfo( 'wpurl' ) . '?unsubscribe=1&userid=' . $sendtouserId . '&group=' . $group_id, $footerText );
                            $body .= $footerText;
                        }
                        if ($mail_type == 'smtp') {
                            global $phpmailer;
                            if (!is_object($phpmailer) || !is_a($phpmailer, 'PHPMailer')) {
                                require_once ABSPATH . WPINC . '/class-phpmailer.php';
                                require_once ABSPATH . WPINC . '/class-smtp.php';
                                $phpmailer = new PHPMailer();
                            }
                            $mail = new PHPMailer();
                            $mail->IsSMTP();
                            $mail->SMTPDebug = 0;
                            /*
                             * Connect to SMTP
                             */
                            //Add Thread ID to references, if replied to, becomes parent of reply
                            if (get_post_meta($group_id, 'mg_group_smtp_username', true) != '' && get_post_meta($group_id, 'mg_group_smtp_password', true) != '') {
                                $mail->Username = get_post_meta($group_id, 'mg_group_smtp_username', true);
                                $mail->Password = get_post_meta($group_id, 'mg_group_smtp_password', true);
                                $mail->SMTPAuth = true;
                                $mail->SMTPSecure = "ssl";
                            } else {
                                $mail->Username = $groupEmail;
                                $mail->Password = get_post_meta($group_id, 'mg_group_password', true);
                                $mail->SMTPAuth = false;
                            }
                            $mail->Host = get_post_meta($group_id, 'mg_group_smtp_server', true);
                            $mail->Port = get_post_meta($group_id, 'mg_group_smtp_port', true);
                            /* Custom Headers
                            
                            							$mail->addCustomHeader( 'Errors-To', 'no-reply@domain' );
                            							$mail->addCustomHeader( 'Return-Path', 'no-reply-bounces@domain' );
                            							*/
                            $mail->addCustomHeader('references', '[' . $thread_id . ']');
                            $mail->addCustomHeader('sender', $groupEmail);
                            //Set top level addresses
                            $mail->SetFrom($senderEmail);
                            $mail->AddReplyTo($groupEmail, $groupTitle);
                            $mail->AddAddress($groupEmail, $groupTitle);
                            //Add each user in group to bcc email
                            foreach ($user_query->get_results() as $memberstoSent) {
                                $sendtouserId = $memberstoSent->ID;
                                $Userrow = get_user_by("id", $sendtouserId);
                                $sendToEmail = $Userrow->user_email;
                                $mail->addBCC($sendToEmail);
                            }
                            //Subject fron thread
                            $mail->Subject = get_post_meta($thread_id, 'mg_thread_email_subject', true);
                            //Email formating for body
                            //							$mail->IsHTML( true );
                            $mail->MsgHTML($body);
                            $mail->CharSet = 'utf-8';
                            //							$mail->Body = $body;
                            $alt_body = nl2br($mail->html2text($body));
                            $mail->AltBody = $alt_body;
                            //Add attachments to email from thread
                            $args = array('numberposts' => -1, 'post_parent' => $thread_id, 'post_status' => null, 'post_type' => 'attachment');
                            $attachments = get_children($args);
                            if ($attachments) {
                                foreach ($attachments as $attachment) {
                                    if (get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) == 'ATTACHMENT') {
                                        $fullsize_path = get_attached_file($attachment->ID);
                                        $filename_only = basename($fullsize_path);
                                        //If is attached .eml(needs 8bit encoding)
                                        if ($attachment->post_mime_type == 'message/rfc822') {
                                            $mail->addAttachment($fullsize_path, $filename_only, '8bit');
                                        } else {
                                            $mail->addAttachment($fullsize_path, $filename_only);
                                        }
                                    }
                                }
                            }
                            //If email is sent, update status and post
                            if ($mail->Send()) {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Sent');
                                $thread_update = array('ID' => $thread_id, 'post_status' => 'publish');
                                wp_update_post($thread_update);
                            } else {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Error');
                                update_post_meta($thread_id, 'mg_thread_email_status_error', $mail->ErrorInfo);
                            }
                        }
                        //Needs to be updated to SMTP format
                        if ($mail_type == 'php') {
                            $mail_Subject = get_post_meta($thread_id, 'mg_thread_email_subject', true);
                            $to = array();
                            foreach ($user_query->get_results() as $memberstoSent) {
                                $sendtouserId = $memberstoSent->ID;
                                $Userrow = get_user_by("id", $sendtouserId);
                                $sendToEmail = $Userrow->user_email;
                                $to[] = $sendToEmail;
                            }
                            $subject = $mail_Subject;
                            $headers = 'From: ' . $groupTitle . '<' . $groupEmail . '>' . "\r\n";
                            $headers .= 'Reply-To: <' . $senderEmail . '>' . "\r\n";
                            $headers .= 'X-Mailer: PHP' . phpversion() . "\r\n";
                            $headers .= 'MIME-Version: 1.0' . "\r\n";
                            $headers .= 'Content-Type: ' . get_bloginfo('html_type') . '; charset=\\"' . get_bloginfo('charset') . '\\"' . "\r\n";
                            $headers .= 'references: [' . $thread_id . ']' . "\r\n";
                            $headers .= 'Content-type: text/html' . "\r\n";
                            $php_sent = mail($to, $subject, $body, $headers);
                            if ($php_sent) {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Sent');
                                $thread_update = array('ID' => $thread_id, 'post_status' => 'publish');
                                wp_update_post($thread_update);
                            } else {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Error');
                            }
                        }
                        //Needs to be updated to SMTP format
                        if ($mail_type == 'wp') {
                            $mail_Subject = get_post_meta($thread_id, 'mg_thread_email_subject', true);
                            $to = array();
                            foreach ($user_query->get_results() as $memberstoSent) {
                                $sendtouserId = $memberstoSent->ID;
                                $Userrow = get_user_by("id", $sendtouserId);
                                $sendToEmail = $Userrow->user_email;
                                $to[] = $sendToEmail;
                            }
                            $subject = $mail_Subject;
                            $headers[] = 'From: ' . $groupTitle . '<' . $groupEmail . '>' . "\r\n";
                            $headers[] = 'Reply-To: <' . $senderEmail . '>' . "\r\n";
                            /* $headers[] = 'Cc: '. $sendToName .'<'.$sendToEmail.'>'."\r\n"; */
                            $headers[] = 'X-Mailer: PHP' . phpversion() . "\r\n";
                            $headers[] = 'MIME-Version: 1.0' . "\r\n";
                            $headers[] = 'Content-Type: ' . get_bloginfo('html_type') . '; charset=\\"' . get_bloginfo('charset') . '\\"' . "\r\n";
                            $headers[] = 'Content-type: text/html' . "\r\n";
                            $args = array('numberposts' => -1, 'post_parent' => $thread_id, 'post_status' => null, 'post_type' => 'attachment');
                            $attachments = get_children($args);
                            $attachment_send = array();
                            foreach ($attachments as $attachment) {
                                if (get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) === 'ATTACHMENT') {
                                    $fullsize_path = get_attached_file($attachment->ID);
                                    $attachment_send[] = $fullsize_path;
                                }
                            }
                            $wp_sent = wp_mail($to, $subject, $body, $headers, $attachment_send);
                            if ($wp_sent) {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Sent');
                                $thread_update = array('ID' => $thread_id, 'post_status' => 'publish');
                                wp_update_post($thread_update);
                            } else {
                                update_post_meta($thread_id, 'mg_thread_email_status', 'Error');
                            }
                        }
                    } else {
                        echo "No other user subscribed in this group!";
                    }
                } else {
                    echo "No Valid Sender Found in DB!";
                }
            } else {
                echo "No Valid Mailing Group Found!";
            }
        }
    } else {
        echo "No Parsed Email found!";
    }
}