Email template placeholders?

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

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)

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

Hey Tim,

That shouldn't be too difficult, you'd have to update the multiselect name_of_resident field so that its the email_of_resident instead, then ensure its value contains the email to send to, then you could use the code below to send the email:

if(is_array($_REQUEST['email_of_resident'])) {
  foreach($_REQUEST['email_of_resident'] as $email) {
    // 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.email_of_resident'    => $email,
        '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);
  }
}

Something to keep in mind, if the value in the multi-select field is the email address anyone who's on the page could see the email addresses if they viewed the HTML, so I'd not recommend using this method on a public page.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com