This article mainly introduces the usage of PHP functions related to sending email via WordPress. It also describes how to solve common failures in sending emails. For more information, see wp_mail () the function is used to send emails, similar to the mail () function in PHP.
The default sender name is WordPress, and the sender mailbox is similar to a wordpress@example.com.
Usage
wp_mail( $to, $subject, $message, $headers, $attachments );
Parameters
$
(String | array) (required) recipient. Multiple recipients can use arrays or strings separated by commas.
Default value: None
$ Subject
(String) (required) Mail title.
Default value: None
$ Message
(String) (required) the content of the email.
Default value: None
$ Headers
(String | array) (optional) mail Headers information. It can be an array or a string separated by carriage return (format: name: content ).
Default value: Null string
$ Attachments
(String | array) (optional) email attachment. Multiple attachments can use arrays or strings separated by carriage return.
Default value: Null string
Return value
(Boolean) whether the email is successfully sent.
Example
Basic email sending:
wp_mail( 'me@example.net', 'The subject', 'The message' );
Attachment and headers information:
$attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );$headers = 'From: My Name
' . "rn";wp_mail( 'test@example.org', 'subject', 'message', $headers, $attachments );
Html mail and multiple mailboxes sent at a time:
$multiple_to_recipients = array( 'recipient1@example.com', 'recipient2@foo.example.com')wp_mail( $multiple_to_recipients, 'The subject', 'The HTML message
', 'Content-Type: text/html' );
Others
This function is located in: wp-uplodes/pluggable. php
Solve the problem that emails cannot be sent
Failure to send emails is a common problem in WordPress. Generally, the server does not support the mail () function in PHP. There are two solutions.
PS: WordPress must use the wp_mail () function to send emails, rather than directly calling the php smtp class or other functions.
1. configure the server environment
If you are using VPS, you can configure the server to allow the server to support the mail () function. you can use the search engine to find the configuration method based on your situation.
II. use SMTP
The second method is to use SMTP to send emails, which is also a recommended method. SMTP is to use the servers of other free email service providers to send emails. Therefore, it is not easy to identify emails as spam.
We recommend that you use the wp smtp plug-in for WordPress support. after installation, configure your SMTP information in the background. I use QQ mail.
After configuration, you can test whether the configuration is successful.
Plug-in home: https://wordpress.org/plugins/wp-smtp/