I just want to setup SMTP for a specific instance of wp_mail()
.
I have tested plugins like “Easy WP SMTP” and also checked how to set up SMTP manually but all these apply to the entire site and then every email from the site is sent through the SMTP account.
I don’t want to send any other emails like newsletters or comment approval emails through the same SMTP account.
1 Answer
The following is a way to handle your question. It is in two parts. First, you would create your connection as constants to be used later. The best way to do this is in wp-config.php. (You mentioned that you’re doing this in a custom plugin. If that’s something that is to be portable, then you may want to change this to saving setting in the db instead.) Second, you’ll apply a function hooked to the phpmailer that WP uses. In that function you can define your criteria in which you’d use the SMTP connection instead of the default.
You can set up your SMTP credentials and server connection info in wp-config.php as constants as follows:
/*
* 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
Once you’ve added that to your wp-config.php file, you have constants that you can use to connect and send any email via SMTP. You can do this by hooking to the phpmailer_init action and using that to set up connection criteria using the constants you defined above.
In your specific case, you would want to add some conditional logic in the function to identify the condition in which you want to send via SMTP. Set up your conditional so that only your criteria uses the SMTP connection for the phpmailer, and all others would use whatever is already being used.
Since we don’t know what that is from your OP, I’ve represented it here with a generic true === $some_criteria
:
add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
if ( true === $some_criteria ) {
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;
}
// any other case would not change the sending server
}
This concept is from the following gist on github:
https://gist.github.com/butlerblog/c5c5eae5ace5bdaefb5d
General instructions on it here:
http://b.utler.co/Y3