smtp.php 4.4 KB

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