• I’m working on a plugin that sends out custom email notifications, and using the Easy WP SMTP plugin so I can use Amazon’s SES service for sending messages. I’m running into an issue though where my emails aren’t sending using the From Name as specified in the Easy WP SMTP settings. I did some digging and here’s what I’m finding:

    Easy WP SMTP version 1.3.7
    WordPress version 4.9.8

    Mock Email Implementation:
    wp_mail([email protected], "Email Subject", "Message to be sent", array('Content-type: text/html'));

    When I send out this email, it’s sending just fine, but it’s showing up using the From Name “WordPress” and the From Email address as specified in the Easy WP SMTP settings. I do not have the “Force From Name Replacement” checked.

    From digging into the code I think I identified the problem. The wp_mail function in wp-includes/pluggable.php is defining the From Name on line 318 if it’s not defined in the headers. It’s not until line 477 of the same file where the phpmailer_init action is fired. If I’m understanding the code in the Easy WP SMTP plugin correctly, it’s hooking into the phpmailer_init action to fire the swpsmtp_init_smtp method in the easy-wp-smtp.php file of the plugin. On line 149 the plugin is looking to see if $phpmailer->FromName is empty, and if it is, it sets the From Name to the value from the plugin settings, otherwise, it uses the value already set, which in this case, WordPress has already defined the FromName, meaning that unless the “Force from Name Replacement” setting is checked, this will never be triggered.

    I did run a test email in the Easy WP SMTP settings, however, the test message doesn’t use the built in wp_mail method to send the email, rather it’s instantiating it’s own instance of phpmailer to send the test message, so that doesn’t really do anything.

    My proposed fix for this would be to add another conditional on line 149 to see if FromName is empty, or equal to “WordPress” and if either are true, proceed with the replacement.

    Let me know if there’s something different I need to be doing. I know I could code in what the From Name should be, but this plugin I’m building can’t use a hardcoded value, so it would need to pull that value from somewhere, but can’t be guaranteed the Easy WP SMTP plugin will be in use on every site.

    Hope I provided enough information to help.

    David

    • This topic was modified 6 years, 3 months ago by stndrdsnz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi, have you managed to solve your issue yet?

    Regards

    Thread Starter stndrdsnz

    (@stndrdsnz)

    Well, I figured out how to work around it, but I also know how to solve it. This can be implemented in a custom plugin, or a theme. Ideally though, this would be incorporated into the Easy WP SMTP plugin.

    I’m going to apologize if the code below doesn’t work, I just threw it together here, but didn’t actually test it out. It should be pretty close to what you’ll need to patch the plugin.

    add_filter('wp_mail_from_name', swpsmtp_replace_from_name);
    if ( ! function_exists('swpsmtp_replace_from_name' ) ) {
        public function swpsmtp_replace_from_name($original_email_from){
            $swpsmtp_options = get_option( 'swpsmtp_options' );
            if($original_email_from == 'WordPress'){
                return $swpsmtp_options[ 'from_name_field' ];
            } else {
                return $original_email_from;
            }
        }
    }

    Note: If incorporating this fix into the plugin, there are some other options that will need to be checked too, however, as a patch, this will replace the From Name if it matches the string “WordPress”. As a part of a solution for the plugin, you’d want to integrate this logic into the swpsmtp_init_smtp method, or rework that method to utilize the WordPress wp_mail hooks.

    David

    • This reply was modified 6 years, 3 months ago by stndrdsnz.
    • This reply was modified 6 years, 3 months ago by stndrdsnz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘From Name Replacement’ is closed to new replies.