Skip to content

How To Use wp_mail With WordPress

WordPress has its own email functionality, which calls wp_mail for sending emails. WordPress wp_mail function is allowing you the same functionality as PHP is giving to you so let’s try and use it.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?>
  • $to: (string or array) (required) The intended recipient(s). Multiple recipients may be specified using an array or a comma-separated string. Default: None
  • $subject: (string) (required) The subject of the message. Default: None
  • $message: (string) (required) Message content. Default: None
  • $headers: (string or array) (optional) Mail headers to send with the message. For the string version, each header line (beginning with From:, Cc:, etc.) is delimited with a newline (“\r\n”) (advanced) Default: Empty
  • $attachments: (string or array) (optional) Files to attach: a single filename, an array of filenames, or a newline-delimited string list of multiple filenames. (advanced) Default: Empty

Examples of wp_mail():

Sending an email with simple way:

<?php wp_mail( '', 'The subject', 'The message' ); ?>

Sending an email to multiple recipients:

    $multiple_to_recipients = array(
    $subject = 'The subject';
    $message = 'The message';
    $headers[] = 'From: webmaster <>';
    $headers[] = 'Cc: somebody <>';
    $headers[] = 'Cc:'; // note you can just use a simple email address
    wp_mail( $multiple_to_recipients, $subject, $message, $headers );

Sending an email with the attachment:

    $to  = '';
    $subject = 'The subject';
    $message = 'The message';
    $attachments = array( WP_CONTENT_DIR . '/uploads/2013/07/wallpaper-2407801-150x150.jpg' );
    $headers[] = 'From: webmaster <>';
    add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' );
    return wp_mail( $to, $subject, $message, $headers, $attachments );
    function my_custom_email_content_type() {
        return 'text/html';

Keep visiting for new stuff andĀ give your feedback.

Happy Coding šŸ˜‰

(Visited 6 times, 3 visits today)
%d bloggers like this: