📜  wp_mail (1)

📅  最后修改于: 2023-12-03 14:48:34.558000             🧑  作者: Mango

Introduction to wp_mail

Overview

wp_mail is a powerful function in WordPress that allows you to send emails. It is a core function provided by WordPress itself, making it easy to incorporate email functionality into your plugin or theme.

Function Signature

The function signature for wp_mail is as follows:

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

Let's take a closer look at each parameter:

  1. $to (required): The recipient(s) of the email. This can be a single email address or an array of multiple addresses.
  2. $subject (required): The subject line of the email.
  3. $message (required): The content of the email.
  4. $headers (optional): Additional headers to include in the email. This can be a string or an array.
  5. $attachments (optional): File attachments to include with the email. This can be a string or an array.
Usage

To send an email using wp_mail, you can simply call the function and pass in the required parameters. Here's an example:

$to = 'john@example.com';
$subject = 'Hello from WordPress!';
$message = 'This is a test email sent using wp_mail.';
$headers = array(
    'From: Your Name <yourname@example.com>',
    'Reply-To: Your Name <yourname@example.com>',
    'Content-Type: text/html; charset=UTF-8'
);

// Send the email
wp_mail( $to, $subject, $message, $headers );
Markdown Code Formatting

To format code snippets in markdown, you can enclose the code within triple backticks (```). For example:

```php $to = 'john@example.com'; $subject = 'Hello from WordPress!'; $message = 'This is a test email sent using wp_mail.';

// Send the email wp_mail( $to, $subject, $message ); ```

This will render the code snippet in a nicely formatted manner, making it easier to read and understand.

Conclusion

wp_mail provides a convenient way to send emails within your WordPress projects. By understanding its function signature and proper usage, you can leverage this powerful function to incorporate email functionality seamlessly.