1

Let's say i have a custom module in D7 , i want to add a feature to this .

the feature is : when a user publish a node send an email to someone .

   function sending_email_node_update($node){
    if (isset($node->original->status) && $node->original->status == 0 && $node->status == 1){
        drupal_mail('sending_email','email_action','soheilsadeghbayan@yahoo.com',language_default());
  }
}

function sending_mail_mail($key, &$message, $params) {
switch ($key){
    case 'sending_email_node_update':
        $message['subject'] = t('this is my action report');
        $message['body'][] = t('this is a reporting from sending email made by soheil') ;

        break;
}
}

Could you please help me how to do this ?

5
  • you problem is in key, you set key email_action but in case statement you work with sending_email_node_update . :)
    – Yuseferi
    Jan 11 '14 at 4:01
  • thank you , i changed the key in drupal_mail to sending_email_node_update but nothing changed @Zhilevan !!!!
    – user23811
    Jan 11 '14 at 8:06
  • do u mean put case 'sending_email_node_update': to case 'email_action': and not work?
    – Yuseferi
    Jan 11 '14 at 8:19
  • yes that's right
    – user23811
    Jan 11 '14 at 8:34
  • It is strange , It had to work :(
    – Yuseferi
    Jan 11 '14 at 10:53
4

Why not just use Rules to do this. You'll have to watch a few tutorials to wrap your head around rules, but you won't have to re-invent the wheel or maintain any code.

If you need the custom module solution, check these out.

Check if node is being published: How can I detect when a node is changed from 'Published' to 'Unpublished'?

Then, use drupal_mail() to send your emails.

3
  • Yes, Rules is a good solution here. Note you can export your Rule configuration with your custom module, if required, using hook_default_rules_configuration Jan 10 '14 at 1:59
  • Thx guys , yes i know it's possible with Rules module , but i want to practice with coding . Thx
    – user23811
    Jan 10 '14 at 11:56
  • I wouldn't use Rules. If you can code, you should code. I used to think Rules was the answer, but in reality it's just a load of cruft that's complicates what you're really trying to do. Also, we have a way for revisioning code that's far less complicated that hook_default_rules_cofiguration: git.
    – Angry Dan
    Nov 27 '14 at 11:06
1

Why not using the Message stack? I think Rules is sometime is overrated:

  1. To much work to set up the rule.
  2. Why to use a large module like rule when you can use your own module and deal with the situation in 20 +- lines of code?
  3. How would people become module developer and contribute code back to the community if they need rules to do simple action when a entity is updated?

And this how you can send email to a user when a node is updated in 4 simple steps:

  1. Download the module Message and Message Notify and enable them.
  2. Create a Message Type with the text your want to put in the mail.
  3. Create your own module or just export the Message with Features (this could be your module).
  4. Use the next gist in your module: https://gist.github.com/RoySegall/8369317

This is how that gist looks like:

/**
* Implements hook_node_update().
*/
function hook_node_load($node) {
  if ($node->original->status == $node->status) {
    return;
  }

  $message = message_create(MESSAGE_NAME, array('uid' => SOME_UID));
  message_notify_send_message($message);
}

The Message stack is great. Commerce Kickstart and Drupal Commons are using them, why not you to?

3
  • HI @Roy Segall because i do want to create it programaticlly, i know it's possible with these stuff but this kind of practicing
    – user23811
    Jan 11 '14 at 17:18
  • 1
    +1 for the "message" part of your answer, though I don't really disagree with what you wrote about Rules. Feb 13 '17 at 11:25
  • Well.. We are artists of websites. just like an artist have several tools to paint(oil based colors, water based, different brushes etc., etc.) we have different tools to create sites. I don't like rules but if you like rules - you go girl :)
    – Roy Segall
    Feb 14 '17 at 11:42
0

I have a solution for you, use hook_node_update something like this in your custom module run when the node be published

function MYMODULE_node_update($node){
  if (isset($node->original->status) && $node->original->status == 0 && $node->status == 1){

       put your code here to run after node publish

  }
}
5
  • I do appreciate it , i'll give it a try and let you know THX ;)
    – user23811
    Jan 10 '14 at 13:49
  • @Soheil you welcome, I glad to help you Soheil ;)
    – Yuseferi
    Jan 10 '14 at 20:27
  • i edited my post . is this true ? can you please help me out ?
    – user23811
    Jan 10 '14 at 21:09
  • @Soheil , please dont change your question completely,It's convert all answer to waste :(
    – Yuseferi
    Jan 24 '14 at 18:03
  • put this comment below my question not here if it's about my question not your answer by the way i have not changed my question like 10 days and the last person who answered it was you ... so don't be so hard ;) i'll put the right answer in a minute
    – user23811
    Jan 24 '14 at 20:26
0

while calling drupal_mail function the second argument is key and you are sending key as email_action and in you hook_mail you are checking for sending_email_node_update instead of email_action.. once you fix this you will be able to catch that mail just before being sent.. do your stuff there and done..

one more thing i would suggest you is check out drupal RULES module https://drupal.org/project/rules ... The Rules module allows site administrators to define conditionally executed actions based on occurring events..

1
  • thank you , i changed the key in drupal_mail to sending_email_node_update but nothing changed @Ashish Sajwan
    – user23811
    Jan 11 '14 at 8:07

Your Answer

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