-
-
Save progress44/67ead3520b268a536952639f4bb342d4 to your computer and use it in GitHub Desktop.
Configure WordPress wp_mail function to send through SMTP server http://b.utler.co/Y3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Set the following constants in wp-config.php | |
* These should be added somewhere BEFORE the | |
* constant ABSPATH is defined. | |
*/ | |
define( 'SMTP_USER', 'user@example.com' ); // Username to use for SMTP authentication | |
define( 'SMTP_PASS', 'smtp password' ); // Password to use for SMTP authentication | |
define( 'SMTP_HOST', 'smtp.example.com' ); // The hostname of the mail server | |
define( 'SMTP_FROM', 'website@example.com' ); // SMTP From email address | |
define( 'SMTP_NAME', 'e.g Website Name' ); // SMTP From name | |
define( 'SMTP_PORT', '25' ); // SMTP port number - likely to be 25, 465 or 587 | |
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls | |
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false) | |
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function will connect wp_mail to your authenticated | |
* SMTP server. This improves reliability of wp_mail, and | |
* avoids many potential problems. | |
* | |
* Values are constants set in wp-config.php. Be sure to | |
* define the using the wp_config.php example in this gist. | |
* | |
* Author: Chad Butler | |
* Author URI: http://butlerblog.com | |
* | |
* For more information and instructions, see: | |
* http://b.utler.co/Y3 | |
*/ | |
add_filter('wp_mail_from', 'set_wp_mail_from'); | |
function set_wp_mail_from($current_from) { | |
if (SMTP_FROM) { | |
return SMTP_FROM; | |
} | |
if ($current_from) { | |
return $current_from; | |
} | |
return get_bloginfo('admin_email'); | |
} | |
add_filter('wp_mail_from_name', 'set_wp_mail_from'); | |
function set_wp_mail_from_name($current_from_name) { | |
if (SMTP_NAME) { | |
return SMTP_NAME; | |
} | |
if ($current_from_name) { | |
return $current_from_name; | |
} | |
return get_bloginfo('name'); | |
} | |
add_action( 'phpmailer_init', 'send_smtp_email' ); | |
function send_smtp_email( $phpmailer ) { | |
if ( ! is_object( $phpmailer ) ) { | |
$phpmailer = (object) $phpmailer; | |
} | |
$phpmailer->Mailer = 'smtp'; | |
$phpmailer->Host = SMTP_HOST; | |
$phpmailer->SMTPAuth = SMTP_AUTH; | |
$phpmailer->Port = SMTP_PORT; | |
$phpmailer->Username = SMTP_USER; | |
$phpmailer->Password = SMTP_PASS; | |
$phpmailer->SMTPSecure = SMTP_SECURE; | |
$phpmailer->From = SMTP_FROM; | |
$phpmailer->FromName = SMTP_NAME; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment