How to give from name in wp_mail
In WordPress, when using the wp_mail
function to send an email, you can specify the from
name and email address using the following parameters:
from_name
: The name of the sender.from_email
: The email address of the sender.
Here's an example:
wp_mail( $to_email, $subject, $message, $headers, array(
'From' => 'Your Name <[email protected]>',
'From_Name' => 'Your Name',
) );
In this example, the From
header is set to Your Name <[email protected]>
, and the From_Name
parameter is set to Your Name
. This will display the sender's name in the email client's "From" field.
Alternatively, you can use the wp_mail
function with the wp_mail_from
and wp_mail_from_name
filters to set the from
name and email address:
add_filter( 'wp_mail_from', 'your_function' );
add_filter( 'wp_mail_from_name', 'your_function' );
function your_function( $from_email ) {
return '[email protected]';
}
function your_function( $from_name ) {
return 'Your Name';
}
wp_mail( $to_email, $subject, $message, $headers );
In this example, the wp_mail_from
filter is used to set the from
email address, and the wp_mail_from_name
filter is used to set the from
name.