WordPress Send Email: Tutorial with Code Snippets [2025]

Sending emails from a WordPress website is a vital functionality for many site owners and developers. Whether it’s for contact forms, user notifications, password resets, or order confirmations, reliable email functionality is essential. Fortunately, WordPress offers a straightforward way to send emails via PHP and built-in functions. In this tutorial, we’ll explore how to send emails in WordPress using various methods with practical code snippets that you can implement right away.

The Basics: Why Email Sending Matters in WordPress

Emails are used in WordPress for a variety of features:

  • User registration and password reset emails
  • WooCommerce order confirmations and invoice emails
  • Contact form submissions and user inquiries
  • Newsletters and marketing automation

By default, WordPress uses the wp_mail() function to send emails. It’s a simple wrapper around PHP’s native mail() function, giving you access to additional filters and it works well for most purposes.

Using wp_mail() – The Native Approach

The wp_mail() function is the standard method used by WordPress to send emails. Here is a basic usage example:

wp_mail( $to, $subject, $message, $headers = '', $attachments = array() );

Example: Sending a Simple Email


$to = 'user@example.com';
$subject = 'Welcome!';
$message = 'Thank you for registering on our site.';
$headers = 'From: MySite <no-reply@mysite.com>';

wp_mail($to, $subject, $message, $headers);

This will send a plain text email to the given recipient. Want HTML formatting? Just tweak the headers!

HTML Email Example


$headers = array('Content-Type: text/html; charset=UTF-8');
$message = '<h1>Welcome!</h1><p>Thanks for signing up.</p>';

wp_mail('user@example.com', 'HTML Email', $message, $headers);

This approach works, but email delivery isn’t always reliable due to PHP’s mail() function limitations. That’s why using SMTP is highly recommended for actual deployment.

Improving Email Deliverability Using SMTP

To improve your email deliverability—meaning ensuring those emails don’t end up in spam folders—it’s essential to use an SMTP plugin or manually configure it using code.

Using a Plugin

Some popular SMTP plugins are:

  • WP Mail SMTP by WPForms
  • Post SMTP Mailer
  • Easy WP SMTP

These plugins let you configure your site to send emails via trusted email servers like Gmail, Outlook, SendGrid, or even your hosting provider’s SMTP server.

To use WP Mail SMTP, follow these steps:

  1. Install and activate the plugin
  2. Navigate to WP Mail SMTP > Settings
  3. Enter your SMTP details (host, port, username, and password)
  4. Choose a mailer (Gmail, SendGrid, Mailgun, etc.)
  5. Send a test email to confirm that everything is working

Manual SMTP Configuration with PHPMailer

If you prefer not to use a plugin, you can use the phpmailer_init action hook to configure WordPress to use SMTP as well:


add_action('phpmailer_init', 'configure_phpmailer');

function configure_phpmailer($phpmailer){
    $phpmailer->isSMTP();
    $phpmailer->Host       = 'smtp.gmail.com';
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Port       = 587;
    $phpmailer->Username   = 'youremail@gmail.com';
    $phpmailer->Password   = 'yourpassword';
    $phpmailer->SMTPSecure = 'tls';
    $phpmailer->From       = 'youremail@gmail.com';
    $phpmailer->FromName   = 'Your Site Name';
}

Note: Never hardcode credentials. Use environment variables or the wp-config.php file instead.

Sending Email from a Custom Form

If you’ve built a custom contact form and you want it to send an email upon submission, here’s a basic example you can use:


// Check for form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cf_email'])) {
    
    $name    = sanitize_text_field($_POST['cf_name']);
    $email   = sanitize_email($_POST['cf_email']);
    $message = sanitize_textarea_field($_POST['cf_message']);
    
    $to = 'admin@example.com';
    $subject = 'New Contact Form Submission';
    $body = "Name: $name\nEmail: $email\nMessage: $message";
    $headers = array('Content-Type: text/plain; charset=UTF-8');
    
    wp_mail($to, $subject, $body, $headers);
}

You would pair this with a simple HTML form like this:


<form method="POST">
  <p><label>Name:</label> <input type="text" name="cf_name" required></p>
  <p><label>Email:</label> <input type="email" name="cf_email" required></p>
  <p><label>Message:</label> <textarea name="cf_message" required></textarea></p>
  <p><button type="submit">Send Message</button></p>
</form>

Testing and Debugging WordPress Emails

Once you’ve set things up, you should always test your email functionality. You can use plugins like:

  • WP Mail Logging – Logs every email sent from your WordPress site
  • Check & Email Log – Helps you debug and track emails

Also, use services like Mail-tester.com to measure your email’s spam score and get suggestions for fixing deliverability issues.

Sending Emails to Multiple Recipients

If you want to email more than one person, simply pass an array to the $to parameter or provide a comma-separated list:


$recipients = array('john@example.com', 'jane@example.com');

wp_mail($recipients, 'Group Update', 'Hello team, here is the latest update.', $headers);

Or using CC and BCC headers:


$headers = array(
    'Content-Type: text/plain; charset=UTF-8',
    'Cc: manager@example.com',
    'Bcc: director@example.com'
);

wp_mail('staff@example.com', 'Meeting Reminder', 'Don’t forget our meeting at 10AM.', $headers);

Security Tips and Best Practices

  • Sanitize user input: Always validate and sanitize anything provided by users before sending it in emails.
  • Avoid spam words: Words like “FREE,” “BUY NOW,” or “CLICK HERE” can trigger spam filters.
  • Use strong SMTP credentials: And avoid hardcoding them directly in themes or plugins.
  • Use SPF, DKIM, and DMARC DNS records: These authenticate your domain and improve deliverability.

Summary: Choose the Best Method for Your Needs

Sending emails in WordPress is straightforward when using wp_mail(), especially for simple tasks. For more robust and secure solutions, SMTP is your best friend.

Whether you opt for a plugin or hook into phpmailer_init directly, you’ll gain better control, more reliability, and enhanced email tracking capabilities.

Key takeaways:</

Leave a Reply

Your email address will not be published. Required fields are marked *