email_body (function)

Last updated: Mon, Apr 17, 2023 8:41 am
Return to Knowledgebase


Predefined email templates can be stored in users/views



See Also:

email (function)


# Parameter Data Type Required Description
1 $template string Yes Represents the name of the template file to be used for generating the email body HTML
2 $options = [] array No which is an optional associative array of key-value pairs that can be used within the template file to dynamically generate content


Data Type Description of Returned Data
string Returns a string value representing the HTML body of the email message generated by the template file.


$body = email_body('_email_adminPwReset.php',$params);

Further Documentation:

The email_body function in UserSpice is a custom function that takes in two parameters: $template and $options. The purpose of this function is to generate the HTML body of an email message using a specified template file and a set of optional parameters.

The function first sets the global variables $abs_us_root and $us_url_root to their respective values. It then uses the extract function to extract any key-value pairs from the $options array and create individual variables with their respective values. These variables can be used within the email template file to dynamically generate content based on the provided options.

The function then uses output buffering (ob_start) to capture the HTML output generated by the included template file. The function checks for the existence of the specified template file in two possible locations: the usersc/views directory and the users/views directory. If the file is found in either location, the function includes the file using the require statement.

Finally, the function returns the captured HTML output using ob_get_clean, which ends the output buffering and returns the contents of the buffer as a string.

Here is an example of how to use the email_body function:

Suppose you want to generate an email message using a template file called welcome_email.php located in the usersc/views directory. The template file contains HTML and PHP code to generate the email content dynamically. You can use the email_body function like this:

$template = 'welcome_email.php';
$options = [
'username' => 'John Doe',
'email' => ''
$email_body = email_body($template, $options);

// Use $email_body to send the email message

This will generate the email body HTML using the welcome_email.php template file and the provided $options array. The array contains two key-value pairs: username and email, which can be used within the template file to generate personalized content.