0

I'm incorporating email notifications into a custom Drupal 7 module, and I'm using the following code for hook_mail().

$subject_template = variable_get('pto_notification_subject', PTO_NOTIFICATION_SUBJECT);
$message['subject'] = t(check_plain($subject_template), $params, array('langcode' => $langcode));
$body_template = variable_get('pto_notification_body', PTO_NOTIFICATION_BODY);
$message['body'][] = t(filter_xss($body_template), $params, array('langcode' => $langcode));

When I do that, I get this error:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1571 of /var/www/mysite.dev/public_html/includes/bootstrap.inc).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1571 of /var/www/mysite.dev/public_html/includes/bootstrap.inc).
Unable to send e-mail. Contact the site administrator if the problem persists.

If I print the value of $subject_template and $body_template, I see strings, not an arrays. Using is_array() on both confirms that as well. Any idea why check_plain() could be reading those variables as arrays?

I also tried the following code, but I am still getting the same error.

function pto_mail($key, &$message, $params) {
  $langcode = $message['language']->language;
  $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  if (!module_exists('mimemail')){
    $message['headers']['Mime-Version'] = '1.0';
  }
  switch ($key) {
    case 'request':
      $subject_template = variable_get('pto_notification_subject', PTO_NOTIFICATION_SUBJECT);
      $message['subject'] = t($subject_template, $params, array('langcode' => $langcode));
      $body_template = variable_get('pto_notification_body', PTO_NOTIFICATION_BODY);
      $message['body'][] = t($body_template, $params, array('langcode' => $langcode));
      break;
  }
}

Here's the result of print_r($params):

Array
(
    [!request_user_name] => Information Technology
    [!request_pto_type] => PTO
    [!request_amount] => 8.00
    [!request_date_range] => 7/13/2016
    [!request_reason] => Testing....again.
    [!request_submitted] => 7/19/2016
    [!site_url] => http://mysite.dev
    [!site_name] => My Site
    [headers] => Array
        (
            [Cc] => me@myemail.com
        )

)
10
  • 1
    Assuming you have a standard/working version of PHP, the only possible answer is that $subject_template is an array. Or maybe a class that implements __toString and ArrayAccess. It's not a string, anyway
    – Clive
    Jul 14 '16 at 14:28
  • And, of course, assuming that your call to check_plain($subject_template) is actually what's causing the problem. Placeholders for t() are also run through check_plain, so you're kind of doubling up there anyway, and it might not be your explicit line that's causing the problem. What's $langcode? Maybe an array?
    – Clive
    Jul 14 '16 at 14:32
  • If I'm reading correctly, it appears that t() only runs through check_plain() if using @parameters, not !parameters, which is what I'm using. (api.drupal.org/comment/25348#comment-25348) either way, I updated the post with some new code to remove the duplicate check_plain() and the problem persists. Jul 14 '16 at 19:39
  • Also, it is perfectly useless to pass a dynamic string to t(); it will be never translated.
    – apaderno
    Jul 14 '16 at 20:40
  • 1
    Not sure how is_array($text) cannot return true when the next line triggers the error that $text is an array. What did you try to do inside the if condition? You will have to do a watchdog call in there to really catch it. If I can get access to your set up I can take a look.
    – anoopjohn
    Jul 21 '16 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.