function ContactHooks::mail
Implements hook_mail().
File
-
core/
modules/ contact/ src/ Hook/ ContactHooks.php, line 109
Class
- ContactHooks
- Hook implementations for contact.
Namespace
Drupal\contact\HookCode
public function mail($key, &$message, $params) {
$contact_message = $params['contact_message'];
/** @var \Drupal\user\UserInterface $sender */
$sender = $params['sender'];
$language = \Drupal::languageManager()->getLanguage($message['langcode']);
$variables = [
'@site-name' => \Drupal::config('system.site')->get('name'),
'@subject' => $contact_message->getSubject(),
'@form' => !empty($params['contact_form']) ? $params['contact_form']->label() : '',
'@form-url' => Url::fromRoute('<current>', [], [
'absolute' => TRUE,
'language' => $language,
])->toString(),
'@sender-name' => $sender->getDisplayName(),
];
if ($sender->isAuthenticated()) {
$variables['@sender-url'] = $sender->toUrl('canonical', [
'absolute' => TRUE,
'language' => $language,
])
->toString();
}
else {
$variables['@sender-url'] = $params['sender']->getEmail() ?? '';
}
$options = [
'langcode' => $language->getId(),
];
switch ($key) {
case 'page_mail':
case 'page_copy':
$message['subject'] .= t('[@form] @subject', $variables, $options);
$message['body'][] = t("@sender-name (@sender-url) sent a message using the contact form at @form-url.", $variables, $options);
$build = \Drupal::entityTypeManager()->getViewBuilder('contact_message')
->view($contact_message, 'mail');
$message['body'][] = \Drupal::service('renderer')->renderInIsolation($build);
break;
case 'page_autoreply':
$message['subject'] .= t('[@form] @subject', $variables, $options);
$message['body'][] = $params['contact_form']->getReply();
break;
case 'user_mail':
case 'user_copy':
$variables += [
'@recipient-name' => $params['recipient']->getDisplayName(),
'@recipient-edit-url' => $params['recipient']->toUrl('edit-form', [
'absolute' => TRUE,
'language' => $language,
])
->toString(),
];
$message['subject'] .= t('[@site-name] @subject', $variables, $options);
$message['body'][] = t('Hello @recipient-name,', $variables, $options);
$message['body'][] = t("@sender-name (@sender-url) has sent you a message via your contact form at @site-name.", $variables, $options);
// Only include the opt-out line in the original email and not in the
// copy to the sender. Also exclude this if the email was sent from a
// user administrator because they can always send emails even if the
// contacted user has disabled their contact form.
if ($key === 'user_mail' && !$params['sender']->hasPermission('administer users')) {
$message['body'][] = t("If you don't want to receive such messages, you can change your settings at @recipient-edit-url.", $variables, $options);
}
$build = \Drupal::entityTypeManager()->getViewBuilder('contact_message')
->view($contact_message, 'mail');
$message['body'][] = \Drupal::service('renderer')->renderInIsolation($build);
break;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.