Reputation: 57
i using contact module to send email to my email box,how to custom the email body? the default is:
the user name (http://example.com/user/3) use
http://example.com/contact ...
the message body
i have used hook_form_alter
adding some fields to the contact us form.eg:phone, address,company name.email dress, how to make them shows in the email body. thank you.
Upvotes: 1
Views: 3120
Reputation: 56
Entityforms module uses the standard Drupal fields which means you can use any standard Drupal fields. For those of you who have used Webforms, this module brings Webform's functionality into the "standard" Drupal field / entity world.
While Webform is a great module with a huge following, it does not integrate with standard Drupal field or entity aware modules. So for Drupal 7 sites, it is recommended to use Entityforms module!
Like Webform, it integrates well with Rules module for form submission notifications and allows for complex notifications logic.
Upvotes: 2
Reputation: 27573
As you can see in contact.module there are a few predefined, hardcoded variabels. IF you add your own fields to the form, they are not available for the mail.
In order to make them available there, you'd need to write, register and write your own mail-handler;
Implement hook_mail
function email_example_mail($key, &$message, $params) {
global $user;
$options = array(
'langcode' => $message['language']->language,
);
switch ($key) {
case 'contact_message':
$message['subject'] = t('E-mail sent from @site-name', array('@site-name' => variable_get('site_name', 'Drupal')), $options);
$message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
$message['body'][] = check_plain($params['message']);
break;
}
}
Then a method to send the mail:
function email_example_mail_send($form_values) {
$module = 'email_example';
$key = 'contact_message';
$to = $form_values['email'];
$from = variable_get('site_mail', '[email protected]');
$params = $form_values;
$language = language_default();
$send = TRUE;
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
if ($result['result'] == TRUE) {
drupal_set_message(t('Your message has been sent.'));
}
else {
drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
}
}
This method would then be called from within a custom submit handler:
function email_example_contact_form_submit($form, &$form_state) {
email_example_mail_send($form_state);
}
Which you register in a hook_form_alter
(I don't know the exact form_id for core contact form, place that where I put contact
):
function email_example_contact_form_alter($form, &$form_state) {
$form['#submit']['my_very_own_submit'] = array();
}
Upvotes: 2
Reputation: 1229
Muhammad has suggested the good solution that we should use Webform module to add fields. That way you don't need to write any code.
For your specific need, you can use hook_mail_alter which will help you to alter an email message and you can add your extra fields in email body.
Upvotes: 2
Reputation: 27043
Consider using webform module. You will not need to implement any hooks to add fields or configure which fields to be sent via email.
It's a lot easier that Drupal's contact module
Upvotes: 3