Reply Email Catch and Send

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
regsysinc
Normal user
Normal user
Posts: 35
Joined: 2015-08-24 14:26

Reply Email Catch and Send

Post by regsysinc » 2020-02-24 15:39

I was wondering if there was a way to read an email that came back to a certain email account and then was send back out to a "reply address" and then send it back out to the "reply address". This would let the end-users "email" each other using our hMailServer.

Ok, let me show you the scenario, so it makes sense what we are after.

We use hMailServer as a backend to an online application, where members receive notifications emails, personal emails and such from other members and admins. They DO NOT have access to anything with hMailServer or external email accounts, they only see members names, check them, write up the email and then click a button to send. The system has the "sent to" member email address from inside our database from which they created the account. So..

Admin Adam sends and email from a page in our application to 3 members (he doesnt know their emails). hMailServer sends them out once we find their primary email addresses. We store this information in our database as an email job, so emails, ID's etc are stored. This works great right now. However, one of the members "replies" to the email, which he doesnt know the other members on the chain (BCC) only the sender which is "member@regsysinc.com" - our email address and domain. It comes back to the hMailServer and sends out an email to the member saying "Oops, this email is not monitored..blah blah blah".

I would like to look up the email job, find the sender and send just him the response from the member. We have all the emails in a table in the database and i think with scripting this would be possible. We would have to hide "ids" maybe in the email to use as our lookup.

I searched the forums, but couldnt find much, probably because my search was too broad. Please let me know if anyone has any ideas or examples of this and/or if this is possible. I would like the hMailSystem work with our database and keep a chain of these emails back and forth, so the members can communicate back to the email that was sent from the system, without knowing anyone's email address.

Thanks a bunch!

palinka
Senior user
Senior user
Posts: 1923
Joined: 2017-09-12 17:57

Re: Reply Email Catch and Send

Post by palinka » 2020-02-24 17:20

Your sequence of operation is a little confusing to me, but generally speaking, adding a header probably won't work because the recipient's mail server will overwrite it when replying, and replace it with their own headers. Maybe you should hide it in plain site.

Just stick the ID in the body of the email. Then search the body text of the reply for the identifier. Or create a random character string to identify ID if you want it to look "more secret".

regsysinc
Normal user
Normal user
Posts: 35
Joined: 2015-08-24 14:26

Re: Reply Email Catch and Send

Post by regsysinc » 2020-02-24 18:19

Thanks for the reply palinka!

Yes, sorry i dont think the request came out right, however you did pick up what i was putting down :wink:

So an email (member@regsysinc.com) comes out of our application using hMailServer as our delivery tool and is sent to undisclosed emails (BCC) from another user inside the system. The undisclosed recipients could reply to the email from (member@regsysinc.com) which is bounced back because its not a monitored email. In the body of the email, visible to the recipients is the email of the user whom sent it as a "reply email", for them to use instead of hitting reply to member@. However, tons of people just hit reply and respond to the email, which is normal. So, we would like to capture that at the member@ email and sent to the original sender of the email.

Current scenerio:
AdminAdam creates email-->sends using-->member@ -->sends-->userJoe@yahoo.com; userJane@hotmail.com; userJack@gamil.com
userJane@-->REPLIES-->member@
member@-->BOUNCE-->userJane@ "this email is not monitored"

What I need:
AdminAdam creates email-->sends using-->member@ -->sends-->userJoe@yahoo.com; userJane@hotmail.com; userJack@gamil.com
userJane@-->REPLIES-->member@
hMail receive script-->to: member@ -->search ID --> lookup AdminAdam email-->userAdam@aol.com
member@-->REPLY-->userAdam@ "This is from Jane, thank you for the email!"

Maybe that helped explain it better, but i do think you hit it on the head already with hiding the ID in the email and finding it on the back-end script if the recipient replies to the email.

So, my next question is where and how do we get that capture?

Thanks for the guidance!

palinka
Senior user
Senior user
Posts: 1923
Joined: 2017-09-12 17:57

Re: Reply Email Catch and Send

Post by palinka » 2020-02-25 02:55

Since your app is sending the message, you can deal with inserting the ID into the email body.

I would handle it like this:

Check every message for the ID. You should preface the ID with something unique, easy to find and won't get confused with any regular conversation. "Acme Inc Message ID : <msgIDstring>"

