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 wish to use the wp_mail_from hook to change the from email address submitted through a form which sends an invitation to a friend by email (I am writing my own plugin to do this).

I am using the following code to at present but I cannot see what I am doing wrong as the from email address is not set at all and goes to the default:

function() {
    $this->from_email = $_POST['your_email'];

    add_filter( 'wp_mail_from', array( $this, 'set_from_email' ) );

    wp_mail( $to, $subject, $message);
}

function set_from_email() {
    return $this->from_email;
}

As far as I can tell this should work flawlessly and out of the box but I have tried for hours at no avail hence, my question.

FYI I am using this function inside a class but I do not think that should change anything.

Many thanks, nav

share|improve this question
    
Do you really just pass $from_mail through … and expect a visible change? –  toscho Mar 12 '12 at 14:17
    
@toscho thanks for your reply your question above made me find out that I was thinking that add_action() and add_filter() worked the same way. I have modified my code above. As far as I know the above should work but I am still only getting the old from address when email is sent. –  navanitachora Mar 12 '12 at 15:43
    
Is $_POST['your_email'] set? Put the following into your function set_from_email(): print '<pre>' . htmlspecialchars( print_r( $_POST, TRUE ) ) . '</pre>';exit; –  toscho Mar 12 '12 at 18:02
    
$_POST['your_email'] is set I get the following output: [your_email] => myemail@example.net [friends_email] => friendsemail@example.com [subject] => Invite [message] => 127.0.1.1:8080/wordpress [action] => gp_send_email [nonce] => 27d36fee6a –  navanitachora Mar 13 '12 at 2:29
    
I am using the WP SMTP Mail plugin to send emais through gmail's SMTP server. The username I set for the gmail server becomes the from name and there does not seem to be a way to change that. I probably need to find a way to send emails on my local machine without using the plugin to make sure that my plugin works. I hope someone can shed some light on this. –  navanitachora Mar 13 '12 at 18:02
add comment

1 Answer

up vote 0 down vote accepted

After further investigation things seems to work with sendmail without the WP SMTP plugin. I have also been told by others that the WP SMTP plugin is needed to get email working on Windows hosts and is not needed if running your website on Linux. Hence, I think this was more to do with a specific plugin that to do with my code per se.

I think WP SMTP Mail gives the above behavior for security but I do not know enough about the SMTP protocol to comment further.

Thanks, nav

share|improve this answer
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.