Support » Developing with WordPress » Customizing email function

  • nilssonline

    (@nilssonline)


    Hello,
    I am trying to customize emails sent to the user.
    I would like to add our website logo but I can’t really figure out how. Do I have to set the content type to text/html for the image to work?
    And should I create a plugin for this or can I keep it in the functions.php file?

    So far this is what I got. and this is placed in the functions.php file in my child theme:

    
    add_filter( 'wp_new_user_notification_email', 'edit_user_notification_email', 10, 4 );
    function edit_user_notification_email( $wp_new_user_notification_email, $user, $blogname ){
    //Subject
    	$new_subject_txt = __( 'Information about username and password');
    //Message
    	$new_msg_txt = sprintf(__( 'Hello and welcome to our website!
    		You have been assigned an education on our e-learning platform.
    		Your username is: %s'),  $user->user_email ) . "\r\n";
    $key = get_password_reset_key( $user );
        if ( is_wp_error( $key ) ) {
            return;
       }
     	$new_msg_txt .= __( 'To set your password, visit the following address:' ) . "\r\n\r\n";
        $new_msg_txt .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . "\r\n\r\n";
    
    $wp_new_user_notification_email['subject'] = $new_subject_txt;
    	$wp_new_user_notification_email['message'] = $new_msg_txt;
    	return $wp_new_user_notification_email;
    }
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • corrinarusso

    (@corrinarusso)

    There are several plugins to help you with this.
    Here are a couple :
    https://wordpress.org/plugins/email-templates/
    https://wordpress.org/plugins/wp-html-mail/

    nilssonline

    (@nilssonline)

    Hello @corrinarusso
    I’m using a plugin that does this. It’s called “Better Notifications for WP (bnfw)”.
    But you see I have a website in two languages (I’m using the Polylang plugin). The bnfw plugin works, but only for one language.

    So I want the same email in swedish and english, depending on the language set for the user. I haven’t found an email plugin where you can translate the messages or have different versions(?).

    Do you know if there’s a plugin out there that you can do this?

    corrinarusso

    (@corrinarusso)

    Did you try this approach already ?
    https://polylang.pro/doc/how-to-translate-emails/

    Moderator bcworkz

    (@bcworkz)

    The last parameter in your add_filter() line should be 3, not 4. You do need to set content type as “text/html” through the ‘wp_mail_content_type’ filter if you want HTML messaging. Your code is fine in functions.php of a child theme. Your callback could check the user’s language preference with get_user_locale() and then use the appropriate language for the email message.

    nilssonline

    (@nilssonline)

    Hello @bcworkz , thank’s for the help!
    I still have the code in the functions.php and The code below works and sends the message to the user. But I’m not sure how to add the wp_mail_content_type’ filter so I can add an image to the email as well, any tips?

    add_filter( 'wp_new_user_notification_email', 'edit_user_notification_email', 10, 3 );
    function edit_user_notification_email( $wp_new_user_notification_email, $user, $blogname ){
    	
    
    	$new_subject_txt = __( 'Information about username and password');
    	$new_msg_txt = __( 'Hello and welcome to Our website!'). "\r\n";
    	$new_msg_txt .=__( 'You have been assigned an education on our e-learning platform.'). "\r\n\r\n";
    	$new_msg_txt .= sprintf(__('Your username is: %s'),  $user->user_email ) . "\r\n";
    	$key = get_password_reset_key( $user );
    		if ( is_wp_error( $key ) ) {
    			return;
    		}
     	$new_msg_txt .= __( 'To set your password, visit the following address:' ) . "\r\n";
        $new_msg_txt .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . "\r\n\r\n";
    	$new_msg_txt .= __( 'For questions or problems with entering a password, contact our support.');
    	$wp_new_user_notification_email['subject'] = $new_subject_txt;
    	$wp_new_user_notification_email['message'] = $new_msg_txt;
    	return $wp_new_user_notification_email;
    }

    I also want to add this function to the fuctions.php file but for some reason it doesn’t work. What am I missing?

    add_filter( 'password_change_email', 'rt_change_password_mail_message', 10, 3 );
    function rt_change_password_mail_message( $pass_change_email, $user, $userdata ) {
    		$new_password_subject_txt = __( 'Reset password on our website');
    		$new_password_msg_txt = __('Someone has requested a password reset for the following account on %s:') . "\r\n";
    		$new_msg_txt .= sprintf(__( 'Username: %s' ), $user->user_login ) . "\r\n";
    		$new_msg_txt .= _('If this was a mistake, just ignore this email and nothing will happen.'). "\r\n";
    		$new_msg_txt .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n";
    		$new_msg_txt .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . "\r\n\r\n";*/
    
    	$pass_change_email['subject'] = $new_password_subject_txt;
    	$pass_change_email['message'] = $new_password_msg_txt;
    	return $pass_change_email; 
    }
    Moderator bcworkz

    (@bcworkz)

    You can set the content type in functions.php like so:

        add_filter('wp_mail_content_type', 'bcw_set_html_content_type');
        function bcw_set_html_content_type() {
                return 'text/html';
        }

    It’s considered poor practice to do this for all emails WP might send. Preferably you’d only add the callback just before you need it in your specific message, then remove it immediately afterwards so that other emails remain unaffected. While you could add the filter from your ‘wp_new_user_notification_email’ callback, I don’t see any way to remove it afterwards. In any case, I don’t think there’s likely to be any adverse effects for all email to be sent as HTML type.

    With text/html content, you can embed an img tag right in the message body. The src attribute needs to be an image accessible over the Internet. Be aware that many people have their mail clients set to block embedded images. Recipients would need to click on something in their mail client to let the image display.

    In the password change email filter, on the last $new_msg_txt .= line there is a syntax error. A remnant */ at the end of the line that does not belong.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.