Email template placeholders?

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

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

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)

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