2

I have added on field in my site wide contact form through this code :

    function cstm_form_contact_site_form_alter(&$form, &$form_state, $form_id) {

    // Add a phone number field to the contact form.
    $form['phone'] = array(
    '#type' => 'textfield',
    '#maxlength' => 20,
    '#title' => t('Your phone'),
    );

    // Define the order of the top level elements on the form (include those from contact_site_form().
    $order = array('name', 'mail', 'phone', 'subject', 'cid', 'message', 'copy', 'actions');

    // Order the elements by changing their #weight property.
    foreach($order as $key => $field) {
    $form[$field]['#weight'] = $key;
    }
    }

Now Do I need to add drupal_mail with phone number into it or Drupal will consider it automatically ? As I am new to drupal I don't know much about it.

7
  • You have modified the form builder, which is only the first part. Now, you need to see how the module sends the email and build its body. – AKS Oct 15 '14 at 13:21
  • So basically I need to use drupal_mail ? Can I append my phone number in Body part of message like this $message['body'][] = t('Phone') .': '. $message['params']['phone']; ? Is it good practice to do in this way ? – Twix Oct 15 '14 at 13:26
  • hook_mail_alter is what you need, because you are altering email sent by another module. However, mail and form functions are not tied well and your first step would be to check if the form's submit handlers include all the responses at all. I understand what you are trying to do, and I wish I could check the contact module for you. I'll try to post a contextual answer some time later as I'm just checking the answers until I get my dinner ready at the restaurant :) – AKS Oct 15 '14 at 13:29
  • Thanks for your reply.Waiting for your contextual answer. – Twix Oct 15 '14 at 13:40
  • Can I use that function in my theme's template file like function mythemename_contact_site_form_alter() ? – Twix Oct 16 '14 at 5:48
0

You should use hook_mail_alter().

hook_mail_alter() allows you to modify the email cleanly before it is sent. The Contact module passes the entire form state to drupal_mail(), so you have access to it in this alter hook via the params key.

A quick example. I haven't tested it:

function my_module_mail_alter(&$message) {
  if ($message['id'] == 'contact_page_mail') {
    $phone = $message['params']['phone'];

    $message['body'][] = "Phone number: " . check_plain($phone);
  }
}

Let me know if that works.

4
  • Oops, Ayesh K was faster than me :-). His answer is in the comments. – wadmiraal Oct 15 '14 at 13:34
  • Will it be okay if I use check_plain ? – Twix Oct 15 '14 at 13:41
  • Actually, if you don't use an HTML email module, you can even just append it. Drupal sends emails in plain text by default, so there's no security issue there. Or, even better would be t("Phone: @num", array('@num' => $phone)). That way, it's translatable and safe. – wadmiraal Oct 15 '14 at 13:44
  • Can I use that function in my theme's template file like function mythemename_contact_site_form_alter() ? – Twix Oct 16 '14 at 5:47
0

I think what you are looking for is already answered here

Also if you are looking for basic tutorial you can check below links

http://www.blog.yamandi.com/how-use-drupalmail-function-tutorial

http://www.chrisfree.me/2010/01/override-drupal-contact-form

http://www.panda-os.com/2013/02/drupal-email-system/#.VD5-TPmSxws

And If you want to theme the outgoing mails with the help of custom templates, you should use the HTML Mail module.

Lets you theme your messages the same way you theme the rest of your website.

It works well with other modules, like Print, Mail mime, etc.

You have to install the Mail System module in order to use HTML mail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.