Send Email via SMTP Server in WordPress

When sending email from the script, it always a good idea to send using SMTP server. Send email via SMTP server prevents many issues such as your email being marked as spam. Basically, the mail() or wp_mail() function is used to send email in WordPress. If you want to use SMTP server to send email in WordPress, PHPMailer Class will be the best choice.

There are many plugins are available in WordPress to sending the email via SMTP server. But you can easily send emails using SMTP server in WordPress without using any plugins. In this tutorial, we will show you how to send email using SMTP in WordPress. With SMTP configuration you can increase the email deliverability in your WordPress site.

WordPress PHPMailer Class

Before you get started, the PHPMailer class need to be initialized.

/*
 * Initialize phpmailer class
 */
global $phpmailer;

// (Re)create it, if it's gone missing
if ( ! ( $phpmailer instanceof PHPMailer ) ) {
    require_once 
ABSPATH WPINC '/class-phpmailer.php';
    require_once 
ABSPATH WPINC '/class-smtp.php';
}
$phpmailer = new PHPMailer;

Send HTML Email in WordPress via SMTP

The following example code sends HTML email via SMTP with PHPMailer class in WordPress.

// SMTP configuration
$phpmailer->isSMTP();                    
$phpmailer->Host 'smtp.example.com';
$phpmailer->SMTPAuth true;
$phpmailer->Username 'user@example.com';
$phpmailer->Password '*****';
$phpmailer->SMTPSecure 'tls';
$phpmailer->Port 587;

$phpmailer->setFrom('info@example.com''CodexWorld');

// Add a recipient
$phpmailer->addAddress('john@example.com');

// Add cc or bcc 
$phpmailer->addCC('cc@example.com');
$phpmailer->addBCC('bcc@example.com');

// Set email format to HTML
$phpmailer->isHTML(true);

// Email subject
$phpmailer->Subject 'Send Email via SMTP from WordPress';

// Email body content
$mailContent "<h1>Send HTML Email using SMTP in WordPress</h1>
    <p>This is a test email has sent using SMTP mail server with PHPMailer from WOrdPress.</p>"
;
$phpmailer->Body    $mailContent;

if(!
$phpmailer->send()){
    echo 
'Message could not be sent.';
    echo 
'Mailer Error: ' $phpmailer->ErrorInfo;
}else{
    echo 
'Message has been sent';
}

Send Email to Multiple Recipients

Add addAddress() method multiple times to send an email to multiple recipients.

// Add multiple recipient
$phpmailer->addAddress('john@example.com');
$phpmailer->addAddress('marry@example.com');
$phpmailer->addAddress('smith@example.com');

Send Email using Gmail SMTP in WordPress

If you wish to use your Gmail account to send email from WordPress, you need to make some changes in Google account settings. Configure your Google account by the following changes to use Gmail SMTP in WordPress.

  • Login to your Google account.
  • Go to the My Account page. Click the Signing in to Google link from Sign-in & security section.
    send-email-php-gmail-smtp-account-settings-codexworld
  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.
    send-email-php-gmail-smtp-off-2-step-verification-codexworld
  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.
    send-email-php-gmail-smtp-allow-less-secure-apps-codexworld

You are done! Now you can use your Gmail account as an SMTP server to send email from WordPress.

Specify your Gmail account credentials (email address and password), SMTP host and port.

// Gmail SMTP configuration
$phpmailer->isSMTP();
$phpmailer->Host 'smtp.gmail.com';
$phpmailer->SMTPAuth true;
$phpmailer->Username 'codexworld@gmail.com';
$phpmailer->Password '********';
$phpmailer->SMTPSecure 'tls';
$phpmailer->Port 587;

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up