Sending email displays Wordpress as sender

2019-03-03 15:28发布

问题:

I set up an emailform in PHP and send it through WORDPRESS. I thought I had everything covered but apparently it keeps displaying WORDPRESS as the sender instead of the email I have configured.

I have added my code below, but I guess I'm missing the §from_name field when I send the email? I Googled for wp_mail() problems with the sender, but couldn't find/understand anything.

$from_name = "MyWebsite";
$from_email = "info@website.com";
$admin_email = "[receiver email]";
$admin_email_subject = "A new message has been sent from MyWebsite!";
$email_styles = "font-family:Verdana; font-size:11px;";
$admin_email_message = <<<ADMIN_EMAIL ...[CONTENT]...ADMIN_EMAIL;

$admin_email_message = str_replace($find_these, $replace_these, $admin_email_message);
$admin_email_message = '<div style="' . $email_styles . '">' . str_replace("\n","<br/>",$admin_email_message) . '</div>';

add_filter( 'wp_mail_content_type', 'set_html_content_type' );

wp_mail($admin_email,$admin_email_subject,$admin_email_message . $fields_string);

回答1:

Add a wp_mail_from_name filter as described in docs to set the sender name.

add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
function custom_wp_mail_from_name( $original_email_from ) {
    return 'MyWebSite';
}

Or if you want to automatically substitute a site name

add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
function custom_wp_mail_from_name( $original_email_from ) {
    return get_option('blogname');;
}


标签: php wordpress