plugin_version); define('SMTP_MAILER_SITE_URL', site_url()); define('SMTP_MAILER_HOME_URL', home_url()); define('SMTP_MAILER_URL', $this->plugin_url()); define('SMTP_MAILER_PATH', $this->plugin_path()); $this->plugin_includes(); $this->loader_operations(); } function plugin_includes() { } function loader_operations() { add_action('plugins_loaded', array($this, 'plugins_loaded_handler')); add_action('admin_menu', array($this, 'options_menu')); //add_action('admin_notices', 'smtp_mailer_admin_notice'); add_filter('pre_wp_mail', 'smtp_mailer_pre_wp_mail', 10, 2); } function plugins_loaded_handler() { if(is_admin() && current_user_can('manage_options')){ add_filter('plugin_action_links', array($this, 'add_plugin_action_links'), 10, 2); } load_plugin_textdomain('smtp-mailer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/'); } function plugin_url() { if ($this->plugin_url) return $this->plugin_url; return $this->plugin_url = plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__)); } function plugin_path() { if ($this->plugin_path) return $this->plugin_path; return $this->plugin_path = untrailingslashit(plugin_dir_path(__FILE__)); } function add_plugin_action_links($links, $file) { if ($file == plugin_basename(dirname(__FILE__) . '/main.php')) { $links[] = ''.__('Settings', 'smtp-mailer').''; } return $links; } function options_menu() { add_options_page(__('SMTP Mailer', 'smtp-mailer'), __('SMTP Mailer', 'smtp-mailer'), 'manage_options', 'smtp-mailer-settings', array($this, 'options_page')); } function options_page() { $plugin_tabs = array( 'smtp-mailer-settings' => __('General', 'smtp-mailer'), 'smtp-mailer-settings&action=test-email' => __('Test Email', 'smtp-mailer'), 'smtp-mailer-settings&action=server-info' => __('Server Info', 'smtp-mailer'), ); $url = "https://wphowto.net/smtp-mailer-plugin-for-wordpress-1482"; $link_text = sprintf(__('Please visit the SMTP Mailer documentation page for usage instructions.', 'smtp-mailer'), esc_url($url)); $allowed_html_tags = array( 'a' => array( 'href' => array(), 'target' => array() ) ); echo '

SMTP Mailer v' . SMTP_MAILER_VERSION . '

'; echo '
'.wp_kses($link_text, $allowed_html_tags).'
'; $current = ''; $action = ''; if (isset($_GET['page'])) { $current = sanitize_text_field($_GET['page']); if (isset($_GET['action'])) { $action = sanitize_text_field($_GET['action']); $current .= "&action=" . $action; } } $content = ''; $content .= ''; $allowed_html_tags = array( 'a' => array( 'href' => array(), 'class' => array() ), 'h2' => array( 'href' => array(), 'class' => array() ) ); echo wp_kses($content, $allowed_html_tags); if(!empty($action)) { switch($action) { case 'test-email': $this->test_email_settings(); break; case 'server-info': $this->server_info_settings(); break; } } else { $this->general_settings(); } echo '
'; } function test_email_settings(){ if(isset($_POST['smtp_mailer_send_test_email'])){ $nonce = $_REQUEST['_wpnonce']; if (!wp_verify_nonce($nonce, 'smtp_mailer_test_email')) { wp_die(__('Error! Nonce Security Check Failed! please send the test email again.', 'smtp-mailer')); } $to = ''; if(isset($_POST['smtp_mailer_to_email']) && !empty($_POST['smtp_mailer_to_email'])){ $to = sanitize_email($_POST['smtp_mailer_to_email']); } $subject = ''; if(isset($_POST['smtp_mailer_email_subject']) && !empty($_POST['smtp_mailer_email_subject'])){ $subject = sanitize_text_field($_POST['smtp_mailer_email_subject']); } $message = ''; if(isset($_POST['smtp_mailer_email_body']) && !empty($_POST['smtp_mailer_email_body'])){ $message = sanitize_text_field($_POST['smtp_mailer_email_body']); } wp_mail($to, $subject, $message); } ?>

'; echo __('Settings Saved!', 'smtp-mailer'); echo '

'; } $options = smtp_mailer_get_option(); if(!is_array($options)){ $options = smtp_mailer_get_empty_options_array(); } // Avoid warning notice since this option was added later if(!isset($options['force_from_address'])){ $options['force_from_address'] = ''; } // Avoid warning notice since this option was added later if(!isset($options['disable_ssl_verification'])){ $options['disable_ssl_verification'] = ''; } ?>

value="1">

value="1">

0 ) { $from_name = substr( $content, 0, $bracket_pos ); $from_name = str_replace( '"', '', $from_name ); $from_name = trim( $from_name ); } $from_email = substr( $content, $bracket_pos + 1 ); $from_email = str_replace( '>', '', $from_email ); $from_email = trim( $from_email ); // Avoid setting an empty $from_email. } elseif ( '' !== trim( $content ) ) { $from_email = trim( $content ); } break; case 'content-type': if ( str_contains( $content, ';' ) ) { list( $type, $charset_content ) = explode( ';', $content ); $content_type = trim( $type ); if ( false !== stripos( $charset_content, 'charset=' ) ) { $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) ); } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) { $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) ); $charset = ''; } // Avoid setting an empty $content_type. } elseif ( '' !== trim( $content ) ) { $content_type = trim( $content ); } break; case 'cc': $cc = array_merge( (array) $cc, explode( ',', $content ) ); break; case 'bcc': $bcc = array_merge( (array) $bcc, explode( ',', $content ) ); break; case 'reply-to': $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); break; default: // Add it to our grand headers array. $headers[ trim( $name ) ] = trim( $content ); break; } } } } // Empty out the values that may be set. $phpmailer->clearAllRecipients(); $phpmailer->clearAttachments(); $phpmailer->clearCustomHeaders(); $phpmailer->clearReplyTos(); $phpmailer->Body = ''; $phpmailer->AltBody = ''; // Set "From" name and email. // If we don't have a name from the input headers. if ( ! isset( $from_name ) ) { $from_name = $options['from_name'];//'WordPress'; } /* * If we don't have an email from the input headers, default to wordpress@$sitename * Some hosts will block outgoing mail from this address if it doesn't exist, * but there's no easy alternative. Defaulting to admin_email might appear to be * another option, but some hosts may refuse to relay mail from an unknown domain. * See https://core.trac.wordpress.org/ticket/5007. */ if ( ! isset( $from_email ) ) { // Get the site domain and get rid of www. $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); $from_email = 'wordpress@'; if ( null !== $sitename ) { if ( str_starts_with( $sitename, 'www.' ) ) { $sitename = substr( $sitename, 4 ); } $from_email .= $sitename; } $from_email = $options['from_email'];//'wordpress@' . $sitename; } /** * Filters the email address to send from. * * @since 2.2.0 * * @param string $from_email Email address to send from. */ $from_email = apply_filters( 'wp_mail_from', $from_email ); /** * Filters the name to associate with the "from" email address. * * @since 2.3.0 * * @param string $from_name Name associated with the "from" email address. */ $from_name = apply_filters( 'wp_mail_from_name', $from_name ); //force from address if checked if(isset($options['force_from_address']) && !empty($options['force_from_address'])){ $from_name = $options['from_name']; $from_email = $options['from_email']; } try { $phpmailer->setFrom( $from_email, $from_name, false ); } catch ( PHPMailer\PHPMailer\Exception $e ) { $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); $mail_error_data['phpmailer_exception_code'] = $e->getCode(); /** This filter is documented in wp-includes/pluggable.php */ do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); return false; } // Set mail's subject and body. $phpmailer->Subject = $subject; $phpmailer->Body = $message; // Set destination addresses, using appropriate methods for handling addresses. $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); foreach ( $address_headers as $address_header => $addresses ) { if ( empty( $addresses ) ) { continue; } foreach ( (array) $addresses as $address ) { try { // Break $recipient into name and address parts if in the format "Foo ". $recipient_name = ''; if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { if ( count( $matches ) === 3 ) { $recipient_name = $matches[1]; $address = $matches[2]; } } switch ( $address_header ) { case 'to': $phpmailer->addAddress( $address, $recipient_name ); break; case 'cc': $phpmailer->addCc( $address, $recipient_name ); break; case 'bcc': $phpmailer->addBcc( $address, $recipient_name ); break; case 'reply_to': $phpmailer->addReplyTo( $address, $recipient_name ); break; } } catch ( PHPMailer\PHPMailer\Exception $e ) { continue; } } } // Tell PHPMailer to use SMTP $phpmailer->isSMTP(); //$phpmailer->isMail(); // Set the hostname of the mail server $phpmailer->Host = $options['smtp_host']; // Whether to use SMTP authentication if(isset($options['smtp_auth']) && $options['smtp_auth'] == "true"){ $phpmailer->SMTPAuth = true; // SMTP username $phpmailer->Username = $options['smtp_username']; // SMTP password $phpmailer->Password = base64_decode($options['smtp_password']); } // Whether to use encryption $type_of_encryption = $options['type_of_encryption']; if($type_of_encryption=="none"){ $type_of_encryption = ''; } $phpmailer->SMTPSecure = $type_of_encryption; // SMTP port $phpmailer->Port = $options['smtp_port']; // Whether to enable TLS encryption automatically if a server supports it $phpmailer->SMTPAutoTLS = false; //enable debug when sending a test mail if(isset($_POST['smtp_mailer_send_test_email'])){ $phpmailer->SMTPDebug = 4; // Ask for HTML-friendly debug output $phpmailer->Debugoutput = 'html'; } //disable ssl certificate verification if checked if(isset($options['disable_ssl_verification']) && !empty($options['disable_ssl_verification'])){ $phpmailer->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); } // Set Content-Type and charset. // If we don't have a Content-Type from the input headers. if ( ! isset( $content_type ) ) { $content_type = 'text/plain'; } /** * Filters the wp_mail() content type. * * @since 2.3.0 * * @param string $content_type Default wp_mail() content type. */ $content_type = apply_filters( 'wp_mail_content_type', $content_type ); $phpmailer->ContentType = $content_type; // Set whether it's plaintext, depending on $content_type. if ( 'text/html' === $content_type ) { $phpmailer->isHTML( true ); } // If we don't have a charset from the input headers. if ( ! isset( $charset ) ) { $charset = get_bloginfo( 'charset' ); } /** * Filters the default wp_mail() charset. * * @since 2.3.0 * * @param string $charset Default email charset. */ $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); // Set custom headers. if ( ! empty( $headers ) ) { foreach ( (array) $headers as $name => $content ) { // Only add custom headers not added automatically by PHPMailer. if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) { try { $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); } catch ( PHPMailer\PHPMailer\Exception $e ) { continue; } } } if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) { $phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) ); } } if ( isset( $attachments ) && ! empty( $attachments ) ) { foreach ( $attachments as $filename => $attachment ) { $filename = is_string( $filename ) ? $filename : ''; try { $phpmailer->addAttachment( $attachment, $filename ); } catch ( PHPMailer\PHPMailer\Exception $e ) { continue; } } } /** * Fires after PHPMailer is initialized. * * @since 2.2.0 * * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference). */ do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); $mail_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); // Send! try { $send = $phpmailer->send(); /** * Fires after PHPMailer has successfully sent an email. * * The firing of this action does not necessarily mean that the recipient(s) received the * email successfully. It only means that the `send` method above was able to * process the request without any errors. * * @since 5.9.0 * * @param array $mail_data { * An array containing the email recipient(s), subject, message, headers, and attachments. * * @type string[] $to Email addresses to send message. * @type string $subject Email subject. * @type string $message Message contents. * @type string[] $headers Additional headers. * @type string[] $attachments Paths to files to attach. * } */ do_action( 'wp_mail_succeeded', $mail_data ); return $send; } catch ( PHPMailer\PHPMailer\Exception $e ) { $mail_data['phpmailer_exception_code'] = $e->getCode(); /** * Fires after a PHPMailer\PHPMailer\Exception is caught. * * @since 4.4.0 * * @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array * containing the mail recipient, subject, message, headers, and attachments. */ do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_data ) ); return false; } }