Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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' . "\r\n" .'Reply-To: webmaster@example.com' . "\r\n" . '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"; }
share|improve this question
    
Could you paste your code? –  Stephen Harris Feb 1 '12 at 16:05
    
@StephenHarris updated code –  Nitesh Feb 1 '12 at 16:29
add comment

1 Answer

up vote 3 down vote accepted

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"; }
share|improve this answer
    
I will check and let you know. Do you think there can be an issue with server? –  Nitesh Feb 1 '12 at 17:49
    
@Brady..did not work..please see my updated code above –  Nitesh Feb 2 '12 at 15:00
    
If you do this just before wp_mail(): var_dump(apply_filters("wp_mail_from", "test")); This will show you how your filters are changing the content. –  Brady Feb 2 '12 at 15:37
    
doing a var_dump shows the correct email address, but even then the email is not sent!! output of var_dump is string(22) "webmaster@example.com" –  Nitesh Feb 2 '12 at 21:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.