Retrieving sender IP

This forum is for discussing Mailtraq's Scripting features. Get help with Mailtraq scripts, templates and external ActiveX scripting.

Retrieving sender IP

Postby prodata » Mon Sep 13th, 2010 10:51am

Is there any scripting function that will return the IP address of the server that sent an e-mail? I know in the headers that it's usually like Received: from abc123[at]xyz.com ([11.22.33.44]) so I could probably parse it out using some string functions, but I didn't know if there was a straightforward function built into Mailtraq that would pull the sender IP and save me a little trouble?

Thanks.
prodata
 
Posts: 27
Joined: Sun Apr 13th, 2008 12:14pm

Re: Retrieving sender IP

Postby Martin Clayton » Mon Sep 13th, 2010 11:51am

Assuming , a regexp literal like this might suit ...
Code: Select all
var rec = Request.GetHeader("Received");
var ip = rec.replace(/^.*?\(.*?\[((\d{1,3}\.){3}\d{1,3})\]\).*/, "$1");

if (ip === rec) // extraction failed


(I'm not aware of an equivalent native property or method for a MailMessage object).
Cheers, Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

Re: Retrieving sender IP

Postby jimhill » Thu Sep 16th, 2010 9:00am

[quote=prodata]Is there any scripting function that will return the IP address of the server that sent an e-mail?[/quote]

This is the simplest way to do it, I think.

Code: Select all
If(Length(Header("Received")) > 0,
    source_ip := WildcardMatchReplace(Header("Received"), "*[*]*", "$2"),
    source_ip := "a.b.c.d"
)


You can then string test the output against "a.b.c.d" to ensure that you really do have the source ip of the sending mail client.
(note: clients send mail, servers receive mail)
jimhill
Expert User
 
Posts: 337
Joined: Sun Dec 19th, 2004 9:59pm
Location: UK

Re: Retrieving sender IP

Postby Martin Clayton » Thu Sep 16th, 2010 10:23am

Ah, yes, WildcardMatchReplace() looks far less evil but I'm firmly in the javascript way now. If anyone's interested, I'll post some ipv4 address functions to calculate network address, host_bits, host_min, host_max, etc from address/subnet strings (covers lan definition wildcards and cidr notation). They are work-in-progress as I'm converting to object notation but it shouldn't take long to tidy up.
Cheers, Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK


Return to Mailtraq Scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron