function contact_mail
Same name in other branches
- 7.x modules/contact/contact.module \contact_mail()
- 8.9.x core/modules/contact/contact.module \contact_mail()
- 10 core/modules/contact/contact.module \contact_mail()
- 11.x core/modules/contact/contact.module \contact_mail()
Implements hook_mail().
File
-
core/
modules/ contact/ contact.module, line 117
Code
function contact_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')->renderPlain($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);
$message['body'][] = t("If you don't want to receive such emails, 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')->renderPlain($build);
break;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.