3 added 536 characters in body
source | link
function hook_rules_action_infomymodule_rules_action_info() {
 return array(
  '_send_new_content_mail' => array(
    'label' => t('Send Mail to users for new content'),
    'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
    ),
    'group' => t('custom'),
  ),
 );
}

Now define the template file in your hook_theme() and print $data inside it.

function mymodule_theme() {
  return array(
    'mymodule_new_content_mail' => array(
      'template' => 'mymodule-new-content-mail',
      'path' => drupal_get_path('module', 'mymodule') . '/templates',
      'variables' => array('data' => NULL),
    ),
  );
}

Now add new rule, select the event node-after publishing new content, add the node type in the condition for which you want to send mail and select _send_new_content_mail in action which will be present under 'custom'.

function hook_rules_action_info() {
 return array(
  '_send_new_content_mail' => array(
    'label' => t('Send Mail to users for new content'),
    'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
    ),
    'group' => t('custom'),
  ),
 );
}

Now define the template file in your hook_theme() and print $data inside it.

function mymodule_rules_action_info() {
 return array(
  '_send_new_content_mail' => array(
    'label' => t('Send Mail to users for new content'),
    'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
    ),
    'group' => t('custom'),
  ),
 );
}

Now define the template file in your hook_theme() and print $data inside it.

function mymodule_theme() {
  return array(
    'mymodule_new_content_mail' => array(
      'template' => 'mymodule-new-content-mail',
      'path' => drupal_get_path('module', 'mymodule') . '/templates',
      'variables' => array('data' => NULL),
    ),
  );
}

Now add new rule, select the event node-after publishing new content, add the node type in the condition for which you want to send mail and select _send_new_content_mail in action which will be present under 'custom'.

2 added 60 characters in body
source | link
function hook_rules_action_info() {
 return array(
  '_send_new_content_mail' => array(
    'label' => t('Send Mail to users for new content'),
    'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
    ),
    'group' => t('custom'),
  ),
 );
}
function hook_rules_action_info() {
 return array(
  'label' => t('Send Mail to users for new content'),
  'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
  ),
  'group' => t('custom'),
 );
}
function hook_rules_action_info() {
 return array(
  '_send_new_content_mail' => array(
    'label' => t('Send Mail to users for new content'),
    'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
    ),
    'group' => t('custom'),
  ),
 );
}
1
source | link

I would suggest you to simply write your custom rules action like this

function hook_rules_action_info() {
 return array(
  'label' => t('Send Mail to users for new content'),
  'parameter' => array(
    'node' => array('type' => 'node', 'label' => t('Current Node')),
  ),
  'group' => t('custom'),
 );
}

Define your callback:

function _send_new_content_mail($node) {
  global $base_url;
  $query = db_select('users', 'u');
  $query->join('users_roles', 'r', 'u.uid = r.uid');
  $query->leftJoin('field_data_field_first_name', 'n', 'u.uid = n.entity_id');
  $query->leftJoin('field_data_field_last_name', 'l', 'u.uid = l.entity_id');
  $query->fields('u', array('mail', 'name'));
  $query->fields('n', array('field_first_name_value'));
  $query->fields('l', array('field_last_name_value'));
  $query->condition('r.rid', ROLE_ID, '=');
  $result = $query->execute()->fetchAll();

  foreach ($result as $val) { 
    $alias = drupal_get_path_alias('node/' . $node->nid);
    $data = 'Hi ' . $val->field_first_name_value . ' ' . $val->field_last_name_value . ', new content has been published of your interest. ';
    $data .= 'Click ' . l('here', $base_url . '/' . $alias) . ' to see the content.';
    $key = 'new_content_mail';
    $subject = 'New content published';
    $to = $val->mail;
    _get_mail_content($data, $to, $key, $subject);
  }
}

Now define your mail content function:

function _get_mail_content($data, $to, $key, $subject) {
  $mail_content = _mail_content($data, $key);
  $params = array(
   'body' => $mail_content,
   'subject' => $subject,
  );
  $mail = drupal_mail('module_name', $key, $to, language_default(), $params, 'no-reply@info.com', TRUE);
  if ($mail['result']) {
   return TRUE;
  }
  else {
    $error_msg = 'Failed to send the email!';
    watchdog('new-content-email', $error_msg, array(), WATCHDOG_ALERT);
    return FALSE;
  }
}

Define _mail_content:

 function _mail_content($data , $key) {
   switch ($key) {
    case 'new_content_mail':
      return theme('mymodule_new_content_mail', array('data' => $data));
    break;
   }
 }

Now define the template file in your hook_theme() and print $data inside it.