Wordpress mail
WordPress has a built-in function to send emails using the wp_mail()
function. Here's an example of how to use it:
wp_mail( '[email protected]', 'Subject', 'This is the email body' );
In this example, wp_mail()
is used to send an email to [email protected]
with the subject "Subject" and the body "This is the email body".
You can also add additional parameters to wp_mail()
to customize the email, such as:
headers
: an array of additional headers to add to the emailattachments
: an array of file paths to attach to the emailcontent_type
: the content type of the email (e.g. "text/plain" or "text/html")
Here's an example of how to use these additional parameters:
wp_mail( '[email protected]', 'Subject', 'This is the email body', array(
'From' => '[email protected]',
'Reply-To' => '[email protected]'
), array('file1.txt', 'file2.txt'), 'text/plain' );
In this example, the email is sent from [email protected]
and the recipient can reply to the email by sending it back to [email protected]
. The email also includes two attachments, file1.txt
and file2.txt
, and the content type is set to "text/plain".
You can also use the wp_mail()
function to send emails with HTML content by setting the content_type
parameter to "text/html". Here's an example:
wp_mail( '[email protected]', 'Subject', '<html>This is the email body</html>', array(
'From' => '[email protected]',
'Reply-To' => '[email protected]'
), array('file1.txt', 'file2.txt'), 'text/html' );
In this example, the email is sent with HTML content and the content type is set to "text/html".
It's also worth noting that you can use the wp_mail()
function to send emails with attachments by using the wp_mail_attachment()
function. Here's an example:
wp_mail_attachment( '[email protected]', 'Subject', 'This is the email body', 'file.txt' );
In this example, the email is sent with the attachment file.txt
.
You can also use the wp_mail()
function to send emails with multiple attachments by using the wp_mail_attachment()
function multiple times. Here's an example:
wp_mail_attachment( '[email protected]', 'Subject', 'This is the email body', 'file1.txt' );
wp_mail_attachment( '[email protected]', 'Subject', 'This is the email body', 'file2.txt' );
In this example, the email is sent with two attachments, file1.txt
and file2.txt
.
It's also worth noting that you can use the wp_mail()
function to send emails with a custom from name and email address by using the wp_mail_from_name
and wp_mail_from_email
filters. Here's an example:
add_filter( 'wp_mail_from_name', 'custom_from_name' );
add_filter( 'wp_mail_from_email', 'custom_from_email' );
function custom_from_name( $from_name ) {
return 'Custom From Name';
}
function custom_from_email( $from_email ) {
return '[email protected]';
}
In this example, the wp_mail_from_name
and wp_mail_from_email
filters are used to set the from name and email address to "Custom From Name" and "[email protected]" respectively.
It's also worth noting that you can use the wp_mail()
function to send emails with a custom subject by using the wp_mail_subject
filter. Here's an example:
add_filter( 'wp_mail_subject', 'custom_subject' );
function custom_subject( $subject ) {
return 'Custom Subject';
}
In this example, the wp_mail_subject
filter is used to set the subject of the email to "Custom Subject".