Email CCK custom module

Email CCK custom module

Categories:

I created a custom module that turns CCK forms into email feedback forms easily.

The purpose of this module is to easily create forms with the flexibility that the CCK module gives you and have the content of the forms be emailed to any address you want as well as create nodes and then have the ability to use VIEWS to sort the content and create reports and more.

Basically you create a content type using CCK module and then, in the content type settings form you will get the following options:

- turn this content type into feedback form.
- set message to show to the user after submitting the form.
- the page to redirect the user after submitting the form.
- set message that will be emailed to the user after submitting the form.

There are many details that can be improved, but its a nice solution to many real life needs for small businesses.

Here is the code.

You can create a folder inside yoursite.com/sites/all/modules called emailcck and then create three files as follows:

emailcck.module

<?php
// $Id: emailcck.module ,v 1 2008/01/18 10:40:00 pedrop Exp $

/**
* Implementation of hook_perm()
*/
function emailcck_perm() {
    return array(
'turn cck forms into email forms', 'bypass moderation');
}


/**
* Implementation of hook_form_alter().
*/
function emailcck_form_alter($form_id, &$form) {

    if (isset(
$form['#node_type']) && 'node_type_form' == $form_id) {
        if (
user_access('turn cck forms into email forms')) {
           
emailcck_node_settings_form($form);
        }
    }
    if (isset(
$form['#node']) && isset($form['#post']) && $form['#node']->type .'_node_form' == $form_id) {
       
//this is a node form
       
if (!user_access('bypass moderation')) {
           
$form['#redirect'] = variable_get('emailcck_redirect_'.$form['#node']->type,drupal_get_normal_path(variable_get('site_frontpage', 'node')));
        }
    }
}

/**
* Implementation of hook_nodeapi().
*/
function emailcck_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) {
    global
$user;
   
$error = false;
    switch (
$op){
        case
'validate':
            if (
emailcck_get_setting($node->type) == 1) {
               
                if (!
user_access('bypass moderation')) {
                   
                   if(
$node->field_email[0]['email'] != ''){
                       
                       
$support = emailcck_build_email(&$node,'support');
                       
$client = emailcck_build_email(&$node,'client');
                                              
                       
drupal_get_messages($type = NULL, $clear_queue = TRUE);
                       
                        if(
drupal_mail($support['mailkey'], $support['to'], $node->title, $support['body'], $support['from'], $support['headers'])){    
                           
drupal_mail($client['mailkey'], $client['to'], $node->title, $client['body'], $client['from'], $client['headers']);
                            break;
                        }else{
                           
form_set_error('field_email', t('There was a problem sending this form. Please try again later or email us at "%email" .', array('%email' => $support['to'])));
                            break;
                        }
                    }        
                   
                }
            }
        case
'submit':
             if(!
user_access('bypass moderation')){
               
$node->status = 0;
               
drupal_get_messages($type = NULL, $clear_queue = TRUE);
               
drupal_set_message(variable_get('emailcck_message_'.$node->type, 'Your form has been submitted successfully. We will contact you shortly.'));
                break;
             }  
    }
}


/**
* Gets the access setting associated with the given content type.
*/
function emailcck_get_setting($type) {
    return
variable_get('emailcck_roles_'. $type, 0);
}

/**
  * Helper function for hook_form_alter() renders the settings per node-type.
  */
