'textfield', '#title' => t('Contact description'), '#description' => t('This text will show at the top of the Author Contact block, can be used for instructions etc.'), '#default_value' => variable_get('authorcontact_desc', '') ); return $form; case 'save': variable_set('authorcontact_desc', $edit['authorcontact_desc']); break; case 'view': if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') { $block['subject'] = t('Contact Author'); $block['content'] = t('
') . variable_get('authorcontact_desc', '') . t('
'); $block['content'] .= drupal_get_form('authorcontact_form'); return $block; } break; } } /** * Define the form for the block */ // It would be good if this was collapsed! function authorcontact_form() { $node = node_load(arg(1)); $form['sendername'] = array( '#title' => t('Your name'), '#type' => 'textfield', '#required' => true ); $form['senderemail'] = array( '#title' => t('Email'), '#type' => 'textfield', '#required' => true ); $form['sendercomment'] = array( '#title' => t('Comment'), '#type' => 'textarea', '#rows' => 4, '#required' => true ); $form['nodetitle'] = array( '#type' => 'hidden', '#value' => $node->title ); $form['file_upload'] = array( '#title' =>t('Attach a file'), '#type' => 'file' ); $form['send'] = array( '#type' => 'submit', '#value' => t('Send') ); $form['#attributes'] = array('enctype' => "multipart/form-data"); return $form; } /** * Validate the form */ function authorcontact_form_validate($form_id, $form_values) { //check the email address is valid if(isset($form_values['email'])) { if (!preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/', $form_values['email'])) { form_set_error('email', t('Invalid email address')); } } } /** * Submit the form */ function authorcontact_form_submit($form_id, &$form_state) { $node = node_load(arg(1)); $pageAuthor = user_load(array('uid' => $node->uid)); $to = $pageAuthor->mail; $from = '"' . $form_state['values']['sendername'] . '" <' . $form_state['values']['senderemail'] . '>'; $attach = $form_state['values']['sendercomment']; // ADD FILE ATTACHMENT if ($file = file_save_upload('file_upload')) { // Check to see if the attachment exists. if ($file->filesize > 0) { // An attachment exists, so add it to body of email // Set initial values. $boundary_id = md5(uniqid(time())); $headers .= "Content-Type: multipart/mixed; boundary=$boundary_id"; $attach .= "\n--$boundary_id\n"; //set the mine type $attach .= "Content-Type: text/plain; charset=UTF-8; format=flowed;\n\n"; // Prepare the body: //$attach .= "\n\n\"; $attach .= "--$boundary_id\n"; // add the attachment $attach .= "Content-Type: ". $file->filemime ."; name=\"". basename($file->filename) ."\"\n"; $attach .= "Content-Transfer-Encoding: base64\n"; $attach .= "Content-Disposition: attachment; filename=\"". basename($file->filename) ."\"\n\n"; if (file_exists($file->filepath)) { $attach .= chunk_split(base64_encode(file_get_contents($file->filepath))); } else { $attach .= chunk_split(base64_encode(file_get_contents(file_directory_temp() .'/'. $file->filename))); } $attach .= "\n\n"; $attach .= "--$boundary_id--\n\n"; } $form_state['values']['sendercomment'] = $attach; } //end of adding attachment //create email vars $params = array( 'sendername' => $form_state['values']['sendername'], 'senderemail' => $form_state['values']['senderemail'], 'sendercomment' => $form_state['values']['sendercomment'], 'nodetitle' => $form_state['values']['nodetitle'], 'referer' => $_SERVER['HTTP_REFERER'] ); drupal_mail('authorcontact', 'sendcontact', $to, language_default(), $params, $from); //make note drupal_set_message(t('Contact sent from @from.', array('@from' => $form_state['values']['senderemail']))); } function authorcontact_mail($key, &$message, $params) { $language = $message['language']; switch($key) { case 'sendcontact': $message['subject'] = t('Contact via @site', array('@site' => variable_get('site_name', 'Your website'))); $message['body'] = t('You have recieved an enquiry through @sitename on the page @nodetitle at @referer Name: @name Email: @email Enquiry: @comment', array( '@sitename' => variable_get('site_name', t('')), '@nodetitle' => $params['nodetitle'], '@referer' => $params['referer'], '@name' => $params['sendername'], '@email' => $params['senderemail'], '@comment' => $params['sendercomment'] )); break; } }