├── .gitignore ├── README.md ├── composer.json └── outlandish-smtp.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # outlandish-smtp 2 | Provides a number of different ways to set how WordPress sends emails 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "outlandish/outlandish-smtp", 3 | "type": "wordpress-muplugin", 4 | "description": "Provides a number of different ways to set how WordPress sends emails", 5 | "license": "MIT", 6 | "version": "1.1.2", 7 | "authors": [ 8 | { 9 | "name": "Nick Sellen", 10 | "email": "nick@outlandish.com" 11 | }, 12 | { 13 | "name": "Matt Kendon", 14 | "email": "matt@outlandish.com" 15 | } 16 | ], 17 | "keywords": [ 18 | "wordpress", 19 | "smtp", 20 | "wp", 21 | "plugin" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /outlandish-smtp.php: -------------------------------------------------------------------------------- 1 | isSMTP(); 27 | $phpmailer->Host = $host; 28 | $phpmailer->SMTPAuth = true; 29 | $phpmailer->Port = $port; 30 | $phpmailer->Username = $user; 31 | $phpmailer->Password = $pass; 32 | }); 33 | } elseif (getEnvValue('MAILTRAP_USER') && getEnvValue('MAILTRAP_PASS')) { 34 | // Use mailtrap 35 | 36 | $user = getEnvValue('MAILTRAP_USER'); 37 | $pass = getEnvValue('MAILTRAP_PASS'); 38 | 39 | add_action('phpmailer_init', function ($phpmailer) use ($user, $pass) { 40 | $phpmailer->isSMTP(); 41 | $phpmailer->Host = 'smtp.mailtrap.io'; 42 | $phpmailer->SMTPAuth = true; 43 | $phpmailer->Port = 2525; 44 | $phpmailer->Username = $user; 45 | $phpmailer->Password = $pass; 46 | }); 47 | } elseif (getEnvValue('SMTP_HOST')) { 48 | // Use SMTP_* env variables 49 | 50 | $host = getEnvValue('SMTP_HOST'); 51 | $port = getEnvValue('SMTP_PORT') ?: '25'; 52 | $secure = getEnvValue('SMTP_SECURE') ?: ''; 53 | $user = getEnvValue('SMTP_USER'); 54 | $pass = getEnvValue('SMTP_PASS'); 55 | 56 | add_action('phpmailer_init', function ($phpmailer) use ($user, $pass, $host, $port, $secure) { 57 | $phpmailer->isSMTP(); 58 | $phpmailer->Host = $host; 59 | $phpmailer->Port = $port; 60 | $phpmailer->SMTPSecure = $secure; 61 | if ($user && $pass) { 62 | $phpmailer->SMTPAuth = true; 63 | $phpmailer->Username = $user; 64 | $phpmailer->Password = $pass; 65 | } 66 | }); 67 | } 68 | 69 | /** 70 | * Filters out the MIME-Version header. It is already added by PHPMailer, and mails with 71 | * duplicate headers are rejected by AWS SES. 72 | **/ 73 | add_filter('wp_mail', function ($args) { 74 | $headers = $args['headers']; 75 | if (empty($headers) || is_array($headers)) { 76 | return $args; 77 | } 78 | $headers = preg_split('#\r?\n#', $headers); 79 | $headers = array_filter($headers, function ($header) { 80 | $header = trim($header); 81 | if (empty($header)) { 82 | return false; 83 | } 84 | $key = strtolower(explode(':', $header)[0]); 85 | // PHPMailer adds this header; duplicate headers are rejected by AWS SES 86 | if ($key === 'mime-version') { 87 | return false; 88 | } 89 | return true; 90 | }); 91 | $args['headers'] = $headers; 92 | return $args; 93 | }); 94 | 95 | // changes what the wp_mail from address is if the constant is set 96 | add_filter("wp_mail_from", function ($original_email_address) { 97 | $from_email = getEnvValue('FROM_EMAIL_ADDRESS'); 98 | return $from_email ?: $original_email_address; 99 | }); 100 | 101 | // changes what the wp_mail from name is if the constant is set 102 | add_filter("wp_mail_from_name", function ($original_from_name) { 103 | $from_name = getEnvValue('FROM_EMAIL_NAME'); 104 | return $from_name ?: $original_from_name; 105 | }); 106 | 107 | add_action('wp_mail_failed', function ($wp_error) { 108 | /** @var WP_Error $wp_error */ 109 | error_log('Mail Failed: ' . $wp_error->get_error_message()); 110 | }); 111 | --------------------------------------------------------------------------------