• Hello everyone!
    How can I know what email type is sending by wp_mail()? (Order confirmation, Registration etc)

    • This topic was modified 6 years, 11 months ago by dimatall.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Hook the “wp_mail” filter. All the arguments for wp_mail() are passed in a single array to your callback. Dump out the values to a log file or email yourself the values if you prefer. If you use wp_mail() to email yourself, you need to remove your filter hook first to avoid entering an infinite loop. That’s right, callbacks can remove themselves from the filter stack. They can also add themselves back after calling wp_mail() so that any subsequent emails sent as part of the same process can also be captured.

    There are opportunities for plugins to alter mail parameters after this filter fires, but you are getting the arguments exactly as passed to wp_mail() before anything else starts changing things.

    Thread Starter dimatall

    (@dimatall)

    Thanks for you reply @bcworkz. Yes I can add filter to wp_mail but how do I know what exactly mail is sending. e.g. I have two emails “Order confirmation” and “Newsletter”. And I have to catch and change only “Order confirmation” template. “Newsletter” template has to be as it is. I need some property like args['type'] = ('order', 'newsletter', 'sign up') to have a difference between email templates.

    • This reply was modified 6 years, 11 months ago by dimatall.
    Thread Starter dimatall

    (@dimatall)

    Probably there is the only single way distinguishing one email from another – is to parse a args[‘message’] content.

    • This reply was modified 6 years, 11 months ago by dimatall.
    Moderator bcworkz

    (@bcworkz)

    “parse a args[‘message’] content.”

    Correct, though “parse” may not be quite the right term. More like search for a signature phrase. The rest of the message does not matter. Parsing implies (at least to me) somehow interpreting the entire content. Regardless of semantics, you have the right idea.

    Thread Starter dimatall

    (@dimatall)

    I have already implemented that by searching a signature phrase. It could be useful for someone else. Here is example of “Order processing” email.
    I add a signature to email template:

    function add_pot_sendgrid_order_mark($order, $sent_to_admin = false, $plain_text = false, $email = '') {
            if (!$sent_to_admin && !$plain_text) {
                echo '<span style="display:none" data-pot-sendgrid="order-confirmation-email321"></span>';
            }
    }
    add_action( 'woocommerce_email_before_order_table', 'add_pot_sendgrid_order_mark', 10, 4);

    And then catch this email by signature

    function set_email_template($args) {
            
         // True if signature has been founded
         $isOrder = strpos($args['message'], 'order-confirmation-email321');
    
         ...
    
         return $args;
    }
    add_filter('wp_mail', 'set_email_template', 100);

    Thanks @bcworkz for conversation!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_mail() startpoint’ is closed to new replies.