[Membership 2 Pro] Create custom email templates

Is it possible, by any means, even with custom code (I have a dev team who can help) to create custom automated email responses in Membership 2 Pro? Please advice.

  • Dimitris
    • Support Star

    Hello Simon,

    I trust you’re doing well today! :slight_smile:

    We’ve already escalated this to our Second Level Support dev in order to provide some pointers on this. Me or another colleague of mine will keep you posted here as soon as there’s a progress on this. Your patience is highly appreciated, as response times are larger than ours in Support forums and chats.

    Warm regards,

    Dimitris

  • Panos
    • SLS

    Hi Simon ,

    This definitely needs some custom coding. It’s not that hard to achieve and since you got a team of developers I’ll explain the steps.

    The easiest way would be to check when the action, that you want to send you message on, gets triggered to just send a message calling wp_mail directly. If you want to include it in the Membership Automated Notifications it is a bit more tricky as it needs specific steps to follow. One limitation is that you can send it correctly only via some of the Membership Events, I can’t guarantee that it will work on other actions.

    First you need to register the custom notification type:

    add_filter( 'ms_model_communication_get_communication_types', function( $types ) {
    $types[] = 'type_custom_comm';
    return $types;
    });

    add_filter( 'ms_model_communication_get_communication_type_classes', function( $classes ){
    $classes['type_custom_comm'] = 'MS_Model_Communication_Custom_Comm';
    return $classes;
    });

    add_filter( 'ms_model_communication_get_communication_type_titles', function( $title ){
    $title['type_custom_comm'] = 'Custom Communication Title';
    return $title;
    });

    Then you need to create the callback class:

    add_action( 'plugins_loaded', function(){

    if ( class_exists( 'MS_Model_Communication_Custom_Comm' ) || ! class_exists( 'MS_Model_Communication' ) ) {
    return;
    }

    class MS_Model_Communication_Custom_Comm extends MS_Model_Communication {

    protected $type = 'type_custom_comm';

    public function set_period_name( $field ) {
    $field['title'] = __( 'Message Delay', 'membership2' );
    $field['desc'] = __( 'Use "0" to send on due date, or another value to delay the message.', 'membership2' );

    return $field;
    }

    public function get_description() {
    return __(
    'Here you can describe when this custom notification will be sent', 'membership2'
    );
    }

    public function reset_to_default() {
    parent::reset_to_default();

    $this->subject = __( 'The subject of the custom notification', 'membership2' );
    $this->message = self::get_default_message();
    $this->enabled = false;
    $this->period_enabled = false; // Set to true if sent after action was triggered

    do_action( 'ms_model_communication_reset_to_default_after', $this->type, $this );
    }

    public static function get_default_message() {

    $subject = sprintf(
    __( 'Hi %1$s,', 'membership2' ),
    self::COMM_VAR_USERNAME
    );
    $body_notice = sprintf(
    __( 'This is a custom message %1$s sent from %2$s.', 'membership2' ),
    self::COMM_VAR_USERNAME,
    self::COMM_VAR_BLOG_NAME
    );
    $body_invoice = __( 'By the way, here are your latest invoice details:', 'membership2' );

    $html = sprintf(
    '<h2>%1$s</h2><br /><br />%2$s<br /><br />%3$s<br /><br />%4$s',
    $subject,
    $body_notice,
    $body_invoice,
    self::COMM_VAR_MS_INVOICE
    );

    return apply_filters(
    'ms_model_communication_custom_comm_get_default_message',
    $html
    );
    }
    }
    } );

    Finally you need to check on which Membership Event you want to send it. Here is how to hook on Membership events and in comments I have a list of available events to check:

    add_action( 'ms_model_event', function( $event, $data ){

    /*
    MS_Model_Event::TYPE_MS_CANCELED
    MS_Model_Event::TYPE_CREDIT_CARD_EXPIRE
    MS_Model_Event::TYPE_PAYMENT_FAILED
    MS_Model_Event::TYPE_MS_EXPIRED
    MS_Model_Event::TYPE_UPDATED_INFO
    MS_Model_Event::TYPE_PAID
    MS_Model_Event::TYPE_MS_SIGNED_UP
    MS_Model_Event::TYPE_MS_RENEWED
    MS_Model_Event::TYPE_MS_REGISTERED
    MS_Model_Event::TYPE_MS_RESETPASSWORD
    MS_Model_Event::TYPE_MS_VERIFYACCOUNT
    MS_Model_Event::TYPE_MS_MOVED:
    MS_Model_Event::TYPE_MS_DEACTIVATED:
    MS_Model_Event::TYPE_MS_TRIAL_EXPIRED:
    MS_Model_Event::TYPE_MS_DROPPED:
    MS_Model_Event::TYPE_MS_BEFORE_FINISHES:
    MS_Model_Event::TYPE_MS_AFTER_FINISHES:
    MS_Model_Event::TYPE_MS_BEFORE_TRIAL_FINISHES:
    MS_Model_Event::TYPE_MS_TRIAL_FINISHED:
    MS_Model_Event::TYPE_PAYMENT_PENDING:
    MS_Model_Event::TYPE_PAYMENT_DENIED:
    MS_Model_Event::TYPE_PAYMENT_BEFORE_DUE:
    MS_Model_Event::TYPE_PAYMENT_AFTER_DUE:
    */

    if ( MS_Model_Event::TYPE_MS_CANCELED == $event->type ) {

    if ( $data instanceof MS_Model_Relationship ) {
    $subscription = $data;
    $membership = $data->get_membership();
    } elseif ( $data instanceof MS_Model_Membership ) {
    $subscription = false;
    $membership = $data;
    } else {
    $subscription = false;
    $membership = false;
    }

    $comm = MS_Model_Communication::get_communication( $type, $membership );
    if ( ! $comm ) {
    continue;
    }
    $comm->enqueue_messages( $event, $data );
    }

    }, 10, 2);

    In the above example I use dummy content for communication type, class name etc which your devs would need to change according to their needs.

    Hope this helps!

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.