Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

English string gets 'escaped' #320

Closed
Beee4life opened this issue Oct 7, 2020 · 3 comments
Closed

English string gets 'escaped' #320

Beee4life opened this issue Oct 7, 2020 · 3 comments

Comments

@Beee4life
Copy link

Beee4life commented Oct 7, 2020

I have a cron job which sends a email when certain conditions are met.
For some reason the string in Dutch (which is the default language) shows 'normally', while the English string is escaped/transformed in some way, making it unreadable/not showing.

The cron job works without issues, which is the function below. SHown for reference.

function sd_cron_remind_user_eot() {

    $users = [ 59, 73 ]; // just some random user ids for example

    foreach ( $users as $user ) {
        // stored preferred language for user. Can be NL | EN (is always set)
        $preferred_language = get_user_meta( $user_id, 'sd8_preferred_language', true );

        if ( ICL_LANGUAGE_CODE != $preferred_language ) {
            if ( 'nl' == ICL_LANGUAGE_CODE ) {
                add_filter( 'acf/settings/current_language', function() {
                    return $preferred_language;
                } );
            } elseif ( $preferred_language == ICL_LANGUAGE_CODE ) {
                add_filter( 'acf/settings/current_language', function() {
                    return 'nl';
                } );
            }
        }

        // get subject from ACF fields
        $email_subject = get_field( 'sd_eot_reminder_email_subject', 'option' );
        $email_message = get_field( 'sd_eot_reminder_email_company', 'option' );

        // switch back to default language
        add_filter( 'acf/settings/current_language', function () {
            global $sitepress;
            return $sitepress->get_default_language();
        } );

        // if user has not been informed yet
        if ( false == get_user_meta( $user_id, 'sd8_informed_eot', true ) ) {
            // if user has not set the setting to not receive email
            if ( false == get_user_meta( $user_id, 'no_emails', true ) ) {
                error_log($email_subject);
                // This outputs correctly every time (in both languages)

                do_action( 'sd_email_user', $user_id, false, $email_subject, $email_message, $preferred_language );
                // hook to trigger email function (see below)
            }
        }
    }
}
add_action( 'sd_orders_eot_reminder', 'sd_cron_remind_user_eot' );

do_action( 'sd_email_user' ) triggers the function below.

function sd_email_user_hook( $user_id, $post_id = 0, $email_subject, $email_message, $language = false ) {

    // needed to get the permalink in the correct language
    if ( false != $language ) {
        if ( ICL_LANGUAGE_CODE != $language ) {
            if ( 'nl' == ICL_LANGUAGE_CODE ) {
                add_filter( 'acf/settings/current_language', function() {
                    return 'en';
                } );
            } elseif ( 'en' == ICL_LANGUAGE_CODE ) {
                add_filter( 'acf/settings/current_language', function() {
                    return 'nl';
                } );
            }
        }
    }

    $post_author_data = get_userdata( $user_id );
    $to               = $post_author_data->user_email;
    $custom_message   = b3_replace_template_styling( $email_message );
    $custom_message   = strtr( $custom_message, b3_replace_email_vars( [] ) );
    $custom_message   = htmlspecialchars_decode( stripslashes( $custom_message ) );
    $from_email       = ( false != get_field( 'sd_support_email', 'option' ) ) ? get_field( 'sd_support_email', 'option' ) : 'support@email.com';
    $headers[]        = 'From: Site name <' . $from_email . '>' . "\r\n";
    $headers[]        = 'Content-Type: text/html; charset=UTF-8';

    $replace_vars = array(
        '%account_page%'  => b3_get_account_url(),
        '%profile_url%'   => get_permalink( $post_id ),
    );
    $custom_message = strtr( $custom_message, $replace_vars );

    add_filter( 'acf/settings/current_language', function () {
        global $sitepress;
        return $sitepress->get_default_language();
    } );

    // send email to user
    if ( false == get_user_meta( $user_id, 'no_emails', true ) ) {
        error_log($email_subject); // always outputs the correct string
        wp_mail( $to, $email_subject, $custom_message, $headers );
    }
}
add_action( 'sd_email_user', 'sd_email_user_hook', 10, 5 );

Site lang: NL/EN + User lang: NL
Correct subject in correct language

See image below.
Screenshot 2020-10-07 at 21 01 27

Site lang: NL/EN + User lang: EN
Escaped subject

See image below.
Screenshot 2020-10-07 at 21 04 25

My setup

  • Docker container with Mailhog installed
  • Wordpress with (relevant) plugins ACF and WPML (both latest pro versions)

EDIT: not sure if this is actually caused by Mailhog or by Phpmailer. Will also raise it as ticket there.

@Beee4life
Copy link
Author

After more research I learned the ENTIRE string, including encoding/charset shouldn't be more than 85 characters, not the raw subject.

Mine is 86, so that explains it.

@dregad
Copy link

dregad commented Oct 11, 2020

@Beee4life you are probably hitting the same bug I reported earlier, see #282

@Beee4life
Copy link
Author

@dregad my subject was 1 character to long, so I trimmed it now it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants