Hi,

I'm fairly new to drupal but am getting up to speed with it fairly quickly. Basically, I am trying to create a simple contact form which added to the bottom full page node. The node is a cck content type which custom fields etc.

The catch is, I need the contact form to be unique for each individual post. When you submit it, ideally, I need it to post an email to the data in the email field of the node item.

So for example, if the custom cck email field for node 17 is test@test.com then when you hit submit for that form, it will send the server generated email to test@test.com

For node 18, the custum cck email field value might be hello@hello.com, and when you hit submit on the contact form for that node, it goes to hello@hello.com.

I hope this makes sense, I have tried just about everything. my php knowledge is fairly limited however. I have tried use using the variables node->email_field[0]['view'] from the cck template but they don't seem to work. I can get them to print on the page, but I cannot set the receiptent with that data. It just doesn't work.

Anyway, hopefully you can help as my brain is about to explode.

Thanks again, Todd

Comments

mooffie’s picture

So for example, if the custom cck email field for node 17 is [...]

Why does this contact node has to hold an email? Since this contact node is linked (somehow, see later) to the main node, it could access the email field of the author of that main node.

Here's a recipe. I haven't actually tried it, so excepct some holes. It uses the most recent feature of Workflow-ng.

1. Add a nodereference field to the 'contact' node-type. We want all 'contact' nodes to point to the main nodes. It's a Good Thing. Let's call this field 'in_reply_to'.

2. How do you display this 'contact' node on a page? I assume you add the following to 'node.tpl.php':

$contact_node = array(..., 'type' => 'contact');
print drupal_get_form('contact_node_form', $contact_node);

3. We want to initialize this nodereferece to point to the node we're viewing, so:

$contact_node = array(..., 'type' => 'contact');
$contact_node->field_in_reply_to[0]['nid'] = $node->nid;
print drupal_get_form('contact_node_form', $contact_node);

You can use CSS to hide this nodereference, because users aren't supposed to be able to change it. And you probably want to use 'auto_nodetitle' too.

4. Install the Workflow-ng module and study it. It may take you a day or two.

5. Its latest version has the ability to load a referenced node. So ask it to carry our the following workflow:
- when a 'contact' node is submitted,
- load the referenced node,
- 'send email': send the 'current node' to the owner of that referenced node.

6. You don't want users to see those 'contact' nodes, so you'll have to install some access control module.

hottoddee’s picture

I have been able to include the contact node in the main node via the node.tpl.php template file through the use of suggested code:

$contact_node = array(..., 'type' => 'contact');
print drupal_get_form('contact_node_form', $contact_node);

Works great and to be honest that is one step forward for me. I have installed both the workng module and the token module. Where I am confused is how to get the data from the main node and inserting it into the contact node.

I have a field that is a nodereference field called in_reply_to.

if i were to include the following code in the node.tpl.php file, i thought i should be able to reference the fields in that currently viewed node.

so for example, if i wanted to set the default value for the subject field of the form i might so replace $object['subject'] with $node->field_subject
or any data stored in that node. It doesn't work though, I can print a field out at any point, but I just cannot 'move' that data, if you like, into the fields of the form. I'm very confused...

See code below, I have taken it from the contact form with attachment module which posted here on drupal.org


<?php

function contactform($form){
  $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('your name'),
      '#default_value' => $object['name'],
      '#size' => 30,
      '#maxlength' => 128,
      '#required' => TRUE,
  );

  $form['eMail'] = array(
      '#type' => 'textfield',
      '#title' => t('your email-adress'),
      '#default_value' => $object['eMail'],
      '#size' => 30,
      '#maxlength' => 128,
      '#required' => TRUE,
  );


  $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('subject'),
      '#default_value' => $object['subject'],
      '#size' => 30,
      '#maxlength' => 128,
      '#required' => TRUE,
  );


  $form['message'] = array(
      '#type' => 'textarea',
      '#title' => t('your message'),
      '#default_value' => $object['message'],
      '#size' => 30,
      '#maxlength' => 400,
      '#rows'    => 7,
      '#required' => TRUE,
  );

  $form['file1'] = array(
      '#type' => 'file',
        '#title' => t('attach your files here'),
  );
  $form['file2'] = array(
      '#type' => 'file',
  );
  $form['file3'] = array(
      '#type' => 'file',
  );

  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('send email'),
  );

  $form['#attributes'] = array(
    'enctype' => 'multipart/form-data',
    'id' => 'contactform',
  );

  return $form;
 
}

// validation function for the contact form
function contactform_validate($form_id, $form_values) {
    // first we validate if there is a email injection
    $finds = array("/bcc:/i",
            "/Content-Type:/i",
            "/Mime-Type:/i",
            "/MIME-Version:/i",
            "/multipart\/mixed/i",
            "/boundary=/i",
            "/subject:/i",
            "/cc:/i",
            "/to:/i");
    foreach($form_values as $value)
          foreach($finds as $find)
                if(preg_match($find,$value))
                    form_set_error('', '<h2 class="red center">Stop spamming</h2>');

    // then we validate the email-adress
    if (!valid_email_address($form_values['eMail']) && !empty($form_values['eMail']))
        form_set_error('', t('Please check the spelling of your email-adress.'));
}

// submit function for the contact form
function contactform_submit($form_id, $form_values) {

  $headers   = array();
  $mailkey   = 'contact-with-attachment';
  $from      = $form_values['name'].' <'.$form_values['eMail'].'>';
  $recipient = 'hottoddee@gmail.com';
  $subject   = $form_values['subject'];
  $body      = wordwrap($form_values['message']);
  $reply     = 'Thank you for your message.';
  $goto      = '<front>';

  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 300))) {
      $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
      drupal_set_message('<h3 class="red center">'.$output.'</h3>');
      drupal_goto($goto);
  }else{
    if(file_check_upload('file1')){
      $trenner  = md5(uniqid(time()));
      $headers  = array('Content-Type' => "multipart/mixed; boundary=$trenner");
      $message  = "\n--$trenner\n";
      $message .= "Content-Type: text/plain; charset=UTF-8; format=flowed;"."\n\n"; // sets the mime type
      $message .= $body."\n";
      $message .= "\n\n";
      for($i=1;$i<=3;$i++){
        $file = file_check_upload('file'.$i);
        if($file->filename){
          $file->filepath = str_replace("\\","\\\\",$file->filepath);
          $message   .= "--$trenner"."\n";
          $message  .= "Content-Type:$file->filemime;\n\tname=$file->filename\n";
          $message  .= "Content-Transfer-Encoding: base64\n";
          $message  .= "Content-Disposition: attachment;\n\tfilename=$file->filename\n\n";
          $filedata  = fread(fopen($file->filepath, "rb"), $file->filesize);
          $message  .= chunk_split(base64_encode($filedata));
          $message  .= "\n\n";
        }
      }
      $message .= "--$trenner--";
    }else{
      $message = $body;
    }

    // send mail
    drupal_mail($mailkey, $recipient, $subject, $message, $from, $headers);

    // Reply
    drupal_mail($mailkey, $from, $subject, wordwrap($reply), $recipient);

    // Log the operation:
    flood_register_event('contact');
    watchdog('mail', t('%name-from use contact form', array('%name-from' => theme('placeholder', $form_values['name'] ." <$from>"),)));

    drupal_set_message('Your message has been sent to us.');
    drupal_goto($goto);
  }
}

print drupal_get_form('contactform', $form); 
gdevlugt’s picture

You might want to check out the Webform module. It basically can do everything you mentioned but doesn't require any custom php code.

hottoddee’s picture

Hiya,

I actually managed to figure this one out - have basically created a module which picks up the uid of the person that published the node, then stores that data in a variable which is then used to set the to field of the email address.

So far testing has worked. Once I have finished this module off - probably in a month or so, I will submit it to the drupal project. I predict it could be quite useful for anyone trying to create a gumtree type jobs board.

Todd