phpmailer_init
Allows you to configure authenticated SMTP email delivery. Fires after PHPMailer{} is initialized.
wp_mail() uses PHPMailer{} to send email through PHP's mail(). This hook provides access to the PHPMailer{} object and allows its operating parameters to be changed.
Usage
add_action( 'phpmailer_init', 'wp_kama_phpmailer_init_action' );
/**
* Function for `phpmailer_init` action-hook.
*
* @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
*
* @return void
*/
function wp_kama_phpmailer_init_action( $phpmailer ){
// action...
}
- $phpmailer(PHPMailer)
- The
PHPMailer{}instance (passed by reference).
Examples
#1 Example Yandex SMTP server configuration
// SMTP configuration
add_action( 'phpmailer_init', 'smtp_phpmailer_init' );
function smtp_phpmailer_init( $phpmailer ){
$phpmailer->IsSMTP();
$phpmailer->CharSet = 'UTF-8';
$phpmailer->Host = 'smtp.yandex.ru';
$phpmailer->Username = '[email protected]';
$phpmailer->Password = '6AAAuuSSS2k';
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->Port = 465;
$phpmailer->From = '[email protected]';
$phpmailer->FromName = 'MY-Megasite';
$phpmailer->isHTML( true );
}
#2 General custom SMTP connection example
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.example.com';
$phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
$phpmailer->Port = 25;
$phpmailer->Username = 'yourusername';
$phpmailer->Password = 'yourpassword';
// Additional settings…
}
Error logging:
add_action('wp_mail_failed', 'log_mailer_errors', 10, 1);
function log_mailer_errors( $wp_error ){
// Output the error to a .log file
error_log( $wp_error->get_error_message() );
}
#3 Office 365 SMTP server configuration example
To set up SMTP on WordPress using the Office 365 server, you need to modify the default email-sending function and use the Microsoft SMTP server. This allows you to send emails through your corporate Office 365 account, which improves message deliverability and their reliability. In this guide, we’ll look at how to step-by-step configure email sending from WordPress via Office 365.
Step-by-step setup of SMTP for WordPress with Office 365:
1) Register an account in office365
To use this code, you must be registered in office365.
2) Using the hook phpmailer_init
You need to add special code to the functions.php file of your theme or in a must-use plugin. This will allow you to configure SMTP settings for WordPress.
WordPress uses PHPMailer to send emails, and we can change its settings using the phpmailer_init hook.
add_action( 'phpmailer_init', 'smtp_enable_for_office365' );
function smtp_enable_for_office365( $phpmailer ) {
$phpmailer->isSMTP(); // Indicate that this is SMTP
$phpmailer->SMTPAuth = true; // Indicate that this is SMTP with authentication
$phpmailer->Host = 'smtp.office365.com'; // URL, change if needed
$phpmailer->Port = '587'; // Port, change if needed
$phpmailer->Username = '[email protected]'; // Login in office365, specify your own
$phpmailer->Password = 'pass12345'; // Password in office365, specify your own
$phpmailer->SMTPSecure = 'tls'; // Indicate that this is SMTP in tls mode
$phpmailer->From = '[email protected]'; // From, specify your own email
$phpmailer->FromName = 'My super site'; // Site name, specify your own
}
-
Security settings: Be sure to use the
tlsprotocol to improve the security of data transmission. Authentication viaUsernameandPasswordwill help you make sure you really have access to the specified account. - Security tips: Never store the login and password in plain text in the code. Use environment variables, constants, or options (database) for this.
3) Checking functionality
After adding the code, check that emails are sent through the contact form or an SMTP testing plugin. Make sure the emails are delivered successfully.
Possible errors and their solutions:
-
Connection Error:
- Make sure the server supports outgoing connections on port 587.
- Check the correctness of the login and password for the SMTP server.
-
Deliverability issues:
- If emails end up in spam, add SPF and DKIM records at the DNS level.
- Incorrect encryption protocol:
- Make sure you use
tlsorssldepending on the settings of your Office 365 account.
- Make sure you use
Conclusion:
Configuring SMTP for WordPress via Office 365 is a simple and effective way to improve email deliverability and increase their reliability. By following this guide, you can easily integrate your WordPress site with the corporate Office 365 mail and ensure consistent email sending.
Changelog
| Since 2.2.0 | Introduced. |
Where the hook is called
do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
Where the hook is used in WordPress
add_action( 'phpmailer_init', 'fix_phpmailer_messageid' );