Advertisement
dudinmaster

easy-wp-smtp.php

Aug 29th, 2017
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.48 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Easy WP SMTP
  4. Version: 1.2.6
  5. Plugin URI: https://wp-ecommerce.net/easy-wordpress-smtp-send-emails-from-your-wordpress-site-using-a-smtp-server-2197
  6. Author: wpecommerce
  7. Author URI: https://wp-ecommerce.net/
  8. Description: Send email via SMTP from your WordPress Blog
  9. Text Domain: easy-wp-smtp
  10. Domain Path: /languages
  11. */
  12.  
  13. //Prefix/Slug - swpsmtp
  14.  
  15. include_once('easy-wp-smtp-admin-menu.php');
  16.  
  17. /**
  18.  * Add action links on plugin page in to Plugin Name block
  19.  * @param $links array() action links
  20.  * @param $file  string  relative path to pugin "easy-wp-smtp/easy-wp-smtp.php"
  21.  * @return $links array() action links
  22.  */
  23. if (!function_exists('swpsmtp_plugin_action_links')) {
  24.  
  25.     function swpsmtp_plugin_action_links($links, $file) {
  26.         /* Static so we don't call plugin_basename on every plugin row. */
  27.         static $this_plugin;
  28.         if (!$this_plugin) {
  29.             $this_plugin = plugin_basename(__FILE__);
  30.         }
  31.         if ($file == $this_plugin) {
  32.             $settings_link = '<a href="options-general.php?page=swpsmtp_settings">' . __('Settings', 'easy-wp-smtp') . '</a>';
  33.             array_unshift($links, $settings_link);
  34.         }
  35.         return $links;
  36.     }
  37.  
  38. }
  39.  
  40. /**
  41.  * Add action links on plugin page in to Plugin Description block
  42.  * @param $links array() action links
  43.  * @param $file  string  relative path to pugin "easy-wp-smtp/easy-wp-smtp.php"
  44.  * @return $links array() action links
  45.  */
  46. if (!function_exists('swpsmtp_register_plugin_links')) {
  47.  
  48.     function swpsmtp_register_plugin_links($links, $file) {
  49.         $base = plugin_basename(__FILE__);
  50.         if ($file == $base) {
  51.             $links[] = '<a href="options-general.php?page=swpsmtp_settings">' . __('Settings', 'easy-wp-smtp') . '</a>';
  52.         }
  53.         return $links;
  54.     }
  55.  
  56. }
  57.  
  58. //plugins_loaded action hook handler
  59. if (!function_exists('swpsmtp_plugins_loaded_handler')) {
  60.  
  61.     function swpsmtp_plugins_loaded_handler() {
  62.         load_plugin_textdomain('easy-wp-smtp', false, dirname(plugin_basename(__FILE__)) . '/languages/');
  63.     }
  64.  
  65. }
  66.  
  67.  
  68. /**
  69.  * Function to add plugin scripts
  70.  * @return void
  71.  */
  72. if (!function_exists('swpsmtp_admin_head')) {
  73.  
  74.     function swpsmtp_admin_head() {
  75.         wp_enqueue_style('swpsmtp_stylesheet', plugins_url('css/style.css', __FILE__));
  76.  
  77.         if (isset($_REQUEST['page']) && 'swpsmtp_settings' == $_REQUEST['page']) {
  78.             wp_enqueue_script('swpsmtp_script', plugins_url('js/script.js', __FILE__), array('jquery'));
  79.         }
  80.     }
  81.  
  82. }
  83.  
  84. /**
  85.  * Function to add smtp options in the phpmailer_init
  86.  * @return void
  87.  */
  88. if (!function_exists('swpsmtp_init_smtp')) {
  89.  
  90.     function swpsmtp_init_smtp($phpmailer) {
  91.         //check if SMTP credentials have been configured.
  92.         if (!swpsmtp_credentials_configured()) {
  93.             return;
  94.         }
  95.         $swpsmtp_options = get_option('swpsmtp_options');
  96.         /* Set the mailer type as per config above, this overrides the already called isMail method */
  97.         $phpmailer->IsSMTP();
  98.         $from_email = $swpsmtp_options['from_email_field'];
  99.         $phpmailer->From = $from_email;
  100.         $from_name = $swpsmtp_options['from_name_field'];
  101.         $phpmailer->FromName = $from_name;
  102.         $phpmailer->SetFrom($phpmailer->From, $phpmailer->FromName);
  103.         /* Set the SMTPSecure value */
  104.         if ($swpsmtp_options['smtp_settings']['type_encryption'] !== 'none') {
  105.             $phpmailer->SMTPSecure = $swpsmtp_options['smtp_settings']['type_encryption'];
  106.         }
  107.  
  108.         /* Set the other options */
  109.         $phpmailer->Host = $swpsmtp_options['smtp_settings']['host'];
  110.         $phpmailer->Port = $swpsmtp_options['smtp_settings']['port'];
  111.  
  112.         /* If we're using smtp auth, set the username & password */
  113.         if ('yes' == $swpsmtp_options['smtp_settings']['autentication']) {
  114.             $phpmailer->SMTPAuth = true;
  115.             $phpmailer->Username = $swpsmtp_options['smtp_settings']['username'];
  116.             $phpmailer->Password = swpsmtp_get_password();
  117.         }
  118.         //PHPMailer 5.2.10 introduced this option. However, this might cause issues if the server is advertising TLS with an invalid certificate.
  119.         $phpmailer->SMTPAutoTLS = false;
  120.     }
  121.  
  122. }
  123.  
  124. /**
  125.  * Function to test mail sending
  126.  * @return text or errors
  127.  */
  128. if (!function_exists('swpsmtp_test_mail')) {
  129.  
  130.     function swpsmtp_test_mail($to_email, $subject, $message) {
  131.         if (!swpsmtp_credentials_configured()) {
  132.             return;
  133.         }
  134.         $errors = '';
  135.  
  136.         $swpsmtp_options = get_option('swpsmtp_options');
  137.  
  138.         require_once( ABSPATH . WPINC . '/class-phpmailer.php' );
  139.         $mail = new PHPMailer();
  140.        
  141.         $mail->Hostname = "uvcms1.pusathosting.info";
  142.         $charset = get_bloginfo('charset');
  143.         $mail->CharSet = $charset;
  144.  
  145.         $from_name = $swpsmtp_options['from_name_field'];
  146.         $from_email = $swpsmtp_options['from_email_field'];
  147.  
  148.         $mail->IsSMTP();
  149.  
  150.         /* If using smtp auth, set the username & password */
  151.         if ('yes' == $swpsmtp_options['smtp_settings']['autentication']) {
  152.             $mail->SMTPAuth = true;
  153.             $mail->Username = $swpsmtp_options['smtp_settings']['username'];
  154.             $mail->Password = swpsmtp_get_password();
  155.         }
  156.  
  157.         /* Set the SMTPSecure value, if set to none, leave this blank */
  158.         if ($swpsmtp_options['smtp_settings']['type_encryption'] !== 'none') {
  159.             $mail->SMTPSecure = $swpsmtp_options['smtp_settings']['type_encryption'];
  160.         }
  161.  
  162.         /* PHPMailer 5.2.10 introduced this option. However, this might cause issues if the server is advertising TLS with an invalid certificate. */
  163.         $mail->SMTPAutoTLS = false;
  164.  
  165.         /* Set the other options */
  166.         $mail->Host = $swpsmtp_options['smtp_settings']['host'];
  167.         $mail->Port = $swpsmtp_options['smtp_settings']['port'];
  168.         $mail->SetFrom($from_email, $from_name);
  169.         $mail->isHTML(true);
  170.         $mail->Subject = $subject;
  171.         $mail->MsgHTML($message);
  172.         $mail->AddAddress($to_email);
  173.         $mail->SMTPDebug = 0;
  174.  
  175.         /* Send mail and return result */
  176.         if (!$mail->Send())
  177.             $errors = $mail->ErrorInfo;
  178.  
  179.         $mail->ClearAddresses();
  180.         $mail->ClearAllRecipients();
  181.  
  182.         if (!empty($errors)) {
  183.             return $errors;
  184.         } else {
  185.             return 'Test mail was sent';
  186.         }
  187.     }
  188.  
  189. }
  190.  
  191. if (!function_exists('swpsmtp_get_password')) {
  192.  
  193.     function swpsmtp_get_password() {
  194.         $swpsmtp_options = get_option('swpsmtp_options');
  195.         $temp_password = $swpsmtp_options['smtp_settings']['password'];
  196.         $password = "";
  197.         $decoded_pass = base64_decode($temp_password);
  198.         /* no additional checks for servers that aren't configured with mbstring enabled */
  199.         if (!function_exists('mb_detect_encoding')) {
  200.             return $decoded_pass;
  201.         }
  202.         /* end of mbstring check */
  203.         if (base64_encode($decoded_pass) === $temp_password) {  //it might be encoded
  204.             if (false === mb_detect_encoding($decoded_pass)) {  //could not find character encoding.
  205.                 $password = $temp_password;
  206.             } else {
  207.                 $password = base64_decode($temp_password);
  208.             }
  209.         } else { //not encoded
  210.             $password = $temp_password;
  211.         }
  212.         return $password;
  213.     }
  214.  
  215. }
  216.  
  217. if (!function_exists('swpsmtp_admin_notice')) {
  218.  
  219.     function swpsmtp_admin_notice() {
  220.         if (!swpsmtp_credentials_configured()) {
  221.             $settings_url = admin_url() . 'options-general.php?page=swpsmtp_settings';
  222.             ?>
  223.             <div class="error">
  224.                 <p><?php printf(__('Please configure your SMTP credentials in the <a href="%s">settings menu</a> in order to send email using Easy WP SMTP plugin.', 'easy-wp-smtp'), esc_url($settings_url)); ?></p>
  225.             </div>
  226.             <?php
  227.         }
  228.     }
  229.  
  230. }
  231.  
  232. if (!function_exists('swpsmtp_credentials_configured')) {
  233.  
  234.     function swpsmtp_credentials_configured() {
  235.         $swpsmtp_options = get_option('swpsmtp_options');
  236.         $credentials_configured = true;
  237.         if (!isset($swpsmtp_options['from_email_field']) || empty($swpsmtp_options['from_email_field'])) {
  238.             $credentials_configured = false;
  239.         }
  240.         if (!isset($swpsmtp_options['from_name_field']) || empty($swpsmtp_options['from_name_field'])) {
  241.             $credentials_configured = false;
  242.             ;
  243.         }
  244.         return $credentials_configured;
  245.     }
  246.  
  247. }
  248.  
  249. /**
  250.  * Performed at uninstal.
  251.  * @return void
  252.  */
  253. if (!function_exists('swpsmtp_send_uninstall')) {
  254.  
  255.     function swpsmtp_send_uninstall() {
  256.         /* Don't delete plugin options. It is better to retain the options so if someone accidentally deactivates, the configuration is not lost. */
  257.        
  258.         //delete_site_option('swpsmtp_options');
  259.         //delete_option('swpsmtp_options');
  260.     }
  261.  
  262. }
  263.  
  264. /**
  265.  * Add all hooks
  266.  */
  267. add_filter('plugin_action_links', 'swpsmtp_plugin_action_links', 10, 2);
  268. add_action('plugins_loaded', 'swpsmtp_plugins_loaded_handler');
  269. add_filter('plugin_row_meta', 'swpsmtp_register_plugin_links', 10, 2);
  270.  
  271. add_action('phpmailer_init', 'swpsmtp_init_smtp');
  272.  
  273. add_action('admin_menu', 'swpsmtp_admin_default_setup');
  274.  
  275. add_action('admin_init', 'swpsmtp_admin_init');
  276. add_action('admin_enqueue_scripts', 'swpsmtp_admin_head');
  277. add_action('admin_notices', 'swpsmtp_admin_notice');
  278.  
  279. register_uninstall_hook(plugin_basename(__FILE__), 'swpsmtp_send_uninstall');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement