Home » Functions » wp_mail

Function Name: wp_mail

Explanation: The wp_mail function is a powerful WordPress function used for sending emails from your WordPress website. It provides a convenient way to send emails to users or administrators directly from your code.

This function is commonly used in WordPress plugins or themes to send various types of email notifications, such as user registration confirmations, password reset links, order confirmations, and more. It allows you to customize the email’s content, subject, headers, and attachments.

The wp_mail function supports both plain text and HTML emails, giving you the flexibility to create visually appealing emails with rich content. It also provides a reliable way to handle email delivery by utilizing the server’s built-in PHP mail function or by integrating with third-party email services like SMTP.

Example Usage:

Let’s say you have a custom user registration form on your WordPress website, and you want to send a welcome email to the new user upon successful registration. You can use the wp_mail function to achieve this.

function send_welcome_email($user_email) {
    $subject = 'Welcome to our website!';
    $message = 'Dear new user, welcome to our website. We are excited to have you on board!';
    $headers = array('Content-Type: text/html; charset=UTF-8');

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

In this example, we define a function called send_welcome_email. It takes the user’s email address as a parameter. Inside the function, we set the subject, message, and headers for the email. We then call the wp_mail function with the user’s email, subject, message, and headers as arguments.

This code will send a welcome email to the user’s email address, containing the subject, message, and headers specified. The email can be sent in HTML format due to the specified header setting.

Remember to replace $user_email with the actual email address of the user you want to send the email to.

In conclusion, the wp_mail function is a versatile tool for sending emails within WordPress, enabling you to create customized email notifications and enhance user engagement on your website.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.