0

Our server is currently automatically setting the return path as {serverusername@serverhostname.tld}. When sending an email in PHP, I can override this by doing the following:

$return = '<bounce@domain.tld>';
$additional = "-f$return";
mail($to,$subject,$message,$headers, $additional);

My problem is that WordPress doesn't offer the functionality to add additional_parameters like the native PHP Mail function does. Is there a workaround to get this working? Here's the actual code that my WordPress plugin is using to send the email:

$headers = array(
    'From: Eden Mobility <website@domain.co.uk>',
    'Content-Type: text/html; charset=UTF-8',
    'Return-Path: <website@domain.co.uk>'
);
wp_mail( "myemailaddress@hidden.com", 'Your ' . $item . ' renewal is almost due', $template, $headers ); // LIVE

One thing I have noticed is that I'm now getting warnings when submitting mail:

PHP Warning: escapeshellcmd() has been disabled for security reasons in /home/user/domain.co.uk/wp-includes/class-phpmailer.php on line 1442 [02-Feb-2018 15:25:54 UTC]

  • the warning is something your host can clarify on, looks like they've disabled a PHP function – Tom J Nowell Feb 2 '18 at 16:55
  • I've logged into my server and enabled escapeshellcmd and re-tried, but it's the exact same outcome. I think I'll reach out to the server support team to see if they're aware of anything that could cause the issue. – Liam McArthur Feb 2 '18 at 16:58
1

Yes, there's a plugin that does this, and it's very simple. It involves the phpmailer_init to adjust the $phpmailer object

This is the code it uses:

https://plugins.trac.wordpress.org/browser/wp-mail-returnpath/trunk/index.php

if ( ! function_exists( 'wp_mail_returnpath_phpmailer_init' ) ) [
    function wp_mail_returnpath_phpmailer_init( $phpmailer ) {
        // Set the Sender (return-path) if it is not already set
        if ( filter_var( $params->Sender, FILTER_VALIDATE_EMAIL ) !== true ) {
            $phpmailer->Sender = $phpmailer->From;
        }
    }
}

add_action('phpmailer_init','wp_mail_returnpath_phpmailer_init');
  • I've tried that, but it still isn't working for me. It's still showing as username@serverhostname.tld. – Liam McArthur Feb 2 '18 at 14:54
  • hmmm are you sure the From value is correct then? How are you setting that? And can you edit your question to include all the relevant code? I only see a call to mail, but nothing for wp_mail – Tom J Nowell Feb 2 '18 at 15:09
  • Also, are you aware of the wp_mail_from and wp_mail_from_name filters mentioned in the docs for wp_mail? It's difficult to tell when your WP code is not posted. It might be that the code above works, but the from address is the default, not the desired address, in which case your question is not quite your problem, but without your code I can't tell. Also, wether the email is that of the administrator account or not is an important note that's missing – Tom J Nowell Feb 2 '18 at 15:11
  • I've updated my code so that you can see. Also, my wp_mail_from and wp_mail_from_name are set in my functions.php file so that they're used globally. – Liam McArthur Feb 2 '18 at 15:18

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.