Dynamic SMTP Relay

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
prashanthRaju
New user
New user
Posts: 17
Joined: 2012-04-03 15:32

Dynamic SMTP Relay

Post by prashanthRaju » 2019-03-11 10:17

Hello,
We use hmailserver to fetch mail from our mail servers to local accounts (1 domain hosted at GoDaddy and another with Gmail). While we do have hmailserver configured to send outgoing mail, our ISP sometimes gets blacklisted on some obscure MX blacklist and the whole range is affected. In such cases, we have to resort to using a relay account. The problem is GoDaddy only allows 250 relays/account/day and someone has to keep changing the relay user name/password through the day (!). Gmail is another beast as it replaces the sender with the relay user account so recipients get confused as to who sent the mail.
I would like to have a list of servers/usernames/passwords in a table and make hmailserver use it decide which relay to use, depending on the sender address. It would be on a per-account basis as all our local users have external accounts at the email providers.
One crazy solution would be to define one route for each user and use rules. That seems impractical.
I did go through the COM API and saw the Route object. Can this be dynamically altered or set in OnAcceptMessage?

I'm adequate at scripting though not an expert. Any guidance would be much appreciated as I could not find a relevant match for this topic on this forum.
Thank you.

User avatar
mattg
Moderator
Moderator
Posts: 19903
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Dynamic SMTP Relay

Post by mattg » 2019-03-11 11:13

yes, it could be set in OnAcceptMessage

set a rule and a route, then set the rule to use the route
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

prashanthRaju
New user
New user
Posts: 17
Joined: 2012-04-03 15:32

Re: Dynamic SMTP Relay

Post by prashanthRaju » 2019-03-11 11:32

Thank you for the quick response Matt.
So I was thinking I'd just have one domain-level route (which I could easily disable/enable as per the situation).
Going through the documentation: https://www.hmailserver.com/documentati ... ject_route
I would have to do the following:
In the OnAcceptMessage event, grab the Routes collection, then access the domain specific Route object using ItemByName, then change the SMTP server username property and set the password using SetRelayerAuthPassword.
That would do? Do you think you could point me to any scripting examples that manipulate the Route object for reference? I couldn't find any.
Thanks!

User avatar
mattg
Moderator
Moderator
Posts: 19903
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Dynamic SMTP Relay

Post by mattg » 2019-03-11 13:42

I don't recall any examples (This is a fairly unusual request) :D

Have a go, and post back if you get stuck
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

prashanthRaju
New user
New user
Posts: 17
Joined: 2012-04-03 15:32

Re: Dynamic SMTP Relay

Post by prashanthRaju » 2019-03-11 21:01

The test code works! I need to change the global SMTP relay settings for each message to ensure that the relay account of the sender is used for sending the message.
I understand that I should be doing this not in OnAcceptMessage because outgoing messages would be queued, but which of these two delivery events - OnDeliveryStart or OnDeliveryMessage should I be targeting? Any idea what's the difference between them? The documentation isn't too clear on this.
Thanks!

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
On Error Resume Next
if oClient.UserName <> "" THEN
Dim setting_username, setting_password
Dim obApp
Dim userpassword
Dim oSettings
Set obApp = CreateObject("hMailServer.Application")
Call obApp.Authenticate(setting_username, setting_password)


Set oSettings=  obApp.Settings
oSettings.SMTPRelayer = "relayserver.net"
oSettings.SMTPRelayerPort=465
oSettings.SMTPRelayerRequiresAuthentication=true
oSettings.SMTPRelayerUsername = oMessage.fromAddress
oSettings.SMTPRelayerUseSSL=true
userpassword = "password" 'get the password from your db, or wherever
oSettings.SetSMTPRelayerPassword("password")

End If

End Sub

User avatar
mattg
Moderator
Moderator
Posts: 19903
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Dynamic SMTP Relay

Post by mattg » 2019-03-12 01:40

prashanthRaju wrote:
2019-03-11 21:01
...but which of these two delivery events - OnDeliveryStart or OnDeliveryMessage should I be targeting? Any idea what's the difference between them? The documentation isn't too clear on this.
The order in which they are triggered

https://www.hmailserver.com/documentati ... ce_scripts

OnAcceptMessage is probably the correct one, I'd think, as it happens first
OnDeliverMessage is after global rules so that is too late
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Post Reply