2

I want to get the Email address of last comment's author. Actually, I want to send email by creating a rule when new comment posted and this email will go to the user who commented on node before this new comment.

  • In other words, you're probably trying to say: send an e-mail notification when someone replies to your comment. – No Sssweat May 23 '16 at 7:35
  • when someone reply or do an other comment. – Iqbal May 23 '16 at 11:29
3

There are two situations and they might be a bit hard to combine. Drupal has a concept of threading comments, where the last comment is not necessarily the last in line. Like this:

- Comment 1
  - Comment 3 (reply to comment 1)
- Comment 2

Who should get the mail? The poster of comment 2 (the last comment author), or user 1? Or both? Also note that you can disable threading comments on the content type settings page.

In order to send a mail to the author of Comment 1, you can create a Rule on After Saving New Comments. Use the Send mail action and for the to: field you can use the token [comment:parent:author:mail]. (The mail of the author of the comment above this one.)

If you want to send a mail to the author of comment 2, your rule gets a bit more complicated. The initial action need to be Before Saving New Comments (so last comment is actually the last comment before yours). For the first action choose Add a variable, the type is List of comments and you choose the value to be comment:node:comments. This variable holds all comments on the node. Now you need to find a way to keep removing items from this list and send an email to the last one.

Below is based on experience and not tested, but I guess you can add a loop and loop over the items in your newly created variable. Then, in the loop Add a variable to contain a comment entity and set it with the current item in the loop. This will redefine this variable for every comment in the list and in the end this variable will hold the last comment. Then below (not in) the loop, add your mail action and use the author of your newly created variable.

If you want to send a mail to all commenters, place the mail action within the loop (you can omit the comment variable in this case).

Update

This can also cause the author of comment 3 to get a mail triggered by comment 4:

- Comment 1
  - Comment 3 (reply to comment 1)
- Comment 2
- Comment 4

If you want to have more control, you'll need to create a Rules Component and call that component from the loop. Within the component you can add conditions to check if the comment has parents and skip the variable assignment based on that.

  • add your mail action and use the author of your newly created variable. How to get author of newly created variable? – Iqbal May 23 '16 at 13:57
  • 1
    Since the variable should hold the last comment, you should be able to use a token like my_variable:author:mail. – Neograph734 May 23 '16 at 14:00
  • i could not succeeded to send email. the variable does not return any email address – Iqbal May 23 '16 at 14:39
  • Then rework it a bit. Make the variable in the loop of type text, store the current list-item author mail value in it and use the variable directly in the to field. – Neograph734 May 23 '16 at 14:41

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.