Send Email via SMTP Server in WordPress

22 May 2024 by
Rakesh Chavada

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->Host = '';
$phpmailer->SMTPAuth = true;
$phpmailer->Username = '';
$phpmailer->Password = '*****';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Port = 587;

$phpmailer->setFrom('', 'CodexWorld');

// Add a recipient

// Add cc or bcc 

// Set email format to HTML

// 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;

    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
    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

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.
  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.
  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.

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->Host = '';
$phpmailer->SMTPAuth = true;
$phpmailer->Username = '';
$phpmailer->Password = '********';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Port = 587;