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 = '[email protected]';
$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;
}
}