Any way to replace/remove sender IP?

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
Post Reply
omi555
New user
New user
Posts: 4
Joined: 2015-02-14 13:23

Any way to replace/remove sender IP?

Post by omi555 » 2015-02-14 13:48

Hello,

I have my mail server installed on local machine with everything works fine and smooth. My IP is clear from all the blacklists as well. Emails come and go perfectly without being landed in SPAM, which is the best part :)

I have a newsletter app running from a shared hosting with its IP address blacklisted. Emails from my newsletter app (interspire) goes to my mail server and then transported to the final recipient.

Now majority of the emails from my newsletter app are being marked as SPAM... because my shared-hosting server's ip is blacklisted ....

I need a way to replace ip of my hosting server with my mail server's ip. So all outgoing emails will be received as if they were sent directly from my mail server.

This is how header look like :

Received: from mydomain.com (myhostingcompany.com [50.31.160.55])
by my.mailserver.com with ESMTPA


I want a way to completely replace/change details about my hosting company because that is what's getting my emails into spam.


My mailserver has proper domain key, spf record to include ip address of my hosting server's ip as well as my mailserver's, rDNS / PTR all set.
I am using HmailServer 5.6-B2145

I am not a programmer so please help me in details if you know how to get this working.

[I do know that I can install my newsletter app on a local machine or on mailserver pc and get the results I wan, but that is not an option here with me, so I'm restricted to the scenario mentioned above]

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Any way to replace/remove sender IP?

Post by percepts » 2015-02-14 14:19


glenluo
Normal user
Normal user
Posts: 194
Joined: 2011-07-03 12:10

Re: Any way to replace/remove sender IP?

Post by glenluo » 2015-02-14 15:09

Use below code the replace it as local IP.

Sub OnAcceptMessage(oClient, oMessage)

If instr(oMessage.FromAddress,"@")>0 then
If instr(1,LCase(oMessage.HeaderValue("Received")),"50.31.160.55",1)>0 then
oMessage.HeaderValue("Received")=replace(oMessage.HeaderValue("Received"),"50.31.160.55","192.168.1.55")
oMessage.Save
End if
End if

End Sub

omi555
New user
New user
Posts: 4
Joined: 2015-02-14 13:23

Re: Any way to replace/remove sender IP?

Post by omi555 » 2015-02-16 12:45

Thanks glenluo. I tested the script today, but it doesn't seem to fix the issue. Here is what the header look like now:

Received: from mail.mymailserver.tk (mail.mymailserver.tk. [123.123.123.123]) <--------- This is what your code fixed
by mx.google.com with ESMTPS id [removed]
for <my@email.com>
(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
Mon, 16 Feb 2015 02:10:28 -0800 (PST)

Received: from mydomain.com (hostingserver-name.com [50.31.160.100]) by 123.123.123.123 <----- This is what I wanted to change


Any idea ? Yep its changing the first Received from entry, but the second one remains the same.

omi555
New user
New user
Posts: 4
Joined: 2015-02-14 13:23

Re: Any way to replace/remove sender IP?

Post by omi555 » 2015-02-16 14:15

Just adding a note, while searching I found the following script to remove all headers.. and just confirmed that this works pretty good. It removed header all the information about my hosting server ...and recieving end only sees information about my mailserver.

EDIT} It also removes the information from all incoming emails too :( any suggestions?

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
   Dim oHeaders
   set oHeaders = oMessage.Headers

   EventLog.Write("Header count: " + CStr(oHeaders.Count))

   Dim i
   For i = oHeaders.Count -1 To 0 Step -1
   
      dim oHeader
      Set oHeader =oHeaders.Item(i)
   
      EventLog.Write("Header found: " + oHeader.Name)
      
      ' Check i this is a header which we should delete.
      if LCase(oHeader.Name) = "received" Then
         oHeader.Delete
         EventLog.Write("Header deleted.")
      End If
      
   Next
   
   ' Save all the changes...
   oMessage.Save
   
End Sub

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Any way to replace/remove sender IP?

Post by percepts » 2015-02-16 14:34

I was obviously wasting my time providing you with it then.

omi555
New user
New user
Posts: 4
Joined: 2015-02-14 13:23

Re: Any way to replace/remove sender IP?

Post by omi555 » 2015-02-16 14:43

percepts wrote:
I was obviously wasting my time providing you with it then.

So sorry man. I totally overlooked your reply.. :( my bad

But this script removes header from incoming emails too... i just wanted to remove it from just one host/ip... not from incoming emails as well.. any work around for this ?

User avatar
SorenR
Senior user
Senior user
Posts: 4186
Joined: 2006-08-21 15:38
Location: Denmark

Re: Any way to replace/remove sender IP?

Post by SorenR » 2015-02-16 17:48

omi555 wrote:Thanks glenluo. I tested the script today, but it doesn't seem to fix the issue. Here is what the header look like now:

Received: from mail.mymailserver.tk (mail.mymailserver.tk. [123.123.123.123]) <--------- This is what your code fixed
by mx.google.com with ESMTPS id [removed]
for <my@email.com>
(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
Mon, 16 Feb 2015 02:10:28 -0800 (PST)

Received: from mydomain.com (hostingserver-name.com [50.31.160.100]) by 123.123.123.123 <----- This is what I wanted to change


Any idea ? Yep its changing the first Received from entry, but the second one remains the same.
If you look carefully in the headers you will find multiple "Received:" headers - one for each server involved, using the COM API will only give you the last one.

You need a different approach...

This may spark some ideas ;-)

Code: Select all

'   Sub LogAllHeaders(oMessage)
'      EventLog.Write("LogAllHeaders(oMessage) ****************************************************")
'      EventLog.Write("MAIL FROM: " & oMessage.FromAddress)
'      Dim i
'      For i = 0 to oMessage.Headers.Count-1
'         EventLog.Write(oMessage.Headers(i).Name & ": " & oMessage.Headers(i).Value)
'      Next
'      EventLog.Write("****************************************************************************")
'   End Sub
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Any way to replace/remove sender IP?

Post by percepts » 2015-02-16 17:52

For i = 0 to oMessage.Headers.Count-1 won't work if you are doing deletes.

You must use following if you are deleting from a collection of rows as in the script from the documentation posted above.

For i = oHeaders.Count -1 To 0 Step -1

User avatar
SorenR
Senior user
Senior user
Posts: 4186
Joined: 2006-08-21 15:38
Location: Denmark

Re: Any way to replace/remove sender IP?

Post by SorenR » 2015-02-16 18:09

percepts wrote:For i = 0 to oMessage.Headers.Count-1 won't work if you are doing deletes.

You must use following if you are deleting from a collection of rows as in the script from the documentation posted above.

For i = oHeaders.Count -1 To 0 Step -1
That's a whole different story :wink:

If you delete headers you may break something else, so this is a suggestion to CHANGE the header value only. OTH it will probably break DKIM regardless.

OT.. I probably need to check through my For loops, just in case :oops:
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Any way to replace/remove sender IP?

Post by percepts » 2015-02-16 19:35

omi555 wrote:It also removes the information from all incoming emails too :( any suggestions?
You modify the code so as it loops through the headers you check each received header to see if it contains the server name or IP that you want removed and only delete if it does. You can change the IP for servername if that is always the same.

So you have something like:

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
 Dim oHeaders
 set oHeaders = oMessage.Headers

 EventLog.Write("Header count: " + CStr(oHeaders.Count))

 Dim i, oHeader
 For i = oHeaders.Count -1 To 0 Step -1
  Set oHeader =oHeaders.Item(i)
  EventLog.Write("Header found: " + oHeader.Name)
      
' Check if this is a header which we should delete.
  if LCase(oHeader.Name) = "received" Then
   If instr(1,LCase(oHeader.Value,"50.31.160.55",1) > 0 ) then
    oHeader.Delete
    EventLog.Write("Header deleted.")
   End If 
  End If
 Next
   
' Save all the changes...
 oMessage.Save
End Sub

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Any way to replace/remove sender IP?

Post by percepts » 2015-02-16 20:19

SorenR wrote:OTH it will probably break DKIM regardless.
Only if you're doing DKIM checks which I don't and if you're forwarding it'll probably break DKIM at recipient even if you don't modify received header. So to be on the safe side, I would take out DKIM-Signature header too.

kdiamond
Normal user
Normal user
Posts: 94
Joined: 2013-03-20 10:51

Re: Any way to replace/remove sender IP?

Post by kdiamond » 2015-04-05 13:57

percepts wrote:
I was obviously wasting my time providing you with it then.
That's it. this was bugging me for a long time!

Thank you !!!

lifeofguenter
New user
New user
Posts: 10
Joined: 2019-06-28 09:56

Re: Any way to replace/remove sender IP?

Post by lifeofguenter » 2019-06-28 10:08

I ended up modifying the official version a bit: https://gist.github.com/lifeofguenter/0 ... 113cf1e1d8
  • it only deletes the "received" header on email submissions (e.g. outgoing / authenticated emails)
  • no extra configuration is needed, so it works with whatever IP-range or hostname you are using
It basically just checks if "oClient.Username" is set. Just in case if anyone coming from google is interested.

Post Reply