Send an email when a node gets unpublished

Events happening in the community are now at Drupal community events on www.drupal.org.
vlooivlerke's picture

Rules have a great way to email a user when a node is created or updated. But lets say a 3rd party module like scheduler.module that publish and unpublish nodes is used. Rules can send an email when a node is published but not when it is unpublished. Any ideas, coz this shoud be simple.

Comments

"Content is published"

itangalo's picture

Hya

Here's one way of doing what you ask for:
1) A rule triggered on "After updating existing content".
2) Two conditions, checking the value "Content is published". The first condition checks "unchanged content" (which should be published) and the second checks "updated content" (which should not be published). Both unchanged and updated content is available in the configuration for these conditions.
3) An action, sending e-mail to the address you want. Possibly you use Tokens to get the e-mail address to the author.

Good luck!
//Johan Falk, NodeOne, Sweden

Thanks that really helped!

dwalker51's picture

Here's what my rule looks like, it sends a message (but will be replaced by an email) that is to be sent when the content type night report is published for the first time. The origina state is unpublished, and I downloaded the module save_edit, to add a publish button. The idea being that after maybe multiple edits during the night (keeping the state in unpublished) the user would then click the publish button to send an email to everyone (but only once, changing the node:status from 0 to 1). Though they can edit the node afterwards I don't want more emails going out. I will try and see about removing editing privileges to the node once its published. Thanks again.

Events

 After updating existing content    

Conditions

Content is of type
Parameter: Content: [node], Content types: Night Report

Content is published
Parameter: Content: [node]

NOT Content is published
Parameter: Content: [node-unchanged]

Actions

Show a message on the site
Parameter: Message: Sent an EMAIL of the Night...

For sending email when unpublishing the content

ajay547's picture

The above condition is correct bust just need a little tweak as the condition mentioned above is for sending email when the content is published.

I have changed it slightly for my condition " when content is unpublished"

Conditions

Content is published
Parameter: Content: [node-unchanged]

NOT Content is published
Parameter: Content: [node]

the part in bold is changed from previous.

Please let me know if you still feel difficulty for this

Regards

Cool, but...

Northern_Girl's picture

I have a rule to unpublish nodes after some time and I understand that you need a second rule that will send an email when the node goes from unchanged to changed (data comparision).

But this will send an email if the user modifies his node before the scheduled unpublishing process.

Is there a way to make shure that the email will be sent only if the node is unpublished and not modified? Should I add an "and" condition that would be : the node is unpublished? (or something like that).

(unless I'm missing something...).

Thanks.

Take a look a this issue.

paean99's picture

Take a look a this issue. https://www.drupal.org/node/2325525
In particular the 'status' condition which is equivalent to the published state.

Thanks...

Northern_Girl's picture

But I just cannot figure it out. Drives me nuts!

I have probably made 2 mistakes! 1) in the Event; 2) in the condition. I've tried different settings, but the email is not sent.

NG

Here is my rule:

{ "rules_unpublish_email" : {
"LABEL" : "unpublish_email",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_insert--board" : { "bundle" : "board" } },
"IF" : [
{ "data_is" : { "data" : [ "node:status" ], "value" : "0" } },
{ "AND" : [] },
{ "data_is" : { "data" : [ "node:status" ], "value" : "1" } }
],
"DO" : [
{ "mail" : {
"to" : "[node:author:mail]",
"subject" : "expired",
"message" : "message",
"language" : [ "" ]
}
}
]
}
}

Just add an action to your scheduled rule

spanners's picture

If you have a rule that is Unpublishing the content (is it a scheduled rule?) then can't you just add an action to that rule? Rather than creating a whole new rule?

--
Simon Olsen
Web design, graphic design, motion graphics
Find me on LinkedIn: https://au.linkedin.com/pub/simon-olsen/12/359/143

This Works

realEuph's picture

I think the problem is that your condition is "if (A and !A)". You are comparing the status of the node after saving to be 'Published' and 'Not Published'.

I created the following and it does what I think you wanted. Note the used of 'node-unchanged' in the second part of the 'IF' condition.

{ "rules_test_email_on_publish" : {
    "LABEL" : "TEST EMail on Publish",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "test" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "node:status" ], "value" : "0" } },
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "1" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "webmaster@realism.com",
          "subject" : "Node Unpublished",
          "message" : "[site:url]\r\nNode ID: [node:nid]\r\nNode Title: [node:title]",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Well...

Northern_Girl's picture

The answer to this question is simple : I just dont know how to do this! So for now, I'm guessing from what I can find (understand) over the web.

My users can decide for how long they want their ad to be online. For this, I have a rule/composent with a "data comparision": if field X=Y, than unpublish the node (I can export my rule if someone is interested).

But since I'm not that good with Rules, I figured that I needed one rule for each possibility (1 week, 2 weeks, 1 month).

I have set a fourth rule to send an email just to notify the user that his ad is now expired.

If the unpublish rules work great, I just cannot understand how to make Rules send the email. Or if I can make one rule for everything (unpublish after X days and send email according to the content of field "ad duration").

To send the email, it seems that you have to make a comparision on the "status" of the node (so Rules can see that the node is now unpublished). At the same time, I want rules to send the email once.

It drives me nuts because I just dont see where I did it wrong... Or, it is definitly more complecated than I thought!

NG

Try node expire module

pinueve's picture

This module does all you need, https://www.drupal.org/project/node_expire
May I suggest you also to look for answers like this one in http://drupal.stackexchange.com/

It works... finally!

Northern_Girl's picture

So I came up with a rule that will send an email when a node is unpublish. For now, the unpublishing process is handle by another rule (that is another story...)

Event :

After updating existing content of type Board

Conditions :

Data comparison > Parameter: Data to compare: [node:status], Data value: Unpublished

Actions

Send email > Parameter: To: [node:author:mail], Subject:...

I posted a few nodes on my test site to see if the emails were sent : it works!

Rules does not send more than one email, even if there are many unpublished (old) nodes.

And if a user modifies a node, Rules does not send no more than one email per unpublished node.

If somebody could check if this rule works just to be shure (it seems to easy to be true...!).

Thanks to all!

{ "rules_send_email_unpublish" : {
    "LABEL" : "Unpublish_email",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update--board" : { "bundle" : "board" } },
    "IF" : [ { "data_is" : { "data" : [ "node:status" ], "value" : "0" } } ],
    "DO" : [
      { "mail" : {
          "to" : "[node:author:mail]",
          "subject" : "Title",
          "message" : "Message",
          "language" : [ "" ]
        }
      }
    ]
  }
}

It seems that you have an

paean99's picture

It seems that you have an error on your rule. There should be two conditions:

  • one to check if the node (unchanged) before the update is published
  • one to check if the node after the update is unpublished

Something like this (taken from this issue https://groups.drupal.org/node/97454):

{ "rules_on_node_publish" : {
    "LABEL" : "On node publish",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "data_is" : { "data" : [ "node:status" ], "value" : "0" } },
      { "AND" : [] },
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "1" } }
    ],
    "DO" : []
  }
}

edit: Or see the above comment from realEuph (sorry for the repeated edits and for the spam from the email notification...)

Maybe...

Northern_Girl's picture

But I have tested my solution through Masquerade (different situations) and it... works.

I have seen these solutions with the "If/And"... it never worked. Maybe because I have 3 rules for unpublishing (one if the user chooses 1 one week; 2 weeks or 1 month) and one rule for sending the email.

NG

I post

realEuph's picture

Northern_Girl,

I posted a solution yesterday as a reply to one of your messages. (repeated below for convenience.) If you want to send email when a node is unpublished, you must insure that it was published prior to the latest action (node-unchanged:status == 1). You will need to logically combine that conditional with all others (or create separate rules) for each time interval.

Note that the email is an action ("DO") not a rule.

