How To Render A Widget In An Email Template

Link: https://support.brilliantdirectories.com/support/solutions/articles/12000075479-how-to-render-a-widget-in-an-email-template

To include a widget in an email template use the shortcode [widget=Name of Widget].

 

Another option is to have the results of a widget sent to a variable. This option will apply if you have a custom widget and you want more control over what data will be used in the e-mail template.

 
STEP 1: Create the widget variable

Turn the printed data from the widget into a variable
<?php
     $widget_var = widget("Name of Widget");

?>

 

Another way to combine more data before and after the widget and combine it into one variable.
<?php 

ob_start();
echo "Beginning Text";

echo widget("Name of Widget");
echo "Ending Text";

$widget_var = ob_get_contents();

ob_end_clean();

?>

STEP 2:  Include this variable in the email template.

       

Then reference the variable inside of your email template as %%%widget_var%%%.


STEP 3: Use the following code to pass this newly created variable into the email template and send the email

Add the variable to the $w variable so it will become accessible and executable within the email template. You
<?php
      $user = getUser($user_id,$w); /// Get the email address from a member
      $w['widget_var'] = $widget_var;  /// Add the widget contents to a variable

$email = prepareEmail('templae-name-goes-here', $w);  /// Email Template to Send

$results = sendEmailTemplate($email['sender'],$user['email'],$email['subject'],$email['html'],$email['text'],$email['priority'],$w); print_r($results); /// This will show the results of the email send. When it returns success, the email was sent.

 ?>

ADVANCED USAGE:

You can also pass the value directly into the email, skipping the email template. This will allow you to create your own email template that sends an email with your own custom logic.


///This must include the sender name and email address. Name <[email protected]>

$email['sender'] = $w['website_name']." <".$w['website_email'].">";

/// This must be a valid email

$user['email] = "to@recipient_domain.com";

/// You must input a subject

$email['subject'] = "Subject";

/// You must input a body in html

$email['html'] = "<strong>Your html code</strong>".$widget_var."with the variable from the widget included"; 


/// Email Text, If this is blank the system will strip the tags and send a text version
$email['text'] = "Text version of your email";

///Email Priority, can be left blank or input a number
$email['priority'] = 2;

 /// This function will send the email

sendEmailTemplate($email['sender'], $user['email'], $email['subject'], $email['html'], $email['text'], $email['priority'], $w);  


* Note both the subject and a body are required to send an email this way.