smtp.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class dwp_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'), 'GLOBAL_SMTP_AUTH_TYPE' => array('LOGIN','PLAIN','NTLM') );
  25. $assume = array(
  26. 'GLOBAL_SMTP_PORT'=> 465,
  27. 'GLOBAL_SMTP_SECURE' => 'ssl',
  28. 'GLOBAL_SMTP_TIMEOUT' => 10,
  29. 'GLOBAL_SMTP_FROM' => get_site_option('admin_email','',true),
  30. 'GLOBAL_SMTP_FROM_NAME' => get_site_option('site_name','WordPress',true),
  31. 'GLOBAL_SMTP_AUTH_TYPE' => 'LOGIN',
  32. );
  33. foreach ($assume as $setting => $default) {
  34. if(!defined($setting)) {
  35. define($setting, $default);
  36. }
  37. }
  38. }
  39. protected function validate() {
  40. foreach ($this->validations->required as $setting) {
  41. if(!defined($setting)) {
  42. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is required. Please define this in wp-config.php.', 'multisite-smtp' ), $setting ) );
  43. }
  44. }
  45. foreach ($this->validations->is_email as $setting) {
  46. if (defined($setting) && !is_email(constant($setting))) {
  47. 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 ) );
  48. }
  49. }
  50. foreach ($this->validations->not_empty as $setting) {
  51. if(defined($setting) && constant($setting)=="") {
  52. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is empty. Check wp-config.php, or ensure a valid fallback is available.', 'multisite-smtp' ), $setting ) );
  53. }
  54. }
  55. foreach ($this->validations->is_int as $setting) {
  56. if(defined($setting) && !is_int(constant($setting)) ) {
  57. return new WP_Error( 'multisite-smtp', sprintf( __( '%s should be an integer.', 'multisite-smtp' ), $setting ) );
  58. }
  59. }
  60. foreach ($this->validations->should_be as $setting => $allowed) {
  61. if(defined($setting) && !in_array(constant($setting), $allowed)) {
  62. return new WP_Error( 'multisite-smtp', sprintf( __( '%s is invalid. It should be one of these values: "%s"', 'multisite-smtp' ), $setting, implode('" , "',$allowed) ) );
  63. }
  64. }
  65. return true;
  66. }
  67. public function mailer( $phpmailer ) {
  68. if(defined('GLOBAL_SMTP_DEBUG') && GLOBAL_SMTP_DEBUG ) $phpmailer->SMTPDebug = true;
  69. $phpmailer->Mailer = "smtp";
  70. $phpmailer->SMTPAuth = true;
  71. $phpmailer->Host = GLOBAL_SMTP_HOST;
  72. $phpmailer->Username = GLOBAL_SMTP_USER;
  73. $phpmailer->Password = GLOBAL_SMTP_PASSWORD;
  74. $phpmailer->From = GLOBAL_SMTP_FROM;
  75. $phpmailer->FromName = GLOBAL_SMTP_FROM_NAME;
  76. $phpmailer->Port = GLOBAL_SMTP_PORT;
  77. $phpmailer->SMTPSecure = GLOBAL_SMTP_SECURE;
  78. $phpmailer->AuthType = GLOBAL_SMTP_AUTH_TYPE;
  79. $phpmailer->Sender = defined('GLOBAL_SMTP_RETURN_PATH') ? GLOBAL_SMTP_RETURN_PATH : GLOBAL_SMTP_FROM;
  80. if(defined('GLOBAL_SMTP_REPLYTO_FROM')) {
  81. $phpmailer->AddReplyTo(GLOBAL_SMTP_REPLYTO_FROM, defined('GLOBAL_SMTP_REPLYTO_FROM_NAME') ? GLOBAL_SMTP_REPLYTO_FROM_NAME : GLOBAL_SMTP_FROM_NAME);
  82. }
  83. }
  84. }
  85. dwp_smtp::launch();