{ "rules_test_email_on_publish" : {
    "LABEL" : "TEST EMail on Publish",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "test" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "node:status" ], "value" : "0" } },
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "1" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "webmaster@realism.com",
          "subject" : "Node Unpublished",
          "message" : "[site:url]\r\nNode ID: [node:nid]\r\nNode Title: [node:title]",
          "language" : [ "" ]
        }
      }
    ]
  }
}

I am assuming that you are

paean99's picture

I am assuming that you are using rules scheduler and i am not sure how you did your testings or what where the results for you to say '...it never worked...'.
But when testing scheduler, one have to remember that it only runs on cron (if i am not wrong). So if your test doesn't give you any result, it might be because cron hasn't run yet.

On the other hand, if there are others rules interacting, although i don't see how, they could very well affect the behavior of the unpublished node.

With only one condition as in the rule that you have presented, the expected behavior is that the email will be sent when you update an unpublished node (like changing its title).

But it works...

Northern_Girl's picture

I was never able to make this work : https://groups.drupal.org/node/97454 or https://www.drupal.org/node/2325525.

My solution is a "2 part" solution where I have a rule for unpublishing and a 2nd rule for the email. And the tests I did so far have worked. Its all I can say... Or something is dead wrong with my D7 install...

One of the Rules for unpublishing; I have another rule unpublishing after 5 minutes (100% working: nodes are unpublished on cron):

{ "rules_unpublish_ad" : {
    "LABEL" : "unpublish_10_min",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : { "node_insert--board" : { "bundle" : "board" } },
    "IF" : [
      { "data_is" : { "data" : [ "node:field-limite-temps" ], "value" : "10 min" } } ----------> this is a text dropdown where the user can choose the duration of the ad <----------------------
    ],
    "DO" : [
      { "schedule" : {
          "component" : "rules_unpublish_board",
          "date" : "+10 minutes",  -----> For testing purposes <-------
          "identifier" : "[node:nid]",
          "param_unpublish_board" : [ "node" ]
        }
      }
    ]
  }
}

The rue for the email :

{ "rules_unpublish_email" : {
    "LABEL" : "unpublish_email",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update--board" : { "bundle" : "board" } },
    "IF" : [ { "data_is" : { "data" : [ "node:status" ], "value" : "0" } } ],
    "DO" : [
      { "mail" : {
          "to" : "[node:author:mail]",
          "subject" : "Expired",
          "message" : "Message",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Clarify Requirements

realEuph's picture

Northern_girl,

Please clarify what you are trying to do. The reaction rules that I and others have discussed deal with the 'Save' operation initiated by a user. (User unchecks 'Publish' box.) That is the only time when there is the saved and not-yet-saved version of the node available.

If you are unpublishing via cron, then you want to make sure the node is published and meets whatever other criteria is used. Then set the node's status to 0. When you set the node status to 0, you would also send an email.

Alternatively, rather than publishing and unpublishing the node, you might consider adding a flag field that indicates if the node is active. The node would not show up in any view if the field is not set. Unless you wrote some code, it would be available by NID.

Northern_girl you can add as

vaccinemedia's picture

Northern_girl you can add as many actions to a single as you wish. So I'm assuming you simply need to add the action to send the expiry email to the author onto the same rules as are making them become unpublished?

Well...

Northern_Girl's picture

For now, my solution seems to work. Everything else did not work.

Yes, I use Rules Scheduler and cron (Elysia).

If you have a working solution where the user can choose the duration of his ad (auto unpublish + email notification), just share it with the rest of the world.

If you have a working

paean99's picture

If you have a working solution where the user can choose the duration of his ad (auto unpublish + email notification), just share it with the rest of the world.

You could just use a date filed on the node. See this issue for more: Using Rules to publish/unpublish based on a Date field

Another solution would be to follow @pinueve's sugestion and use Node expire, in particular Node expire extras as it has an "Update expiry date" Rules. This two modules work in conjunction with rules.

You could also use a view with Views Bulk Operations to update the schedules of the nodes in bulk. Take a look at this video tut for some inspiration: How To Expire Nodes With Rules Scheduler.

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: