Return Path management on wp_mail function (6 posts)

  1. Stefano Lissa
    Posted 3 years ago #

    Probably since the latest version of included php-mailer library I got some issue with return path header on emails. I'm used to add a return-path header to get back the bounces on specific mailboxes.

    The return-path header is overwritten by many mail server if not passed, on unix, with -f command line parameter.

    Php-mailer does it only if the Sender variable is set, it does not check if there is a return-path header. So, plugins that add such header are not granted that it will be respected.

    To solve this "issue", wp_mail can check the headers (as already done to extract from and fromname), look for a return-path header and use it's value to set the phpmailer Sender value.

    Otherwise plugin should add a filter on phpmailer which is a lot more complex.

    Patch to pluggable.php file is (for version 3.3.1):

    switch ( strtolower( $name ) ) {
      case 'return-path':
      $return_path = trim($content);
    case 'from':
    if ( strpos($content, '<' ) !== false ) {
    @@ -308,6 +311,10 @@
    if ( !isset( $from_name ) )
      $from_name = 'WordPress';
    if (!empty($return_path)) {
      $phpmailer->Sender = $return_path;

    what do you think? I tested it on a couple of servers with exim and postfix... some shared hosting have not that problem since they extract and "respect" the return path.

    Bye, Stefano.

  2. Abdussamad Abdurrazzaq
    Posted 3 years ago #

    I know it's been months since you posted this but thanks for researching this matter and writing about it. With the help of your post I have created a tiny plugin that sets the return-path header to equal the from address. You can download it from my site:


  3. presbycan
    Posted 2 years ago #

    A much simpler solution in a plugin before wp_mail():

    global $phpmailer;
    $phpmailer->Sender = $return_path;

  4. Stefano Lissa
    Posted 2 years ago #

    As far as I know, phpmailer is not granted to be defined bofore the wp_mail() call, so if can lead to problems.

    The correct way is to atach a filter to the phpmailer hook and change the phpmailer values. My observations are out of date :-).


  5. presbycan
    Posted 2 years ago #

    You're right. The $phpmailer object doesn't persist, but an instance is created with the first e-mail in each new sequence of e-mails. That means that $Sender cannot be altered before an instance of $phpmailer is created, only afterwards, correctly setting the Return-Path for the second and all subsequent e-mails in that sequence. A single e-mail cannot be sent out by itself with the correct Return-Path using this command. If you can advise me on the correct way to attach a filter to the phpmailer hook so that the value is set BEFORE the first e-mail is set, it might solve some problems.

  6. Stefano Lissa
    Posted 2 years ago #

    Hi, see my Mailer plugin, it uses the hook to filter the PHP mailer and set the Sender.


Topic Closed

This topic has been closed to new replies.

About this Topic