Email template placeholders?

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 26, 2018   (RSS)

By Toledoh - November 22, 2018

Hi Guys.

I've using form builder plugin to create a form.  "Name of resident" is a multi-select checkboxes.

I've added:

 	    // Send Admin Alert via Email Template
			$emailHeaders = emailTemplate_loadFromDB(array(
			    'template_id'  => 'REGIS-TO-ADMIN',
			    'placeholders' => array(
			      'user.start_date'     => $_REQUEST['start_date'],
			      'user.start_time'    => $_REQUEST['start_time'],
			      'user.name_of_resident'    => $_REQUEST['name_of_resident'],
			      'user.end_time'    => $_REQUEST['end_time'],
			      'user.description_of_visit'    => $_REQUEST['description_of_visit'],
			      'user.summary_of_visit'    => $_REQUEST['summary_of_visit'],
			      'user.next_visit'     => $_REQUEST['next_visit'],
			
			  )));
			$mailErrors   = sendMessage($emailHeaders);

The data is added to the CMSB table just fine, but I get the error as detailed below, which I think is caused by trying to convert the string to a placeholder for the email?

E_NOTICE: Array to string conversion

Filepath: /home/XXX/public_html/cmsAdmin/lib/common.php

Line: 1733

#0 _errorlog_logErrorRecord() called at [/home/XXX/public_html/cmsAdmin/lib/errorlog_functions.php:62]
#1 _errorlog_catchRuntimeErrors() called
#2 str_replace() called at [/home/XXX/public_html/cmsAdmin/lib/common.php:1733]
#3 emailTemplate_replacePlaceholders() called at [/home/XXX/public_html/cmsAdmin/lib/common.php:1662]
#4 emailTemplate_loadFromDB() called at [/home/XXX/public_html/regis.php:80]

Cheers,

Tim (toledoh.com.au)

By gregThomas - November 23, 2018

Hey Tim,

The issue is that multi-select fields are sent in the request as arrays. So when the placeholders are added to the email, an error is thrown as you can't replace the string with an array.

Here is how I'd resolve the issue:

	// Send Admin Alert via Email Template
	$emailHeaders = emailTemplate_loadFromDB(array(
	  'template_id'  => 'REGIS-TO-ADMIN',
	  'placeholders' => array(
	    'user.start_date'           => $_REQUEST['start_date'],
	    'user.start_time'           => $_REQUEST['start_time'],
	    'user.name_of_resident'     => (is_array($_REQUEST['name_of_resident']))? implode(",", $_REQUEST['name_of_resident']): $_REQUEST['name_of_resident'] ,
	    'user.end_time'             => $_REQUEST['end_time'],
	    'user.description_of_visit' => $_REQUEST['description_of_visit'],
	    'user.summary_of_visit'     => $_REQUEST['summary_of_visit'],
	    'user.next_visit'           => $_REQUEST['next_visit'],
	)));
	$mailErrors   = sendMessage($emailHeaders);

So the code above will check if the name of a resident field is an array, if it is, it will combine the values into a string and separate them with ', '.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - November 23, 2018

Thanks Greg.

Do you think it would be a simple process to loop the send email function for every checkbox?  ie. if 1 resident is selected, 1 email is sent.  If 3 residents are selected, loop the send function 3 times, each with a different vale in the placeholder?

The reason for this is that the email template is structured grammatically for a single person.  It's not a big issue, but may be nicer to send an email per resident.

Cheers,

Tim (toledoh.com.au)