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

  • Do you really just pass $from_mail through … and expect a visible change? – fuxia 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; – fuxia 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] => [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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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