How to Use wp_mail Function in WordPress?

In WordPress, the wp_mail() function is a powerful tool for sending emails from your website. It’s a pluggable function, meaning it can be replaced by plugins if needed. The function has a similar structure to PHP’s mail() function but with some WordPress-specific enhancements.

Here’s the basic syntax:

wp_mail( string|array $to, string $subject, string $message, string|array $headers = '', string|array $attachments = array() )
  1. $to: This is the recipient’s email address. You can also pass an array of emails if you want to send the email to multiple recipients.

  2. $subject: This is the subject line of the email.

  3. $message: This is the main content of the email.

  4. $headers (optional): You can specify additional headers like ‘From’, ‘Cc’, ‘Bcc’. This is optional.

  5. $attachments (optional): If you want to attach files, pass the file paths in an array. This is also optional.

Here’s an example:

$to = '';
$subject = 'Hello from WordPress';
$message = 'This is a test email.';
$headers = 'From: My Website <>' . "\r\n";

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

Remember to replace the email addresses, subject, and message with your own.

Leave a Comment