arrow_back
Back

WordPress emails: wp_mail, sender headers, and HTML messages

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

WordPress ships with its own mail function, open_in_new wp_mail() , which wraps PHPMailer and respects whatever SMTP configuration plugins have set up:

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

Changing the sender name

add_filter( 'wp_mail_from_name', 'vortal_wp_mail_from_name' );

function vortal_wp_mail_from_name( $email_from ) {
    return 'XXX';
}

Changing the sender address

add_filter( 'wp_mail_from', 'vortal_wp_mail_from' );

function vortal_wp_mail_from( $email_address ) {
    return 'xxx@yyy.com';
}

By default WordPress sends from wordpress@your-domain, which is often rejected by spam filters, so setting a real mailbox here is worth doing on every project.

Sending HTML

Messages are sent as plain text unless you declare the content type in the $headers parameter:

$to      = 'sendto@example.com';
$subject = 'The subject';
$body    = 'The email body content';
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];

wp_mail( $to, $subject, $body, $headers );
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

WordPress REST API: routes, endpoints, and request parameters

arrow_forward