How to include IP address Token with Rules? 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?
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: &lt;?php print ip_address(); ?&gt;.
Add a variable, with label "User Agent", machine name useragent and with initial value the result of this PHP evaluation: &lt;?php print $_SERVER['HTTP_USER_AGENT']; ?&gt;.
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.