In this WordPress tutorial, we will learn how to use the wp_mail() function to send emails in WordPress. Note that this function will work only if email sending functionality, like SMTP (Simple Mail Transfer Protocol), is enabled on your website. For more information on configuring SMTP, refer to our post on how to set up SMTP in WordPress.

How the wp_mail() Function Works

  • The wp_mail() function uses the PHPMailer library to send emails.
  • The PHPMailer class simplifies the process by leveraging PHP’s built-in mail() function.
  • This built-in mail() function then instructs the mail server on the WordPress host to send the email.

The wp_mail() function makes the task of sending emails much simpler and more efficient compared to writing complex code for PHP’s mail() function. This is why it is recommended to use wp_mail() for sending emails in WordPress.

How to Send Emails from WordPress Without a Plugin

If you prefer not to use third-party plugins for simple email functionality, you can create a custom mail-sending plugin. Follow our tutorial on creating a settings page for plugins and themes to build a mail-sending form in WordPress.

Once SMTP is properly configured and the settings form for sending emails is created, you can call the wp_mail() function when the user clicks the “Send” button. The following code checks whether the send button is clicked, gathers the information entered by the user, and uses the wp_mail() function to send the email.

if(isset($_POST["send"])) {
    $receiver = $_POST['receiver-email'];
    $email_subject = $_POST['email-subject'];
    $email_message = $_POST['message'];
    $email = $_POST['sender-email'];
    $cc = $_POST['cc-email'];
    $bcc = $_POST['bcc-email'];
    wp_mail(
        $receiver,
        $email_subject,
        $email_message,
        array(
            "From: {$email}",
            "Cc: {$cc}",
            "Bcc: {$bcc}"
        )
    );
}

wp_mail() Function Parameters

wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = '', string|string[] $attachments = array() ): bool
  • $to string|string[] required: Array or comma-separated list of email
  • $subject string required: The subject of the email.
  • $message string required: The body/content of the email.
  • $headers string|string[] optional: Additional headers like “From,” “Cc,” “Bcc,” etc.
  • $attachments string|string[] optional: Paths to files that will be attached to the email.

A return value of true does not necessarily mean the email was received successfully by the recipient. It only indicates that the request was processed without errors.

Examples

Send an Email to Multiple Recipients with CC and BCC

wp_mail(
    ['target1@example.com', 'target2@example.com'],
    'Sending email to multiple CC and BCC',
    'This is an example of sending mail to multiple CC and BCC recipients.',
    array(
        "From: I'm Admin <admin@yourdomain.com>",
        "Cc: cc1@example.com",
        "Cc: cc2@example.com",
        "Bcc: bcc1@example.com",
        "Bcc: bcc2@example.com",
    )
);

Send Email with Attachments

wp_mail(
    'target@example.com',
    'How to send an email with attachments using wp_mail',
    'This is an example of sending attachments with an email.',
    '',
    array(
        WP_CONTENT_DIR . '/uploads/file1.zip',
        WP_CONTENT_DIR . '/uploads/file2.pdf',
    )
);

Send HTML-formatted Email

function set_html_mail_content_type() {
    return 'text/html';
}
add_filter( 'wp_mail_content_type', 'set_html_mail_content_type' );

$html_code = '<h1>HTML Email Demo</h1><p>Hi, this is a test email.</p>';

$sent = wp_mail( 'target@example.com', 'How to send an HTML email', $html_code );

remove_filter( 'wp_mail_content_type', 'set_html_mail_content_type' );

How to Send Emails in WordPress Using the SMTP Plugin

To send emails from WordPress using a plugin, you can follow these steps. We’ll use a popular plugin called Easy WP SMTP, which helps ensure reliable email delivery by configuring SMTP (Simple Mail Transfer Protocol) settings.

Steps to Send Emails from WordPress Using Easy WP SMTP Plugin

  1. Install the Easy WP SMTP Plugin
    • From your WordPress dashboard, go to Plugins > Add New Plugin.
    • In the search bar, type Easy WP SMTP.
    • Click Install Now and then Activate to enable the plugin.
  2. Configure SMTP Settings
    • After activation, go to Easy WP SMTP in your WordPress dashboard.
    • Click on Settings.
    • Enter the details for your SMTP provider (e.g., Gmail, SendGrid, Mailgun, etc.).
    • If your SMTP provider is not listed, select the “Other” option.
    • After filling in these details, click Save Settings.
  3. Test the Email Configuration
    • In the Easy WP SMTP settings, click Send a Test Email.
    • Turn on the Custom Email feature.
    • Enter a recipient email address, subject, and message, then click Send Test Email to check if the configuration works.
    • Verify in the recipient’s inbox that the email was successfully delivered.
  4. Explore Additional Features
    • Easy WP SMTP offers other useful features, such as logging sent emails, setting custom headers, and configuring advanced settings for greater control over your email delivery.

Frequently Asked Questions

What is the “Reply-To” Header in an Email?

The “Reply-To” header allows you to specify a different email address to receive replies, rather than the one in the “From” address. For example, if you are sending an email from sender@bcompany.com to user@gmail.com but want replies to go to admin@acompany.com, you would use the “Reply-To” header.

$headers = array(
    'From: sender@bcompany.com',
    'Reply-To: Admin <admin@acompany.com>',
);

wp_mail(
    'user@gmail.com',
    'What is the Reply-To header used for?',
    'This message is an example of the Reply-To header in an email.',
    $headers
);

What is the full form of SMTP?

The full form of SMTP is Simple Mail Transfer Protocol. It is the protocol used for reliably sending emails between servers.