If found, query your database for the email to send the new message to, then send the notification message.

Not tested...

Code: Select all

Function oLookup(strRegEx, strMatch, bGlobal)
   If strRegEx = "" Then strRegEx = StrReverse(strMatch)
   With CreateObject("VBScript.RegExp")
      .Pattern = strRegEx
      .Global = bGlobal
      .MultiLine = True
      .IgnoreCase = True
      Set oLookup = .Execute(strMatch)
   End With
End Function

Sub userIDLookup(ByVal s_msgID, ByRef Originating_Sender)
    Dim oRecord, oConn : Set oConn = CreateObject("ADODB.Connection")
    oConn.Open "Driver={MariaDB ODBC 3.0 Driver}; Server=yourserver; Database=yourdatabase; User=yourusername; Password=yourpassword;"

    If oConn.State <> 1 Then
		EventLog.Write( "Sub userIDLookup - ERROR: Could not connect to database" )
        Originating_Sender = "ERROR"
        Exit Sub
    End If

    Originating_Sender = "ERROR"

    Set oRecord = oConn.Execute("SELECT userEmail FROM yourtable WHERE s_msgID = " & s_msgID )
    Do Until oRecord.EOF
        Originating_Sender = oRecord("userEmail")
        oRecord.MoveNext
    Loop
    oConn.Close
    Set oRecord = Nothing
End Sub

Sub OnAcceptMessage(oClient, oMessage)

	If oMessage.Body = Empty Then
		MessageBody = oMessage.HTMLBody
	Else
		MessageBody = oMessage.Body
	End If

	strRegEx = "Acme\sInc\sMessage\sID\s\:\s\<[0-9a-z]{6}\>"  '<<----- ASSUMES 6 CHAR STRING
	Set Matches = oLookup(strRegEx, MessageBody, False)
	For Each Match In Matches
		a = Split( Match Value, ":" )
		strID = Replace( Replace( Trim( CStr( IDStr(a) ) ), "<" ,"" ), ">", "" )
	Next

	Call userIDLookup(strID, Originating_Sender)
	OriginatingAddress = Originating_Sender
	Reply_Msg_From = oMessage.From
	
	With CreateObject("hMailServer.Message")
		.From = "notification@yourdomain.com <notification@yourdomain.com>"
		.FromAddress = "notification@yourdomain.com"
		.AddRecipient OriginatingAddress
		.HeaderValue("To") = Chr(34) & OriginatingAddress & Chr(34) & " <" & OriginatingAddress & ">"
		.Subject = "Notification: Message Received from " & Reply_Msg_From
		.Body = "You received an accidental reply from " & Reply_Msg_From & vbCrLf & vbCrLf & MessageBody
		.Save
	End With
	
End Sub

tunis
Normal user
Normal user
Posts: 246
Joined: 2015-01-05 20:22
Location: Sweden

Re: Reply Email Catch and Send

Post by tunis » 2020-02-25 10:24

Couldn't you just add reply-to: sender in the email. Then when you hit reply it send to sender and not to member.
HMS 5.6.8 B2494.25 on Windows Server 2019 Core VM.
HMS 5.6.8 B2494.24 on Windows Server 2016 Core VM.
HMS 5.6.7 B2425.16 on Windows Server 2012 R2 Core VM.

palinka
Senior user
Senior user
Posts: 1923
Joined: 2017-09-12 17:57

Re: Reply Email Catch and Send

Post by palinka » 2020-02-25 13:31

tunis wrote:
2020-02-25 10:24
Couldn't you just add reply-to: sender in the email. Then when you hit reply it send to sender and not to member.
I thought the originating sender's address was meant to be a secret.

However, that just gave me another idea.

Same concept as above except the email address itself is a random string that changes on every outgoing email. Then the originating sender's email address is obfuscated. The reply-to is the random string and the random string is also in the bottom of the email body.

Catch all account picks up the email, figures out who the recipients are and forwards it to the actual recipient. Totally anonymous and the "alias" is different for every single message.

regsysinc
Normal user
Normal user
Posts: 35
Joined: 2015-08-24 14:26

Re: Reply Email Catch and Send

Post by regsysinc » 2020-02-25 14:39

Sorry - stepped away putting out a few fired.

