3

I would like to track IP address and User Agent when Rules sends out a email. How can I include the [current-user:ip-address] token and %server[HTTP_USER_AGENT] in „Send email“ rule?

Unfortunately Rules (7.x-2.10) ignores both tokens.

IP Address: [current-user:ip-address]
User-Agent: %server[HTTP_USER_AGENT]
  • I’ve tried to create a rules Replacement tokens, however I am not able to create a new entity with Data selector site:current-user:ip-address How can I create a Replacement tokens to show the current-user ip address? How can I create a Replacement tokens to show the current-user ip address?

1 Answer 1

2
+50

Option 1: Create new Rules variables

Have a look at this prototype of a custom rule (in Rules export format):

{ "rules_send_email_with_user_agent_and_ip_address" : {
    "LABEL" : "Send email with User Agent and IP Address",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules" ],
    "ON" : { "user_login" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "\u003C?php print ip_address(); ?\u003E" },
          "PROVIDE" : { "variable_added" : { "useripaddress" : "User IP address" } }
        }
      },
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : "\u003C?php print $_SERVER[\u0027HTTP_USER_AGENT\u0027]; ?\u003E"
          },
          "PROVIDE" : { "variable_added" : { "useragent" : "User Agent" } }
        }
      },
      { "drupal_message" : { "message" : "User Ip Address is [useripaddress:value] and User Agent is [useragent:value]" } },
      { "mail" : {
          "to" : [ "site:mail" ],
          "subject" : "User [account:name] performed a login from Ip Address [useripaddress:value]",
          "message" : "User [account:name] performed a login from Ip Address [useripaddress:value] (User Agent is [useragent:value]).",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Some details about this rule:

  • Rules Event: User has logged in (replace with whatever event fits for you).
  • Rules Conditions: none (add whatever conditions you want).
  • Rules Actions:
    • Add a variable, with label "User IP address", machine name useripaddress and with initial value the result of this PHP evaluation: <?php print ip_address(); ?>.
    • Add a variable, with label "User Agent", machine name useragent and with initial value the result of this PHP evaluation: <?php print $_SERVER['HTTP_USER_AGENT']; ?>.
    • Show a message on the site, with a message value like User Ip Address is [useripaddress:value] and User Agent is [useragent:value] (just for QA testing, remove this Rules Action after you're convinced it works OK).
    • Send email, with an eMail body like User [account:name] performed a login from Ip Address [useripaddress:value] (User Agent is [useragent:value])., and a similar eMail subject (replace with whatever eMail subject/body that fits for you).

If you have the Rules UI enabled (and the dangerous PHP filter ...) you should be able to import it in your own site also.

Attention: this rule requires the dangerous PHP filter to be enabled (which personally I try to avoid at any cost ...). But if anything else fails, this is "a" solution to get it to work. As an alternative, you could create a custom module in which you create your custom Rules Actions for creating such eMail, and in which you'd include some PHP code similar to the PHP code in the rules Actions shown in the above Rules Actions. Refer to my answer to "How to create custom rules action event conditions?" for more details about that. Or refer to this part of the Commerce Google Analytics module which adds 2 Rules Actions related to User Agent and User IP address.

Option 2: Use the Message module

If you're willing to also use the Message module (and have sufficient experience in using it ...), you could implement a rule like so:

  • Create an appropriate message entity, which refers to a "message type" in which you can refer to token [current-user:ip-address] (= The IP address of the current user). This token comes, out of the box, with the Message module.
  • Include some type of pointer/reference to the created message in your eMail, or use the Message Notify module to get the created message entity delivered as an eMail.
  • For your User-agent token their might be a similar token available already in the Message module also. Though so far I've not found / discovered that one. However, using the technique of "replacement tokens", you should be able to create such token (via Rules), and then include that in your Message type also. For an example to see those replacement tokens at work, refer to my answer to "Which user related token(s) can be used to create a Rule for setting up a message type?".

Refer to this question for a zillion of other samples/cases about the amazing Message module.

6
  • Once again thank you for pointing me in the right direction. I'll take a look at the message module...
    – user32010
    Commented Dec 30, 2017 at 20:37
  • I’ve followed along your blog and rules samples for Replacement tokens. However I am not able to create a new entity with Data selector site:current-user:ip-address as described. I’ve updated a screenshot to my original question. Anything I have missed here?
    – user32010
    Commented Jan 13, 2018 at 22:16
  • 1
    @howdytom 2nd attempt ... refer to my "Option 1" I just added as a possible alternative ... Commented Jan 23, 2018 at 13:00
  • Thank you so much for sharing and your time! This is exactly I was lookng for. I wish there was more a more secure and user-friendly solution. Anyway, perfect. Thx!
    – user32010
    Commented Jan 24, 2018 at 8:45
  • 1
    "merci" to you also @howdytom, for "the chamllenge", and the bounty. Moreover, your question gave me a new idea for an additional submodule of the Visitors module, i.e "visitor_rules" (to make these kinds of data available in Rules Actions/Conditions). PS: anything else I can do to further improve this answer, to consider it for marking it as "accepted" also? Commented Jan 24, 2018 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.