function emailcck_node_settings_form(&$form) {
   
$form['emailcck'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('emailcck settings for this content type.'),
       
'#weight' => -1,
       
'#collapsible' => TRUE,
       
'#collapsed' => TRUE
   
);
   
$form['emailcck']['emailcck_roles'] = array(
       
'#type' => 'radios',
       
'#default_value' => emailcck_get_setting($form['#node_type']->type),
       
'#options' => array(
           
t('Disabled'),
           
t('Enabled')
            ),
       
'#description' => t('Wether to moderate this content and make this form emailable or not.')
    );
   
$form['emailcck']['emailcck_message'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Message'),
       
'#default_value' => variable_get('emailcck_message_'.$form['#node_type']->type,'Your form has been submitted successfully. We will contact you shortly.'),
       
'#size' => 60,
       
'#maxlength' => 128,
       
'#required' => FALSE,
       
'#description' => t('Message to show the user after submitting. If empty, system default message will be displayed.')
    );
   
$form['emailcck']['emailcck_redirect'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Redirect'),
       
'#default_value' => variable_get('emailcck_redirect_'.$form['#node_type']->type,''),
       
'#size' => 60,
       
'#maxlength' => 128,
       
'#required' => FALSE,
       
'#description' => t('Page to redirect the user after submitting content. E.g.: node/12 <br /> If empty, will redirect to the home page.')
    );
   
$form['emailcck']['emailcck_email'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Recipient'),
       
'#default_value' => variable_get('emailcck_email_'.$form['#node_type']->type, ''),
       
'#size' => 60,
       
'#maxlength' => 128,
       
'#required' => FALSE
   
);
   
$form['emailcck']['emailcck_noreply_email'] = array(
       
'#type' => 'textfield',
       
'#title' => t('No reply email'),
       
'#default_value' => variable_get('emailcck_noreply_email_'.$form['#node_type']->type, ''),
       
'#size' => 60,
       
'#maxlength' => 128,
       
'#required' => FALSE
   
);
   
$form['emailcck']['emailcck_response'] = array(
       
'#type' => 'textarea',
       
'#title' => t('Response'),
       
'#default_value' => variable_get('emailcck_response_'.$form['#node_type']->type, ''),
       
'#required' => FALSE
   
);

}

/**
* @desc helper function to build email
*/
function emailcck_build_email(&$node,$destination){
   
$message = array();
    switch(
$destination){
        case
"support":
           
$message['mailkey'] = 'emailcck_email';
           
$message['to'] = variable_get('emailcck_email_'.$node->type, variable_get('site_mail', ini_get('sendmail_from')));
           
$message['from'] = $node->field_email[0]['email'];

           
$message['headers'] = array(   'From' => $node->field_email[0]['email'],
                               
'Reply-To' => $node->field_email[0]['email'],
                               
'Sender' => $node->field_email[0]['email'],
                               
'Content-Type' => 'text/html; charset=ISO-8859-1',
                               
'MIME-Version' => '1.0');

           
$message['body'] = node_view($node, $teaser = FALSE, $page = TRUE, $links = FALSE);
        break;
       
        case
"client":
           
$message['mailkey'] = 'emailcck_noreply_email';
           
$message['to'] = $node->field_email[0]['email'];
           
$message['from'] = variable_get('emailcck_noreply_email_'.$node->type, '');

           
$message['headers'] = array(   'From' => variable_get('emailcck_noreply_email_'.$node->type, ''),
                               
'Reply-To' => variable_get('emailcck_noreply_email_'.$node->type, ''),
                               
'Sender' => variable_get('emailcck_noreply_email_'.$node->type, ''),
                               
'Content-Type' => 'text/html; charset=ISO-8859-1',
                               
'MIME-Version' => '1.0');

           
$message['body'] = variable_get('emailcck_response_'.$node->type, '');
        break;
    }
    return
$message;
}
?>

emailcck.info

; $Id.
name = Email CCK
description = Allow emailing of cck forms with individual settings for each content type. You will need to create a field in your form with system name of field_email
version = "$Name: DRUPAL-5--1-1 $"
package = Custom
dependencies = content

emailcck.install

<?php
// $Id: emailcck.install,v 1 2008/01/18 10:40:00 pedrop Exp $

/**
* Implementation of hook_install().
*/
function emailcck_install() {
 
db_query("UPDATE {system} SET weight = 10 WHERE name = 'emailcck'");
}

/**
* make sure hooks are invoked after cck main hooks
*/
function emailcck_update_1() {
 
$ret = array();
 
$ret[] = update_sql("UPDATE {system} SET weight = 10 WHERE name = 'emailcck'");
  return
$ret;
}
?>

Most of the code is based on the Automatic Nodetitles module.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.