0

I want that if a user subscribe to a comments & post that has been posted by an author, so how i can send mail notification to both the author and the subscriber at the same time. I want that author should get a notification that he has some comment on his post. and the subscriber that author has posted some status or post.

    <?php
/*
 * Implement hook_menu()
 */


function comment_notification_menu(){
  $items=array();
        $items['admin/customize'] = array(
          'title' => 'Send Comment notifications',
        'discription' => 'Admin will send notification to user about updates',
        'type' => MENU_NORMAL_ITEM,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('comment_notification_form'),
        'access arguments' => array('access adminstration page'),
        'access callback' => TRUE,
          );

  return $items;
}

/**
 * Form Structure
 */

function comment_notification_form($form, &$form_state){
  $form=array();
  $form['send_mail_from']=array(
    '#title' => 'Send Mail From',
    '#discription' => 'To whom you want to send form',
    '#row' => 5,
    '#columns' => 10,
    '#type' => 'textfield',
    //'#required' => TRUE,
    '#default_value' => variable_get('send_mail_from'),
    );
//here the admin can wite subject for the mail.
  $form['mail_subject']=array(
      '#title' => 'Subject',
      '#discription' => 'the purpous of this mail',
      '#type' => 'textfield',
      '#size' =>149,
      '#maxlenght' =>200,
      '#required'=>TRUE,
      '#default_value' => variable_get('mail_subject'),
    );
  $form['mail_body']=array(
      '#title' => 'Body',
      '#discription'=>'the body of your mail.',
      '#type' => 'textarea',
      '#row' => 20,
      '#columns' => 50,
      '#required' => TRUE,
      '#default_value' => variable_get('mail_body'),
    );
   $form['mail_bcc']=array(
      '#title' => 'BCC this mail to all',
      '#type' => 'checkbox',

    );


   $form['help'] = array(
        '#theme' => 'token_tree',
        '#token_types' => array('node') ,
     );

  return system_settings_form($form);
}





/**
 * Implement hook_comment_insert().
 */

/*
 *Implement token_replace(). , variable_get()
 */


function comment_notification_comment_insert($comment){


if ($comment->field_comment_notification['und'][0]['value'] == '1') {
      global $user;
      if ($user->uid == 1){

        $user_mail = $user->mail;//user has login
        //dsm($email);
  }
  else{

       $user_mail = $comment->mail;//uesr has not login
  }


      $to = $user_mail;
       // $to = 'm.nikhilanant@gmail.com';
      $message = token_replace(variable_get('mail_body')); // Body of your email here.

$subject = token_replace(variable_get('mail_subject')); // subject of e-mail
$body = token_replace(variable_get('mail_body')); //it might be any variable from the form eg. $form_state['values']['your_field']


//params is the array passed to hook_mail function
$params = array(
    'subject' => $subject,
    'body' => $body,
    );
      $message = drupal_mail('comment_notification', 'loaded',
        $to,language_default(),
        $params,$to, TRUE);
    }

}




function comment_notification_mail($key, &$message, $params) {
/*dsm($key);
dsm($message);
dsm($params);
*/
  switch($key) {

    case 'loaded':
      $message['subject'] = token_replace(variable_get('mail_subject'));
      $message['body'][] = token_replace(variable_get('mail_body'));
      break;

  }
}
4
  • Describe your issue properly. Now it is not clear enough and write your queries in body part not in title.
    – DRUPWAY
    Jan 30, 2015 at 6:40
  • 1
    Have you tried the "subscriptions" module or the "comment_notify" module? They may fit your needs without having to write custom code.
    – rooby
    Jan 30, 2015 at 7:10
  • i don't want to use this all module because i am at learning stage of drupal and also new so i want to learn how to do it custom. Jan 30, 2015 at 8:20
  • As mentioned, you should look at the comment_notify module. Your wanting to learn is totally understandable, but it does exactly what you're trying to accomplish, so can be a good point of reference to learn how to do it yourself. The solution to your question is too broad. If you want a really high-level overview, could help with that.
    – skh
    Jan 30, 2015 at 10:21

1 Answer 1

0

I do not see a way to allow users to subscribe to comment notification, is that your issue?

In case it is you might have to do something like Create a menu item as 'Subscribe to comments' check this link to ajaxify for menu item. On page callback of your custom menu subscribe the current user(global $user) to the comments, to hold the data you can simply use a custom table let's name as mymoudle_comment_subscription with columns as record_id|uid|comments_on_type|flag update this table in the subscription menu callback,

Rest is what you have covered in your code, i.e. implement hook_comment_update to notify on existing comment update, hook_comment_delete notify user on comment delete, hook_comment_insert notify user on new comment is added. In your hook Implementation query your custom table 'mymoudle_comment_subscription' fetch all uids get user mail by uids or in case you do not allow user to change emails you can store email directly in your custom table and use drupal_mail as your code shows to mail all users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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