function contact_mail in Drupal 10
Same name and namespace in other branches
- 8 core/modules/contact/contact.module \contact_mail()
- 6 modules/contact/contact.module \contact_mail()
- 7 modules/contact/contact.module \contact_mail()
- 9 core/modules/contact/contact.module \contact_mail()
Implements hook_mail().
File
- core/
modules/ contact/ contact.module, line 117 - Enables the use of personal and site-wide contact forms.
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;
}
}