smtp.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. class dw_smtp {
  3. protected static $instance;
  4. protected $validations;
  5. function __construct() {
  6. $this->prepare_settings();
  7. $check = $this->validate();
  8. if( is_wp_error( $check ) ) {
  9. trigger_error($check->get_error_message(), E_USER_WARNING);
  10. } else {
  11. add_action( 'phpmailer_init', array( $this, 'mailer') );
  12. }
  13. unset($this->validations);
  14. }
  15. public static function launch() {
  16. self::$instance = (defined('GLOBAL_SMTP_DISABLE') && GLOBAL_SMTP_DISABLE ) ? null : new self();
  17. }
  18. protected function prepare_settings() {
  19. $this->validations = new stdClass;
  20. $this->validations->required = array('GLOBAL_SMTP_HOST','GLOBAL_SMTP_USER','GLOBAL_SMTP_PASSWORD');
  21. $this->validations->is_email = array('GLOBAL_SMTP_RETURN_PATH','GLOBAL_SMTP_FROM','GLOBAL_SMTP_REPLYTO_FROM');
  22. $this->validations->not_empty = array('GLOBAL_SMTP_FROM','GLOBAL_SMTP_FROM_NAME');
  23. $this->validations->is_int = array('GLOBAL_SMTP_PORT','GLOBAL_SMTP_TIMEOUT');
  24. $this->validations->should_be = array('GLOBAL_SMTP_SECURE' => array('ssl','tls','none'),
  25. 'GLOBAL_SMTP_AUTH_TYPE' => array('LOGIN','PLAIN','NTLM') );
  26. $assume = array(
  27. 'GLOBAL_SMTP_PORT'=> 465,
  28. 'GLOBAL_SMTP_SECURE' => 'ssl',
  29. 'GLOBAL_SMTP_TIMEOUT' => 10,
  30. 'GLOBAL_SMTP_FROM' => get_site_option('admin_email','',true),
  31. 'GLOBAL_SMTP_FROM_NAME' => get_site_option('site_name','WordPress',true),
  32. 'GLOBAL_SMTP_AUTH_TYPE' => 'LOGIN',
  33. );
  34. foreach ($assume as $setting => $default) {
  35. if(!defined($setting)) {
  36. define($setting, $default);
  37. }
  38. }
  39. }
  40. protected function validate() {
  41. foreach ($this->validations->required as $setting) {
  42. if(!defined($setting)) {
  43. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is required for Multisite SMTP. Please define this in wp-config.php.', 'multisite-smtp' ), $setting ) );
  44. }
  45. }
  46. foreach ($this->validations->is_email as $setting) {
  47. if (defined($setting) && !is_email(constant($setting))) {
  48. return new WP_Error( 'multisite-smtp', sprintf( __( 'Value of %s is not a valid email address. Check wp-config.php, or ensure a valid fallback is available.', 'multisite-smtp' ), $setting ) );
  49. }
  50. }
  51. foreach ($this->validations->not_empty as $setting) {
  52. if(defined($setting) && constant($setting)=="") {
  53. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is empty. Check wp-config.php, or ensure a valid fallback is available.', 'multisite-smtp' ), $setting ) );
  54. }
  55. }
  56. foreach ($this->validations->is_int as $setting) {
  57. if(defined($setting) && !is_int(constant($setting)) ) {
  58. return new WP_Error( 'multisite-smtp', sprintf( __( '%s should be an integer. The force is not so strong with this one...', 'multisite-smtp' ), $setting ) );
  59. }
  60. }
  61. foreach ($this->validations->should_be as $setting => $allowed) {
  62. if(defined($setting) && !in_array(constant($setting), $allowed)) {
  63. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is invalid. It should be one of these values: "%s"', 'multisite-smtp' ), $setting, implode('" , "',$allowed) ) );
  64. }
  65. }
  66. return true;
  67. }
  68. public function mailer( $phpmailer ) {
  69. if(defined('GLOBAL_SMTP_DEBUG') && GLOBAL_SMTP_DEBUG ) $phpmailer->SMTPDebug = true;
  70. $phpmailer->Mailer = "smtp";
  71. $phpmailer->SMTPAuth = true;
  72. $phpmailer->Host = GLOBAL_SMTP_HOST;
  73. $phpmailer->Username = GLOBAL_SMTP_USER;
  74. $phpmailer->Password = GLOBAL_SMTP_PASSWORD;
  75. $phpmailer->From = GLOBAL_SMTP_FROM;
  76. $phpmailer->FromName = GLOBAL_SMTP_FROM_NAME;
  77. $phpmailer->Port = GLOBAL_SMTP_PORT;
  78. $phpmailer->SMTPSecure = GLOBAL_SMTP_SECURE;
  79. $phpmailer->AuthType = GLOBAL_SMTP_AUTH_TYPE;
  80. $phpmailer->Sender = defined('GLOBAL_SMTP_RETURN_PATH') ? GLOBAL_SMTP_RETURN_PATH : GLOBAL_SMTP_FROM;
  81. if(defined('GLOBAL_SMTP_REPLYTO_FROM')) {
  82. $phpmailer->AddReplyTo(GLOBAL_SMTP_REPLYTO_FROM, defined('GLOBAL_SMTP_REPLYTO_FROM_NAME') ? GLOBAL_SMTP_REPLYTO_FROM_NAME : GLOBAL_SMTP_FROM_NAME);
  83. }
  84. }
  85. }
  86. dw_smtp::launch();