/**
  * Test smtp settings
  **/
 function test_smtp_ajax()
 {
     global $current_user;
     @set_time_limit(0);
     //Send test email on bounces address
     $email_id = time();
     $email_to = $_REQUEST['smtp_from'];
     $email_from = $_REQUEST['smtp_from'];
     $email_subject = "Test-Connection-Send-" . $email_id;
     $email_contents = 'Test';
     $server_host = $_REQUEST['smtp_host'];
     $server_username = $_REQUEST['smtp_username'];
     $server_password = $_REQUEST['smtp_password'];
     if ($server_password == '********') {
         $settings = $this->get_settings();
         $server_password = $this->_decrypt($settings['smtp_pass']);
     }
     $server_port = $_REQUEST['smtp_port'];
     $server_security = $_REQUEST['smtp_security'];
     if (!class_exists('ePHPMailer')) {
         require_once $this->plugin_dir . "email-newsletter-files/phpmailer/class.phpmailer.php";
     }
     $mail = new ePHPMailer();
     $mail->CharSet = 'UTF-8';
     $mail->IsSMTP();
     $mail->Host = $server_host;
     if ($server_security == 'tls' || $server_security == 'ssl') {
         $mail->SMTPSecure = $server_security;
     }
     if (!empty($server_port)) {
         $mail->Port = $server_port;
     }
     $mail->SMTPAuth = strlen($server_username) > 0;
     if ($mail->SMTPAuth) {
         $mail->Username = esc_attr($server_username);
         $mail->Password = $server_password;
     }
     $mail->From = $email_from;
     $mail->Sender = $email_from;
     $mail->Subject = $email_subject;
     $mail->isHTML(true);
     $mail->MsgHTML($email_contents);
     $mail->AddAddress($email_to);
     $send_status = $mail->Send();
     if ($send_status != true) {
         die(__('Failed to send test email! - Please check your outgoing email settings and server config to see if selected ports are open. Error details: ', 'email-newsletter') . strip_tags($mail->ErrorInfo));
     } else {
         die(__('Test message successfully sent! Feel free to save your settings. Please keep in mind that your server may limit the number of allowed messages to be sent per hour/day/week.', 'email-newsletter'));
     }
 }
 /**
  * Send email
  **/
 function send_email($email_from_name, $email_from, $email_to, $email_subject, $email_contents, $options = array())
 {
     global $enewsletter_send_options;
     $enewsletter_send_options = $options;
     $email_contents = wordwrap($email_contents, 50);
     if ($this->settings['outbound_type'] == 'wpmail') {
         add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
         add_filter('wp_mail_from', create_function('', 'return "' . $email_from . '"; '));
         if ($email_from_name) {
             add_filter('wp_mail_from_name', create_function('', 'return "' . $email_from_name . '"; '));
         }
         add_action('phpmailer_init', array($this, 'wp_mail_phpmailer_init'));
         $this->send_options = $enewsletter_send_options;
         $headers = array();
         $headers[] = $email_from_name ? 'From: ' . $email_from_name . ' <' . $email_from . '>' : 'From: <' . $email_from . '>';
         if (isset($options['bounce_email'])) {
             $headers[] = 'Return-Path: <' . $options['bounce_email'] . '>';
         }
         if (isset($options['message_id'])) {
             $headers[] = 'X-Mailer: ' . $options['message_id'];
             $headers[] = 'Message-ID: ' . $options['message_id'];
         }
         $sent_status = wp_mail($email_to, $email_subject, $email_contents, $headers);
         if (!$sent_status) {
             $this->write_log('WP Mail send email error');
             return 'WP Mail send email error';
         }
     } else {
         if (!class_exists('ePHPMailer')) {
             require_once $this->plugin_dir . "email-newsletter-files/phpmailer/class.phpmailer.php";
         }
         $mail = new ePHPMailer();
         $mail->CharSet = 'UTF-8';
         //Set Sending Method
         switch ($this->settings['outbound_type']) {
             case 'smtp':
                 $mail->IsSMTP();
                 $mail->Host = $this->settings['smtp_host'];
                 if ($this->settings['smtp_secure_method'] == 'tls' || $this->settings['smtp_secure_method'] == 'ssl') {
                     $mail->SMTPSecure = $this->settings['smtp_secure_method'];
                 }
                 if (!empty($this->settings['smtp_port'])) {
                     $mail->Port = $this->settings['smtp_port'];
                 }
                 $mail->SMTPAuth = strlen($this->settings['smtp_user']) > 0;
                 if ($mail->SMTPAuth) {
                     $mail->Username = esc_attr($this->settings['smtp_user']);
                     $mail->Password = $this->_decrypt($this->settings['smtp_pass']);
                 }
                 break;
             case 'mail':
                 $mail->IsMail();
                 break;
             case 'sendmail':
                 $mail->IsSendmail();
                 break;
         }
         $mail->From = $email_from;
         if ($email_from_name) {
             $mail->FromName = $email_from_name;
         }
         $mail->Subject = $email_subject;
         $mail->isHTML(true);
         $mail->MsgHTML($email_contents);
         $mail->AddAddress($email_to);
         if (isset($options['bounce_email'])) {
             $mail->Sender = $options['bounce_email'];
         } else {
             $mail->Sender = $email_from;
         }
         if (isset($options['message_id'])) {
             $mail->XMailer = $options['message_id'];
             $mail->MessageID = $options['message_id'];
         }
         $sent_status = $mail->Send();
         if (!$sent_status) {
             $this->write_log('Send email error: ' . $mail->ErrorInfo . '[' . $mail->ErrorInfoRaw . ']');
             return $mail->ErrorInfoRaw;
         }
     }
     $wait_time = isset($options['wait']) ? $options['wait'] : 1;
     sleep($wait_time);
     return true;
 }