• Resolved Greg Marshall

    (@timeassistant)


    Hello,

    I am looking to append a string of text (custom email signature) to the standard email notifications sent when submitting a form. I have come across the custom form mail data snippet below, however I cannot really find any documentation on it or anything really detailing the options and how it can be manipulated.

    add_filter( 'forminator_custom_form_mail_data', function( $data, $custom_form, $entry ){
    	return $data;
    }, 10, 3 );

    I need to be able to retrieve the custom post ID where the form is embedded, compare it with the assigned taxonomies to the CPT and then append the correct email signature (already stored in an accessible variable) to the end of the email content (E.g. after the all field data tag)

    I previously was using ninja forms but am migrating away from it to your plugin. I have the conditional statements to check the taxonomies etc, I just need assistance retrieving the embedded CPT ID and appending the string to the email content

    For reference the start of the ninja forms function is below and I’d be looking for the forminator alternative:

    function ninja_forms_action_email_message( $message, $data, $action_settings ){ 
    	
        $pageid = $data['extra']['post_id'];    // this is how to get the page id where the form is embedded
        $form_id = $data['form_id'];  // the current form id
        $customer_form_id = get_field('customer_form', $pageid); // getting the embedded ID that was set in an ACF field for the custom post
        if( $customer_form_id == $form_id ) {
            $message .= 'this is a generic text added to ALL messages';
    	if (has_term('term1', 'taxonomy1', $pageid)) {
               $message .= $customsig1;  // variable containing custom signature
            }
            // several more if statements to check and compare terms here
    
    }
    
    

    Thank you

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello @timeassistant !

    I hope you’re doing well today!

    I’ve checked the code and the request and shared them with our Second Line Support to further investigate – we will check whether we can provide a full code snippet here or a suggestion/template which you could later use.

    We’ll get back to you as soon as we hear back from the SLS team about your questions.

    Best regards,
    Pawel

    Thread Starter Greg Marshall

    (@timeassistant)

    Hello,

    thanks for the quick response, I look forward to hearing back from you.

    As an example (as many people like to know that you’ve tried yourself before they give help) I did attempt to modify the notifications sub array but that doesn’t seem to be the content sent via email.

    
    	foreach ( $custom_form->notifications as $key => $value ) {
    	$value['email-editor'] .= "<p>ADDED FOR TESTING</p>";
    

    Thanks

    Hello again @timeassistant !

    That was quick! We don’t have a full code, but we already have a template which you can use to expand, with some stubs to fill in and comments on where to get the data from and where to assign it. Please check if that would work for you:

    add_filter( 'forminator_custom_form_mail_admin_message', 'wpmudev_custom_sign_message_mail' , 10, 5 );
    
    function wpmudev_custom_sign_message_mail ( $message, $custom_form, $data, $entry, $cls ) {
    $pageid = $data['page_id']; // this is how to get the page id where the form is embedded 
    
    $form_id = $data['form_id']; // the current form id 
    
    $customer_form_id = get_field('customer_form', $pageid); // getting the embedded ID that was set in an ACF field for the custom post 
    
    if( $customer_form_id == $form_id ) { 
    
    $message .= 'this is a generic text added to ALL messages'; 	
    
    if (has_term('term1', 'taxonomy1', $pageid)) { 
    
    $message .= $customsig1; // variable containing custom signature 
    
    } 
    
    // several more if statements to check and compare terms here 
    
    }
    
    return $message;
    
    }

    Kind regards,
    Pawel

    Thread Starter Greg Marshall

    (@timeassistant)

    Hello,

    Thanks for this Ive now gotten my custom signatures working. The key piece I was missing was:

    ( $message, $custom_form, $data, $entry, $cls ) {

    Everything appears to be working however I am still to thoroughly test but initial results look good.

    Would definitely recommend adding this kind of stuff to your documentation

    thanks for the help ??

    Thread Starter Greg Marshall

    (@timeassistant)

    Hello, I had a further question regarding the above snippet.

    as mentioned n my last reply it is working as expected, however I have noticed another issue I have not experienced before. Some of our forms will not be using the “dynamic signatures” these forms send their email message to a Email to Text (SMS) service. The problem I now face is that the text message service will notp support HTML tags and the body content of all emails is auto wrapped in <p> tags. which means the text message literally shows on a phone as <p>message content</p>.

    I attempted to add this to the above code but it doesn’t seem to work fully

    $thecustomformarray = get_object_vars($custom_form);
    	$thecustomform = $thecustomformarray['notifications']['0'];
    	if (in_array("Customer Email to Text Message", $thecustomform)) {		
    	
    		$message = wp_strip_all_tags( $message );
    		
    		error_log('Customer Email to Text Message  FOUND');

    I do get the error log statement but the text message is still sent wrapped in the <p> tags.

    Could someone please advise. I also copied the snippet from Adam’s reply in this thread direct but it didn’t work either.

    https://www.ads-software.com/support/topic/plain-text-emails-3/

    Anyway would appreciate if someone can let me know a suitable way to do this please. OR better yet, a “Plain Text” checkbox in the email notification settings so that content is not auto wrapped in html tags ??

    EDIT: this appears to be happening by the email body editor in the add email notification screen. Switching from visual to text reinserts the p tags even if you remove them. They are also added again on save.

    • This reply was modified 2 years ago by Greg Marshall. Reason: added info
    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @timeassistant

    We apologize for the delay. We already pinged our SLS Team to review your additional query and still waiting for data. Thank you for your patience while we look into this further.

    Kind Regards,
    Kris

    Thread Starter Greg Marshall

    (@timeassistant)

    Thank you for letting me know, I was wondering if my additional question had been missed!

    I have used the code below to replace the ordered list output with an easy table layout for our regular email notifications. This worked very well, however for the text messages (SMS) side I have found that I still cannot strip the html tags, the <p> tag always seems to come back. For my Text message forms, they usually only send 1 field out of the 3 fields on the form so not sure if that makes a difference. I have commented out the stuff for the text messages as it doesnt work, the rest of the code is currently in use and is outputting the email data as intended (the extra html tags don’t matter there).

    add_filter( 'forminator_get_formatted_form_entry', 'forminator_modify_email_format', 20, 5 );
    
    function forminator_modify_email_format( $html, $custom_form, $data, $entry, $ignored_field_types ){
    
        $ignored_field_types = Forminator_Form_Entry_Model::ignored_fields();
    
        $form_fields = $custom_form->get_fields();
    
        $table_rows = array();
    
        if ( is_null( $form_fields ) ) {
    
            $form_fields = array();
    
        }
    
        foreach ( $form_fields as $form_field ) {
    
            $field_type = $form_field->__get( 'type' );
    
            if ( in_array( $field_type, $ignored_field_types, true ) ) {
    
                continue;
    
            }
    
            $label = $form_field->get_label_for_entry();
    
            $value = render_entry( $entry, $form_field->slug );
    
            $table_rows[ $label ] = $value;
    
        }
    	
    	$thecustomformarray = get_object_vars($custom_form);
    	$thecustomform = $thecustomformarray['notifications']['0'];
    	
    /*error_log('$thecustomform: ' . print_r($thecustomform, 1));
    	if ($thecustomform['label'] === 'Customer Email to Text Message' ||  strpos( $thecustomform['email-subject'], "123456") !== false || in_array("123456", $thecustomform) ) {
    // was unable to strip tags from the text/SMS from here
    		error_log('Customer TEXT  FOUND HTML');
    		$thecustomform['email-editor'] = wp_strip_all_tags($thecustomform['email-editor']);
     		$html = wp_strip_all_tags($html);
    
    	
    	}*/
    	if ($thecustomform['label'] === 'Customer Email' ||  $thecustomform['email-subject'] === 'NEW MESSAGE'){
    	//	error_log('Customer Email  FOUND HTML');
    			
    	
        $html = '<table>';
    
    	    foreach ($table_rows as $table_label => $table_value){
    	
    
                $html .= "<tr>";
    
                $html .= "<th style='text-align:left; vertical-align: top;'>" . $table_label ."</th>";
    
                $html .= "<td>" . $table_value ."</td>";
    
                $html .= "</tr>";
    
            }
        $html .= '</table>';
    	}
    	    
    	/*  USE THIS FOR TEXT MESSAGING WHEN ITS FIGURED OUT 
    		$html = '';
    	
    	foreach ($table_rows as $line_label => $line_value){
    		$html .= $line_label . ": " .  $line_value . "\r\n";
    	//} 
        */
    		
    
    return $html;
    
    }

    so if you guys can assist me in getting the html tags removed from the text messages that would be fantastic and followed by a big thank you. As you can see in the code I am mainly checking the email subject or the label of the email action in order to determine if its an email or an SMS/text. All text/SMS messages must have the pincode in the subject line, I swapped it out with 123456 here

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @timeassistant,

    Could you please check and see whether the following snippet helps:

    add_filter( 'wp_mail', 'wpmudev_strip_html_mail_message', 10, 1 );
    function wpmudev_strip_html_mail_message( $args ) {
    	if ( ! empty( $args['subject'] ) ) {
    		if ( strpos( $args['subject'], 'Your mail subject' ) !== false ) {
    			$args['message'] = wp_strip_all_tags( $args['message'] );
    		}
    	}
    	return $args;
    }
    

    You’ll need to replace “Your mail subject” in the above snippet with your Email Subject, which should help remove the HTML tags from the message.

    Please do let us know if you have any further queries.

    Kind Regards,
    Nithin

    Thread Starter Greg Marshall

    (@timeassistant)

    Hello,

    I never thought to actually just hook into the WP_Mail function and strip the tags that way, good idea and it appears to be working well.

    Thank you very much for this!

    However I would still recommend developing a Plain text email feature (check box) for the email editor (with all HTML tags natively stripped).

    Thanks again. I’ll be sure to recommend the plugin to others. Great support

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @timeassistant

    I’m glad that this issue is resolved.

    If you have a moment, I would very much appreciate it if you could quickly rate our plugin. This will help us keep the plugin available for free.

    https://www.ads-software.com/support/plugin/forminator/reviews/#new-post

    Best Regards
    Patrick Freitas

    Thread Starter Greg Marshall

    (@timeassistant)

    Thanks Patrick,

    I have just left a 5 star review for you there.

    Thanks again ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to append string / variable to email notification content’ is closed to new replies.