Setting Custom Email From name and email address in wp_email()

I am writing a custom plugin that sends an email to users from admin/front end section. I am using wp_email() to send emails and emails are being sent fine. I am testing this on a plain WP installation with only my plugin installed and hostgator as my hosting server. Whenever the email is sent, the email is sent from xyz@gator39.hostgator.com and not from the email address of WP admin account. I have also tried setting custom hooks as mentioned here and setting custom headers, but none of them is working. I am not sure what mistake I am doing. Can you please guide me on solving this issue. Code provided below

EDIT

Read More

I have also tried testing it from another dedicated windows server and getting same error.

// new name
function smartsms_mail_from_name() {
    return "WebMaster";
    //$name =  get_option('blogname');
    //$name = esc_attr($name);
    //return $name;
}

// new email-adress
function smartsms_mail_from() {
    return "webmaster@example.com";
    //$email = get_option('admin_email');
    //$email = is_email($email);
    //return $email;
}
add_filter( 'wp_mail_from', 'smartsms_mail_from' ); 
add_filter( 'wp_mail_from_name', 'smartsms_mail_from_name' );

//$headers = 'From: '. get_option('blogname') .' <' . get_option('admin_email') . '>';
            $headers = 'From: webmaster@example.com' . "rn" .'Reply-To: webmaster@example.com' . "rn" . 'X-Mailer: PHP/' . phpversion(); 
            $mail = wp_mail($to, $subject, $message, $headers);

After Brady’s answer below, edited the code as below..but no email was
sent. However, I get the message as “Message was sent successfully” 🙁

if($to!="")
        {
            //$headers = 'From: '. get_option('blogname') .' <' . get_option('admin_email') . '>';
            //$headers = 'From: webmaster@example.com' . "rn" .'Reply-To: webmaster@example.com' . "rn" . 'X-Mailer: PHP/' . phpversion(); 
            add_filter('wp_mail_from', 'smartsms_mail_from'); // add filter to modify the mail from
            add_filter('wp_mail_from_name', 'smartsms_mail_from_name'); // add filter to modify the mail from name
            add_filter('wp_mail_content_type', 'smartsms_wp_mail_content_type'); // add filter to modify the mail content type
            $mail = wp_mail($to, $subject, $message); // send mail
            remove_filter('wp_mail_from', 'smartsms_mail_from'); // remove applied filter
            remove_filter('wp_mail_from_name', 'smartsms_mail_from_name'); // remove applied filter
            remove_filter('wp_mail_content_type', 'smartsms_wp_mail_content_type'); // remove applied filter
            //$mail = wp_mail($to, $subject, $message, $headers);           
            if($mail)
            {
                echo 'Your message has been sent!';             
            }
            else echo 'There was a problem sending your message. Please try again.';
        }

// new name
function smartsms_mail_from_name() {
    return "WebMaster";
    //$name =  get_option('blogname');
    //$name = esc_attr($name);
    //return $name;
}

// new email-adress
function smartsms_mail_from() {
    return "webmaster@example.com";
    //$email = get_option('admin_email');
    //$email = is_email($email);
    //return $email;
}

function smartsms_wp_mail_content_type() { return "text/html"; }

Related posts

Leave a Reply

1 comment

  1. The correct way is to apply a filter to wp_mail_from and wp_mail_from_name. You would think that using the $headers would work and normally it would but there are many plugins that filter and then don’t take off their filter when they have sent their email which then leaves those details on the email for the next send. Below is a snippet of my plugin that uses these filters. Adjust code to your liking. Take note how I remove the filters once I’ve sent the mail via wp_mail():

        public function send_notify_email($alertMessage) {
        $options = get_option(self::$settings_option_field); // Get settings
        $subject = sprintf(__("WordPress File Monitor Plus: Alert (%s)", "wordpress-file-monitor-plus"), site_url()); // build subject
        $subject = apply_filters("sc_wpfmp_format_email_subject", $subject); // allow filter to alter subject
            add_filter('wp_mail_from', array(__CLASS__, 'sc_wpfmp_wp_mail_from')); // add filter to modify the mail from
            add_filter('wp_mail_from_name', array(__CLASS__, 'sc_wpfmp_wp_mail_from_name')); // add filter to modify the mail from name
            add_filter('wp_mail_content_type', array(__CLASS__, 'sc_wpfmp_wp_mail_content_type')); // add filter to modify the mail content type
            wp_mail($options['notify_address'], $subject, $alertMessage); // send mail
            remove_filter('wp_mail_from', array(__CLASS__, 'sc_wpfmp_wp_mail_from')); // remove applied filter
            remove_filter('wp_mail_from_name', array(__CLASS__, 'sc_wpfmp_wp_mail_from_name')); // remove applied filter
            remove_filter('wp_mail_content_type', array(__CLASS__, 'sc_wpfmp_wp_mail_content_type')); // remove applied filter
    }
    
    
    /**
     * Set from address for email notification
     *
     * @return void
     */
        public function sc_wpfmp_wp_mail_from() {
        $options = get_option(self::$settings_option_field); // Get settings
        return $options['from_address']; // Return the from address
    }
    
    
    /**
     * Set from name for email notification
     *
     * @return string $from_name
     */
        public function sc_wpfmp_wp_mail_from_name() {
        $from_name = __("WordPress File Monitor Plus", "wordpress-file-monitor-plus");
        $from_name = apply_filters("sc_wpfmp_format_email_from_name", $from_name); // allow filter to alter the from name
        return $from_name; // return from name
    }
    
    
    /**
     * Set content type for email notification
     *
     * @return string
     */
        public function sc_wpfmp_wp_mail_content_type() { return "text/html"; }