Drupal: Edit email template from contact form
When submitting a message in my site-wide contact form in Drupal 6.x I get the following message along the top of every message:
[Name] sent a message us开发者_JS百科ing the contact form at [www.mysite.com/contact]
I would like to remove this message. Looking around, I've found it comes from the contact.module here:
$message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
I've done a bit of research and it seems that I need to create a custom module with a hook_mail_alter() function to edit the contact.module. When it comes to this I get a bit lost. Could anyone kindly take me through the steps to accomplish the task?
Many thanks.
I did something like that recently. Here is a template you can use to get what you need. Most is from the contact module. The code below is from Drupal 7 but should work as is in Drupal 6.
/**
* Implementation of hook_mail_alter().
*/
function modulename_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail') {
$language = $message['language'];
$params = $message['params'];
$variables = array(
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
'!sender-name' => format_username($params['sender']),
'!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
);
$message['body'] = array();
$message['body'][] = t("Your custom message with variables", $variables, array('langcode' => $language->language));
$message['body'][] = $params['message']; // Append the user's message/
}
}
function theme_mail_alter(&$message) {
// only alter contact forms
if (!empty($message['id']) && $message['id'] == 'contact_page_mail') {
$contact_message = $message['params']['contact_message'];
$message['body'] = [];
$fields = $contact_message->getFields();
$new_body .= 'Message:' . PHP_EOL . $contact_message->get('message')->value . PHP_EOL . PHP_EOL;
foreach ($fields as $field_name => $field) {
if (get_class($field->getFieldDefinition()) == 'Drupal\field\Entity\FieldConfig') {
$new_body .= $field->getFieldDefinition()->label() . ':' . PHP_EOL;
if (isset($contact_message->get($field_name)->entity->uri->value)) {
$uri = $contact_message->get($field_name)->entity->uri->value;
$url = file_create_url($uri);
$new_body .= $url . PHP_EOL . PHP_EOL;
} else {
$new_body .= $contact_message->get($field_name)->value . PHP_EOL . PHP_EOL;
}
}
}
$message['body'][] = $new_body;
}
}
精彩评论