Passing message content to a database

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
RogerK
New user
New user
Posts: 5
Joined: 2020-04-17 08:21

Passing message content to a database

Post by RogerK » 2020-04-17 08:37

I am looking for a way of passing incoming message content to a mySQL database
and I am wondering if hMailserver can do this.

The requirement is for one email account only. A script will extract contents of messages
and populate fields in the database. Preferred language for script will be PHP.

At this stage I am just trying to establish how easy this will be to achieve with hMailserver.

Thank you,

Roger K

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

Re: Passing message content to a database

Post by SorenR » 2020-04-17 09:29

RogerK wrote:
2020-04-17 08:37
I am looking for a way of passing incoming message content to a mySQL database
and I am wondering if hMailserver can do this.

The requirement is for one email account only. A script will extract contents of messages
and populate fields in the database. Preferred language for script will be PHP.

At this stage I am just trying to establish how easy this will be to achieve with hMailserver.

Thank you,

Roger K
In hMailServer...

Create account,
Edit account and create RULE to Run Function "DumpToMySQLWithPHP" on receipt of new mail

In eventhandlers.vbs add something like...

Code: Select all

Function DumpToMySQLWithPHP(oMessage)
    Const Verbose = 0
    Dim oCMD : Set oCMD = CreateObject("WScript.Shell")
    oCMD.Run "<path-to>\PHP.EXE <path-to>\DumpToMySQLWithPHP.php " & oMessage.filename, Verbose, True
End Function
oMessage.Filename = <path-to>\<filename>.EML and it lists the location and filename of the raw email.

NOW... IF YOU had taken 5 minutes to read the documentation I could have saved 10 minutes doing YOUR job!
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

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

Re: Passing message content to a database

Post by palinka » 2020-04-17 14:38

To be fair, I could not find an example of external scripting as such in the documentation.

Also, to be very, very fair, Soren is rarely a grumpy dude and is one of the best and most prolific contributors here. :mrgreen:

However, a simple search would have brought up lots of results. Taking one of Soren's examples: http://hmailserver.com/forum/viewtopic.php?f=21&t=33566

Its probably a lot easier to do the whole thing in vbs since you have to use it anyway. hMailServer COM has a built in database execution object. So the easiest way would be to create a table in the hMailServer database and use the COM database object. Then you can retrieve the data using php or whatever you want.

From Soren's example, I put this together:

Code: Select all

Private Const ADMIN = "Administrator"
Private Const PASSWORD = "########"

Function GetDatabaseObject()
   Dim oApp : Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate(ADMIN, PASSWORD)
   Set GetDatabaseObject = oApp.Database
End Function

Sub DumpToMySQLWithPHP(oClient, oMessage)
   Dim strSQL, oDB : Set oDB = GetDatabaseObject
   strSQL = "INSERT INTO your_message_table (timestamp,ipaddress,from,message) VALUES (NOW(),'" & oClient.IPAddress & "','" & oMessage.FromAddress & "','" & oMessage.HTMLBody & "');"
   Call oDB.ExecuteSQL(strSQL)
End Sub

You'll need to create the table first. In the example above there are 4 columns. Change that to whatever suits your needs. This is still called by rule.

RogerK
New user
New user
Posts: 5
Joined: 2020-04-17 08:21

Re: Passing message content to a database

Post by RogerK » 2020-04-19 00:24

Many thanks, this is just what I need. I have not used VBS for a long time, but I can see that it might be easier than PHP

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

Re: Passing message content to a database

Post by mattg » 2020-04-19 17:56

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