Send email through SMTP without plugin in WordPress

This code allows you to send emails through SMTP without using a plugin in WordPress. The code is extremely minimalistic and will not add any overhead to the loading of WordPress. Use this instead of bloated plugins with ads and slow functionality.

/**
 * Send mail through SMTP without plugin
 */

function trsdm_phpmailer($phpmailer)
{
    $phpmailer->isSMTP(); //Activate SMTP-mode
    $phpmailer->Host = 'mail.example.com'; //SMTP-server provided by your host
    $phpmailer->SMTPAuth = true; //Activate for using username and password
    $phpmailer->Port = 465; //Your port provided by your host (SSL-port recommended)
    $phpmailer->SMTPSecure = 'ssl'; //Security protocol provided by your host
    $phpmailer->Username = 'username@example.com'; //Your username
    $phpmailer->Password = 'PASSWORD'; //Your password
}
add_action('phpmailer_init', 'trsdm_phpmailer'); //Hooks into initialization of PHPMailer
Code language: PHP (php)

Code explained:

WordPress uses a version of PHPMailer to send email. We can hook into the initialization of PHPMailer to change the setup. This allows us to send emails through SMTP. WordPress will now use SMTP for sending emails through the wp_mail() function, including any and all plugins that use this function to send emails.

Where to put it:

The code goes in the functions.php file of your active (child) theme, or in a plugin.

Get these knowledge bombs before they are released

Plus exclusive content only for newsletter family members! No spam or yucky sales tactics. The bombs only drop occasionally for a clutter-free inbox.