Untitled

By Scribby Meerkat, 2 Days ago, written in php.
  1. <?php
  2. /*
  3. Plugin Name: WP-Mail-SMTP
  4. Version: 0.8.6
  5. Plugin URI: http://www.callum-macdonald.com/code/wp-mail-smtp/
  6. Description: Reconfigures the wp_mail() function to use SMTP instead of mail() and creates an options page to manage the settings.
  7. Author: Callum Macdonald
  8. Author URI: http://www.callum-macdonald.com/
  9. */
  10.  
  11. /**
  12.  * @author Callum Macdonald
  13.  * @copyright Callum Macdonald, 2007-8, All Rights Reserved
  14.  * This code is released under the GPL licence version 3 or later, available here
  15.  * http://www.gnu.org/licenses/gpl.txt
  16.  */
  17.  
  18. /**
  19.  * Setting options in wp-config.php
  20.  *
  21.  * Specifically aimed at WPMU users, you can set the options for this plugin as
  22.  * constants in wp-config.php. This disables the plugin's admin page and may
  23.  * improve performance very slightly. Copy the code below into wp-config.php.
  24.  */
  25. /*
  26. define('WPMS_ON', true);
  27. define('WPMS_MAIL_FROM', 'From Email');
  28. define('WPMS_MAIL_FROM_NAME', 'From Name');
  29. define('WPMS_MAILER', 'smtp'); // Possible values 'smtp', 'mail', or 'sendmail'
  30. define('WPMS_SMTP_HOST', 'localhost'); // The SMTP mail host
  31. define('WPMS_SMTP_PORT', 25); // The SMTP server port number
  32. define('WPMS_SSL', ''); // Possible values '', 'ssl', 'tls' - note TLS is not STARTTLS
  33. define('WPMS_SMTP_AUTH', true); // True turns on SMTP authentication, false turns it off
  34. define('WPMS_SMTP_USER', 'username'); // SMTP authentication username, only used if WPMS_SMTP_AUTH is true
  35. define('WPMS_SMTP_PASS', 'password'); // SMTP authentication password, only used if WPMS_SMTP_AUTH is true
  36. */
  37.  
  38. /**
  39.  * CHANGELOG
  40.  *
  41.  * 0.8.6 - The Settings link really does work this time, promise. Apologies for the unnecessary updates.
  42.  * 0.8.5 - Bugfix, the settings link on the Plugin page was broken by 0.8.4.
  43.  * 0.8.4 - Minor bugfix, remove use of esc_html() to improve backwards compatibility. Removed second options page menu props ovidiu.
  44.  * 0.8.3 - Bugfix, return WPMS_MAIL_FROM_NAME, props nacin. Add Settings link, props MikeChallis.
  45.  * 0.8.2 - Bugfix, call phpmailer_init_smtp() correctly, props Sinklar.
  46.  * 0.8.1 - Internationalisation improvements.
  47.  * 0.8 - Added port, SSL/TLS, option whitelisting, validate_email(), and constant options.
  48.  * 0.7 - Added checks to only override the default from name / email
  49.  * 0.6 - Added additional SMTP debugging output
  50.  * 0.5.2 - Fixed a pre 2.3 bug to do with mail from
  51.  * 0.5.1 - Added a check to display a warning on versions prior to 2.3
  52.  * 0.5.0 - Upgraded to match 2.3 filters which add a second filter for from name
  53.  * 0.4.2 - Fixed a bug in 0.4.1 and added more debugging output
  54.  * 0.4.1 - Added $phpmailer->ErroInfo to the test mail output
  55.  * 0.4 - Added the test email feature and cleaned up some other bits and pieces
  56.  * 0.3.2 - Changed to use register_activation_hook for greater compatability
  57.  * 0.3.1 - Added readme for WP-Plugins.org compatability
  58.  * 0.3 - Various bugfixes and added From options
  59.  * 0.2 - Reworked approach as suggested by westi, added options page
  60.  * 0.1 - Initial approach, copying the wp_mail function and replacing it
  61.  */
  62.  
  63. // Array of options and their default values
  64. $wpms_options = array (
  65.         'mail_from' => '',
  66.         'mail_from_name' => '',
  67.         'mailer' => 'smtp',
  68.         'smtp_host' => 'localhost',
  69.         'smtp_port' => '25',
  70.         'smtp_ssl' => 'none',
  71.         'smtp_auth' => false,
  72.         'smtp_user' => '',
  73.         'smtp_pass' => ''
  74. );
  75.  
  76.  
  77. /**
  78.  * Activation function. This function creates the required options and defaults.
  79.  */
  80. if (!function_exists('wp_mail_smtp_activate')) :
  81. function wp_mail_smtp_activate() {
  82.        
  83.         global $wpms_options;
  84.        
  85.         // Create the required options...
  86.         foreach ($wpms_options as $name => $val) {
  87.                 add_option($name,$val);
  88.         }
  89.        
  90. }
  91. endif;
  92.  
  93. if (!function_exists('wp_mail_smtp_whitelist_options')) :
  94. function wp_mail_smtp_whitelist_options($whitelist_options) {
  95.        
  96.         global $wpms_options;
  97.        
  98.         // Add our options to the array
  99.         $whitelist_options['email'] = array_keys($wpms_options);
  100.        
  101.         return $whitelist_options;
  102.        
  103. }
  104. endif;
  105.  
  106. // To avoid any (very unlikely) clashes, check if the function alredy exists
  107. if (!function_exists('phpmailer_init_smtp')) :
  108. // This code is copied, from wp-includes/pluggable.php as at version 2.2.2
  109. function phpmailer_init_smtp($phpmailer) {
  110.        
  111.         // If constants are defined, apply those options
  112.         if (defined('WPMS_ON') && WPMS_ON) {
  113.                
  114.                 $phpmailer->Mailer = WPMS_MAILER;
  115.                
  116.                 if (WPMS_MAILER == 'smtp') {
  117.                         $phpmailer->SMTPSecure = WPMS_SSL;
  118.                         $phpmailer->Host = WPMS_SMTP_HOST;
  119.                         $phpmailer->Port = WPMS_SMTP_PORT;
  120.                         if (WPMS_SMTP_AUTH) {
  121.                                 $phpmailer->SMTPAuth = true;
  122.                                 $phpmailer->Username = WPMS_SMTP_USER;
  123.                                 $phpmailer->Password = WPMS_SMTP_PASS;
  124.                         }
  125.                 }
  126.                
  127.                 // If you're using contstants, set any custom options here
  128.                
  129.         }
  130.         else {
  131.                
  132.                 // Check that mailer is not blank, and if mailer=smtp, host is not blank
  133.                 if ( ! get_option('mailer') || ( get_option('mailer') == 'smtp' && ! get_option('smtp_host') ) ) {
  134.                         return;
  135.                 }
  136.                
  137.                 // Set the mailer type as per config above, this overrides the already called isMail method
  138.                 $phpmailer->Mailer = get_option('mailer');
  139.                
  140.                 // Set the SMTPSecure value, if set to none, leave this blank
  141.                 $phpmailer->SMTPSecure = get_option('smtp_ssl') == 'none' ? '' : get_option('smtp_ssl');
  142.                
  143.                 // If we're sending via SMTP, set the host
  144.                 if (get_option('mailer') == "smtp") {
  145.                        
  146.                         // Set the SMTPSecure value, if set to none, leave this blank
  147.                         $phpmailer->SMTPSecure = get_option('smtp_ssl') == 'none' ? '' : get_option('smtp_ssl');
  148.                        
  149.                         // Set the other options
  150.                         $phpmailer->Host = get_option('smtp_host');
  151.                         $phpmailer->Port = get_option('smtp_port');
  152.                        
  153.                         // If we're using smtp auth, set the username & password
  154.                         if (get_option('smtp_auth') == "true") {
  155.                                 $phpmailer->SMTPAuth = TRUE;
  156.                                 $phpmailer->Username = get_option('smtp_user');
  157.                                 $phpmailer->Password = get_option('smtp_pass');
  158.                         }
  159.                 }
  160.                
  161.                 // You can add your own options here, see the phpmailer documentation for more info:
  162.                 // http://phpmailer.sourceforge.net/docs/
  163.                
  164.                
  165.                
  166.                 // STOP adding options here.
  167.                
  168.         }
  169.        
  170. } // End of phpmailer_init_smtp() function definition
  171. endif;
  172.  
  173.  
  174.  
  175. /**
  176.  * This function outputs the plugin options page.
  177.  */
  178. if (!function_exists('wp_mail_smtp_options_page')) :
  179. // Define the function
  180. function wp_mail_smtp_options_page() {
  181.        
  182.         // Load the options
  183.         global $wpms_options, $phpmailer;
  184.        
  185.         // Make sure the PHPMailer class has been instantiated
  186.         // (copied verbatim from wp-includes/pluggable.php)
  187.         // (Re)create it, if it's gone missing
  188.         if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
  189.                 require_once ABSPATH . WPINC . '/class-phpmailer.php';
  190.                 require_once ABSPATH . WPINC . '/class-smtp.php';
  191.                 $phpmailer = new PHPMailer();
  192.         }
  193.  
  194.         // Send a test mail if necessary
  195.         if (isset($_POST['wpms_action']) && $_POST['wpms_action'] == __('Send Test', 'wp_mail_smtp') && isset($_POST['to'])) {
  196.                
  197.                 // Set up the mail variables
  198.                 $to = $_POST['to'];
  199.                 $subject = 'WP Mail SMTP: ' . __('Test mail to ', 'wp_mail_smtp') . $to;
  200.                 $message = __('This is a test email generated by the WP Mail SMTP WordPress plugin.', 'wp_mail_smtp');
  201.                
  202.                 // Set SMTPDebug to level 2
  203.                 $phpmailer->SMTPDebug = 2;
  204.                
  205.                 // Start output buffering to grab smtp debugging output
  206.                 ob_start();
  207.  
  208.                 // Send the test mail
  209.                 $result = wp_mail($to,$subject,$message);
  210.                
  211.                 // Grab the smtp debugging output
  212.                 $smtp_debug = ob_get_clean();
  213.                
  214.                 // Output the response
  215.                 ?>
  216. <div id="message" class="updated fade"><p><strong><?php _e('Test Message Sent', 'wp_mail_smtp'); ?></strong></p>
  217. <p><?php _e('The result was:', 'wp_mail_smtp'); ?></p>
  218. <pre><?php var_dump($result); ?></pre>
  219. <?php if ($result != true) { ?>
  220. <p><?php _e('The full debugging output is shown below:', 'wp_mail_smtp'); ?></p>
  221. <pre><?php var_dump($phpmailer); ?></pre>
  222. <?php } ?>
  223. <p><?php _e('The SMTP debugging output is shown below:', 'wp_mail_smtp'); ?></p>
  224. <pre><?php echo $smtp_debug ?></pre>
  225. </div>
  226.                 <?php
  227.  
  228.         }
  229.        
  230.         ?>
  231. <div class="wrap">
  232. <h2><?php _e('Advanced Email Options', 'wp_mail_smtp'); ?></h2>
  233. <form method="post" action="options.php">
  234. <?php wp_nonce_field('email-options'); ?>
  235. <p class="submit"><input type="submit" name="Submit" value="<?php _e('Update Options &raquo;', 'wp_mail_smtp'); ?>" />
  236. <fieldset class="options">
  237. <legend><?php _e('From', 'wp_mail_smtp'); ?></legend>
  238. <table class="optiontable">
  239. <tr valign="top">
  240. <th scope="row"><?php _e('From Email:', 'wp_mail_smtp'); ?> </th>
  241. <td><p><input name="mail_from" type="text" id="mail_from" value="<?php print(get_option('mail_from')); ?>" size="40" class="code" /><br />
  242. <?php _e('You can specify the email address that emails should be sent from. If you leave this blank, the admin email will be used.', 'wp_mail_smtp'); if(get_option('db_version') < 6124) { print('<br /><span style="color: red;">'); _e('<strong>Please Note:</strong> You appear to be using a version of WordPress prior to 2.3. Please ignore the From Name field and instead enter Name&lt;[email protected]&gt; in this field.', 'wp_mail_smtp'); print('</span>'); } ?></p></td>
  243. </tr>
  244. <tr valign="top">
  245. <th scope="row"><?php _e('From Name:', 'wp_mail_smtp'); ?> </th>
  246. <td><p><input name="mail_from_name" type="text" id="mail_from_name" value="<?php print(get_option('mail_from_name')); ?>" size="40" class="code" /><br />
  247. <?php _e('You can specify the name that emails should be sent from. If you leave this blank, the emails will be sent from WordPress.', 'wp_mail_smtp'); ?></p></td>
  248. </tr>
  249. </table>
  250.  
  251. <legend><?php _e('Mailer', 'wp_mail_smtp'); ?></legend>
  252. <table class="optiontable">
  253. <tr valign="top">
  254. <th scope="row"><?php _e('Mailer:', 'wp_mail_smtp'); ?> </th>
  255. <td>
  256. <p><input id="mailer_smtp" type="radio" name="mailer" value="smtp" <?php checked('smtp', get_option('mailer')); ?> />
  257. <label for="mailer_smtp"><?php _e('Send all WordPress emails via SMTP.', 'wp_mail_smtp'); ?></label></p>
  258. <p><input id="mailer_mail" type="radio" name="mailer" value="mail" <?php checked('mail', get_option('mailer')); ?> />
  259. <label for="mailer_mail"><?php _e('Use the PHP mail() function to send emails.', 'wp_mail_smtp'); ?></label></p>
  260. </td>
  261. </tr>
  262. </table>
  263.  
  264. <legend><?php _e('SMTP Options', 'wp_mail_smtp'); ?></legend>
  265. <p><?php _e('These options only apply if you have chosen to send mail by SMTP above.', 'wp_mail_smtp'); ?></p>
  266. <table class="optiontable">
  267. <tr valign="top">
  268. <th scope="row"><?php _e('SMTP Host:', 'wp_mail_smtp'); ?> </th>
  269. <td><input name="smtp_host" type="text" id="smtp_host" value="<?php print(get_option('smtp_host')); ?>" size="40" class="code" /></td>
  270. </tr>
  271. <tr valign="top">
  272. <th scope="row"><?php _e('SMTP Port:', 'wp_mail_smtp'); ?> </th>
  273. <td><input name="smtp_port" type="text" id="smtp_port" value="<?php print(get_option('smtp_port')); ?>" size="6" class="code" /></td>
  274. </tr>
  275. <tr valign="top">
  276. <th scope="row"><?php _e('Encryption:', 'wp_mail_smtp'); ?> </th>
  277. <td>
  278. <p><input id="smtp_ssl_none" type="radio" name="smtp_ssl" value="none" <?php checked('none', get_option('smtp_ssl')); ?> />
  279. <label for="smtp_ssl_none"><?php _e('No encryption.', 'wp_mail_smtp'); ?></label></p>
  280. <p><input id="smtp_ssl_ssl" type="radio" name="smtp_ssl" value="ssl" <?php checked('ssl', get_option('smtp_ssl')); ?> />
  281. <label for="smtp_ssl_ssl"><?php _e('Use SSL encryption.', 'wp_mail_smtp'); ?></label></p>
  282. <p><input id="smtp_ssl_tls" type="radio" name="smtp_ssl" value="tls" <?php checked('tls', get_option('smtp_ssl')); ?> />
  283. <label for="smtp_ssl_tls"><?php _e('Use TLS encryption. This is not the same as STARTTLS. For most servers SSL is the recommended option.', 'wp_mail_smtp'); ?></label></p>
  284. </td>
  285. </tr>
  286. <tr valign="top">
  287. <th scope="row"><?php _e('Authentication:', 'wp_mail_smtp'); ?> </th>
  288. <td>
  289. <p><input id="smtp_auth_false" type="radio" name="smtp_auth" value="false" <?php checked('false', get_option('smtp_auth')); ?> />
  290. <label for="smtp_auth_false"><?php _e('No: Do not use SMTP authentication.', 'wp_mail_smtp'); ?></label></p>
  291. <p><input id="smtp_auth_true" type="radio" name="smtp_auth" value="true" <?php checked('true', get_option('smtp_auth')); ?> />
  292. <label for="smtp_auth_true"><?php _e('Yes: Use SMTP authentication.', 'wp_mail_smtp'); ?></label></p>
  293. <p><?php _e('If this is set to no, the values below are ignored.', 'wp_mail_smtp'); ?></p>
  294. </td>
  295. </tr>
  296. <tr valign="top">
  297. <th scope="row"><?php _e('Username:', 'wp_mail_smtp'); ?> </th>
  298. <td><input name="smtp_user" type="text" id="smtp_user" value="<?php print(get_option('smtp_user')); ?>" size="40" class="code" /></td>
  299. </tr>
  300. <tr valign="top">
  301. <th scope="row"><?php _e('Password:', 'wp_mail_smtp'); ?> </th>
  302. <td><input name="smtp_pass" type="text" id="smtp_pass" value="<?php print(get_option('smtp_pass')); ?>" size="40" class="code" /></td>
  303. </tr>
  304. </table>
  305.  
  306. <p class="submit"><input type="submit" name="Submit" value="<?php _e('Update Options &raquo;', 'wp_mail_smtp'); ?>" />
  307. <input type="hidden" name="action" value="update" />
  308. </p>
  309. <input type="hidden" name="option_page" value="email">
  310. </fieldset>
  311. </form>
  312.  
  313. <form method="POST">
  314. <fieldset class="options">
  315. <legend><?php _e('Send a Test Email', 'wp_mail_smtp'); ?></legend>
  316. <table class="optiontable">
  317. <tr valign="top">
  318. <th scope="row"><?php _e('To:', 'wp_mail_smtp'); ?> </th>
  319. <td><p><input name="to" type="text" id="to" value="" size="40" class="code" /><br />
  320. <?php _e('Type an email address here and then click Send Test to generate a test email.', 'wp_mail_smtp'); ?></p></td>
  321. </tr>
  322. </table>
  323. <p class="submit"><input type="submit" name="wpms_action" value="<?php _e('Send Test', 'wp_mail_smtp'); ?>" /></p>
  324. </fieldset>
  325. </form>
  326.  
  327. </div>
  328.         <?php
  329.        
  330. } // End of wp_mail_smtp_options_page() function definition
  331. endif;
  332.  
  333.  
  334. /**
  335.  * This function adds the required page (only 1 at the moment).
  336.  */
  337. if (!function_exists('wp_mail_smtp_menus')) :
  338. function wp_mail_smtp_menus() {
  339.        
  340.         if (function_exists('add_submenu_page')) {
  341.                 add_options_page(__('Advanced Email Options', 'wp_mail_smtp'),__('Email', 'wp_mail_smtp'),'manage_options',__FILE__,'wp_mail_smtp_options_page');
  342.         }
  343.        
  344. } // End of wp_mail_smtp_menus() function definition
  345. endif;
  346.  
  347.  
  348. /**
  349.  * This is copied directly from WPMU wp-includes/wpmu-functions.php
  350.  */
  351. if (!function_exists('validate_email')) :
  352. function validate_email( $email, $check_domain = true) {
  353.     if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.
  354.         '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
  355.         '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email))
  356.     {
  357.         if ($check_domain && function_exists('checkdnsrr')) {
  358.             list (, $domain)  = explode('@', $email);
  359.  
  360.             if (checkdnsrr($domain.'.', 'MX') || checkdnsrr($domain.'.', 'A')) {
  361.                 return true;
  362.             }
  363.             return false;
  364.         }
  365.         return true;
  366.     }
  367.     return false;
  368. } // End of validate_email() function definition
  369. endif;
  370.  
  371.  
  372. /**
  373.  * This function sets the from email value
  374.  */
  375. if (!function_exists('wp_mail_smtp_mail_from')) :
  376. function wp_mail_smtp_mail_from ($orig) {
  377.        
  378.         // This is copied from pluggable.php lines 348-354 as at revision 10150
  379.         // http://trac.wordpress.org/browser/branches/2.7/wp-includes/pluggable.php#L348
  380.        
  381.         // Get the site domain and get rid of www.
  382.         $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  383.         if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  384.                 $sitename = substr( $sitename, 4 );
  385.         }
  386.  
  387.         $default_from = 'wordpress@' . $sitename;
  388.         // End of copied code
  389.        
  390.         // If the from email is not the default, return it unchanged
  391.         if ( $orig != $default_from ) {
  392.                 return $orig;
  393.         }
  394.        
  395.         if (defined('WPMS_ON') && WPMS_ON)
  396.                 return WPMS_MAIL_FROM;
  397.         elseif (validate_email(get_option('mail_from'), false))
  398.                 return get_option('mail_from');
  399.        
  400.         // If in doubt, return the original value
  401.         return $orig;
  402.        
  403. } // End of wp_mail_smtp_mail_from() function definition
  404. endif;
  405.  
  406.  
  407. /**
  408.  * This function sets the from name value
  409.  */
  410. if (!function_exists('wp_mail_smtp_mail_from_name')) :
  411. function wp_mail_smtp_mail_from_name ($orig) {
  412.        
  413.         // Only filter if the from name is the default
  414.         if ($orig == 'WordPress') {
  415.                 if (defined('WPMS_ON') && WPMS_ON)
  416.                         return WPMS_MAIL_FROM_NAME;
  417.                 elseif ( get_option('mail_from_name') != "" && is_string(get_option('mail_from_name')) )
  418.                         return get_option('mail_from_name');
  419.         }
  420.        
  421.         // If in doubt, return the original value
  422.         return $orig;
  423.        
  424. } // End of wp_mail_smtp_mail_from_name() function definition
  425. endif;
  426.  
  427. function wp_mail_plugin_action_links( $links, $file ) {
  428.         if ( $file != plugin_basename( __FILE__ ))
  429.                 return $links;
  430.  
  431.         $settings_link = '<a href="options-general.php?page=wp-mail-smtp/wp_mail_smtp.php">' . __( 'Settings', 'wp_mail_smtp' ) . '</a>';
  432.  
  433.         array_unshift( $links, $settings_link );
  434.  
  435.         return $links;
  436. }
  437.  
  438. // Add an action on phpmailer_init
  439. add_action('phpmailer_init','phpmailer_init_smtp');
  440.  
  441. if (!defined('WPMS_ON') || !WPMS_ON) {
  442.         // Whitelist our options
  443.         add_filter('whitelist_options', 'wp_mail_smtp_whitelist_options');
  444.         // Add the create pages options
  445.         add_action('admin_menu','wp_mail_smtp_menus');
  446.         // Add an activation hook for this plugin
  447.         register_activation_hook(__FILE__,'wp_mail_smtp_activate');
  448.         // Adds "Settings" link to the plugin action page
  449.         add_filter( 'plugin_action_links', 'wp_mail_plugin_action_links',10,2);
  450. }
  451.  
  452. // Add filters to replace the mail from name and emailaddress
  453. add_filter('wp_mail_from','wp_mail_smtp_mail_from');
  454. add_filter('wp_mail_from_name','wp_mail_smtp_mail_from_name');
  455.  
  456. load_plugin_textdomain('wp_mail_smtp', false, dirname(plugin_basename(__FILE__)) . '/langs');
  457.  
  458. ?>
  459.