Really appreciate the great ideas and thoughts! One thing we don't need to worry about is the other BCCs. Its the admin sending it out from inside the system, so one or many BCCs can reply and the hMailServer will search for something (ID - as a BigInt {long}) hidden in the email bod and find the corresponding email address of that admin only. It will then send it back and and so on. All emails are kept secret, so each time it sends the email out from "member@regsysinc.com".

That code you provided will get me started, again, so appreciative. Where do we capture the "member@" reply? We don't want to check everything that hMail received but only the incoming to "member@". Would that be in the "OnAcceptMessage" function?

We have a JobEmail table which holds the parent email record - the contents, subject, type (our internal use). We then have the child table which holds the email addresses that it went to the status, some internal ID's, etc. that links back to the parent, normal stuff

So the ID would be the unique from Jobs_tbl.JobEmailId in the database. Easy lookup, we need to add the senders primary address to it, we currently don't care what it is in the system, because again, it comes from "member@" email service we provide.

palinka
Senior user
Senior user
Posts: 1923
Joined: 2017-09-12 17:57

Re: Reply Email Catch and Send

Post by palinka » 2020-02-25 16:12

regsysinc wrote:
2020-02-25 14:39
We don't want to check everything that hMail received but only the incoming to "member@".
Try this then.

Code: Select all

Function oLookup(strRegEx, strMatch, bGlobal)
   If strRegEx = "" Then strRegEx = StrReverse(strMatch)
   With CreateObject("VBScript.RegExp")
      .Pattern = strRegEx
      .Global = bGlobal
      .MultiLine = True
      .IgnoreCase = True
      Set oLookup = .Execute(strMatch)
   End With
End Function

Sub userIDLookup(m_body, m_FromAddress)
	Dim strRegEx, Match, Matches, a, strID, Originating_Sender

	strRegEx = "Acme\sInc\sMessage\sID\s\:\s\<[0-9a-z]{6}\>"  '<<----- ASSUMES 6 CHAR STRING
	Set Matches = oLookup(strRegEx, m_body, False)
	For Each Match In Matches
		a = Split( Match Value, ":" )
		strID = Replace( Replace( Trim( CStr( IDStr(a) ) ), "<" ,"" ), ">", "" )
	Next

    Dim oRecord, oConn : Set oConn = CreateObject("ADODB.Connection")
    oConn.Open "Driver={MariaDB ODBC 3.0 Driver}; Server=yourserver; Database=yourdatabase; User=yourusername; Password=yourpassword;"

    If oConn.State <> 1 Then
		EventLog.Write( "Sub userIDLookup - ERROR: Could not connect to database" )
        Originating_Sender = "ERROR"
        Exit Sub
    End If

    Originating_Sender = "ERROR"

    Set oRecord = oConn.Execute("SELECT userEmail FROM yourtable WHERE msgID = " & strID )
    Do Until oRecord.EOF
        Originating_Sender = oRecord("userEmail")
        oRecord.MoveNext
    Loop
    oConn.Close
    Set oRecord = Nothing
	
	With CreateObject("hMailServer.Message")
		.From = "notification@yourdomain.com <notification@yourdomain.com>"
		.FromAddress = "notification@yourdomain.com"
		.AddRecipient Originating_Sender
		.HeaderValue("To") = Chr(34) & Originating_Sender & Chr(34) & " <" & Originating_Sender & ">"
		.Subject = "Notification: Message Received from " & m_FromAddress
		.Body = "You received an accidental reply from " & m_FromAddress & vbCrLf & vbCrLf & m_body
		.Save
	End With

End Sub

Sub OnAcceptMessage(oClient, oMessage)

	If oMessage.Recipients(0).OriginalAddress = "member@yourdomain.com"
		If oMessage.Body = Empty Then
			Call userIDLookup(oMessage.HTMLBody, oMessage.From)
		Else
			Call userIDLookup(oMessage.Body, oMessage.From)
		End If
	End If

End Sub
I don't think userIDLookup can be called from a rule because I don't think there's a way to pass the message body. I could be wrong about that. Would not be the first time....

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

Re: Reply Email Catch and Send

Post by mattg » 2020-02-25 23:53

tunis wrote:
2020-02-25 10:24
Couldn't you just add reply-to: sender in the email. Then when you hit reply it send to sender and not to member.
I'm with Tunis

Can't the reply-to simply be set as the admin who is to get the replies. doesn't have to be the sender
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