0

I have a website with a lot of furniture products. Each node page has a link to "ask for more info" that opens the sitewide contact form. When I receive the e-mail message, I need to know to which product (node) the request refers to, because many people forget to say it. So I need to pass node url, title or nid to the form in order to insert it automatically in the sent contact e-mail.

Thank you very much

3
  • Are you using Webform for the sitewide contact form? If not, how is this set up? Mar 22, 2016 at 23:10
  • the sitewide contact form, as written
    – Cheope
    Mar 22, 2016 at 23:31
  • Of course! I keep thinking first of contrib modules. Below is my answer. Mar 22, 2016 at 23:45

4 Answers 4

0

this link "ask for more info" needs the nid appended as query string. and then in the form this can be used.

1
  • Yes, but which hook should I use to send it via mail? How to retrieve that value, simply with $_GET["myparam"]?
    – Cheope
    Mar 22, 2016 at 23:34
0

Looked at the code of the core contact module, the following line is important (contact.pages.inc:151):

// Send the e-mail to the recipients using the site default language.
drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);

By using hook_mail in your module, you could modify the body of the $message, like in the following example:

/**
 * Implements hook_mail().
 */
function yourmodule_mail($key, &$message, $params) {
  ...
  if($key == 'page_mail') {
    $nid = $_GET['mynid'];
    ...
    $message['body'][] = "Thanks for ordering the product " . $node->title;
    ...
  }
  ...
}

Hope this helps!

1
  • Thank you. It works with hook_mail_alter(&$message) and if($message['id'] == 'contact_page_mail')
    – Cheope
    Mar 23, 2016 at 0:28
0

Append a query string to your links (for example, /contact?nid=123). Now add the applicable field to your form (ie field_nid) - it can be hidden. In the field settings specify a default value using the token module - the format should be [current-page:query:nid] where nid is the name of the query string parameter.

1
  • Yes, I could add a hidden field, as explained here: willvincent.com/blog/…, but I think it can be solved in an easier way. Thanx
    – Cheope
    Mar 23, 2016 at 0:30
0

Working code, thanx to all for the help:

/**
 * Implements hook_mail().
 */
function mymodule_mail_alter(&$message) {
  if($message['id'] == 'contact_page_mail') {   // is contact_page_autoreply for the sender
    if (isset($_GET['nid'])){
        $node = node_load($_GET['nid']);
        $message['body'][] = "Ref #".$_GET['nid'].": " . $node->title . " [".$node->path."]";
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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