Save emails and attachments to a database (SQL Server and any ODBC database)

Add a row to SQL Server, MySQL, PostgreSQL, Oracle or any ODBC database for each email or attachment, with an ODBC data source and one rule step.

The Rule Editor with a Save to a database step: data source, table dbo.Invoices, an INSERT statement with fields, and the Test SQL and Create table buttons

In this post we save emails and attachments to a database server such as SQL Server. You set up an ODBC data source once, then add a Save to a database step to a rule. Each time the rule saves a file, a row goes into your table.

The program works with any database that has a 64-bit ODBC driver: SQL Server, MySQL, PostgreSQL, Oracle, Access and more. This post uses SQL Server, with notes for other databases. It is written for version 4.0.

An overview: emails to databases

Database steps are part of PRO Rules Server.

  • You don’t need to know SQL. Create table makes a table and writes the SQL for you.
  • If you do know SQL, you can change the statement to suit your own tables.
  • Put any field in the SQL: the sender, the subject, the date, where the file was saved, and values read from the email or its files, such as an invoice number.

A rule works in steps. First its conditions pick the emails or files. Then it can read values from them. Then it saves the files to a folder. Last, its steps run, and Save to a database is one of them.

It runs on your own system and talks to your database directly through ODBC. As a Windows service it runs around the clock, on a PC, a server or a cloud VM, even when nobody is signed in.

Step by step: emails to a database

1. Set up a data source

Open the Settings tab, go to Sources, choose Data Sources and click Add…. The Data source configuration window opens.

  1. Give the source a name in Name:, for example “Contoso Finance”.
  2. Pick the driver in Choose driver:. Version 4.0 is 64-bit, so it lists the 64-bit ODBC drivers installed on this computer. For SQL Server, pick a SQL Server driver.
  3. Type the Connection string:. For SQL Server with Windows sign-in it looks like this:
    Server=<computer name>\<SQL Server instance>;Database=<database name>;Trusted_Connection=yes;

For example:

Server=CONTOSO-SQL01\FINANCE;Database=ContosoInvoices;Trusted_Connection=yes;

Click Test connection. Once it says Connection succeeded., click OK. The data source is ready.

Settings, Sources, Data Sources with the Data source configuration window: name Contoso Finance, ODBC Driver 18 for SQL Server (64-bit ODBC), the Contoso connection string and Connection succeeded
A data source for the database steps: name, 64-bit ODBC driver and connection string, tested.

To connect another way, or to another database such as Oracle or MySQL, look up its ODBC connection string, for example on connectionstrings.com. Choose the ODBC string, not the other kinds.

2. Add a Save to a database step to a rule

Open the PRO Rules tab. Click New rule…, or select a rule and click Edit. The Rule Editor opens.

  1. Set the rule’s conditions under Which emails, for example From ‘acme.example’ and Attachment name ending in .pdf, and where the files are saved under Save, for example \\fileserver\Finance\Invoices.
  2. Click Add a step…, choose Data and fields, then Save to a database, and click Add. The step goes under Then.
  3. In Data source:, pick the source from step 1.
  4. In Table:, pick a table, or type the name of a new one, such as dbo.Invoices.
  5. For a new table, click Create table. It makes the table and fills in the SQL that adds a row to it. Careful: if a table with that name is already there, it is deleted with everything in it and made again. The program asks first.
  6. In Run:, choose For each saved file or Once per email.
  7. Click Test SQL. It runs the SQL with a sample email’s values and adds a row, so you can check the table.
  8. Click Save to save the rule.
The Rule Editor with a Save to a database step: data source, table dbo.Invoices, an INSERT statement with fields, and the Test SQL and Create table buttons
A Save to a database step: the data source, the table and the SQL with its fields.

To change the SQL, edit it in SQL: and click Fields… to insert fields. Each field, such as {FROM} or {SUBJECT}, goes into the SQL as a value. {TABLE_NAME} is the table you chose. Separate several statements with ;.

The step can sit after other steps. For example, convert a file to PDF first, then save its details to the database. See chained rules and actions.

Now click Connect and Download, or let the schedule run. The Downloads tab shows whether each step worked.

That’s it! The program now saves your emails and attachments to a database.

Important considerations

  • 64-bit ODBC driver: version 4.0 is 64-bit only, so it needs your database’s 64-bit ODBC driver. A data source set up for a 32-bit driver in version 3.x needs the 64-bit driver. A driver missing on this computer shows “(not installed on this computer)” next to its name.
  • Connection strings vary: each database has its own, and you may need to try a few. If your connection string names a driver, it must match the one you picked exactly, space for space.
  • Create table may not suit every database. If it fails, make the table in your database yourself, then write the SQL that adds a row to it.
  • The file itself in the table: Create table asks whether to also keep each whole file in a BLOB column. This is not recommended: the table grows quickly. The saved file’s path is usually enough: {SAVE_PATH} is the folder and {FILENAMEEXT} the file name.
  • Running as a service: the service uses the settings it got when it was installed. After you change a rule or a data source, click Reinstall… on Schedule › Service schedule.

More ways to use a database

  • Read the values first: the Parser reads invoice numbers, dates, totals and table lines out of emails and files. With PRO Rules Server it can send them straight to a database with its SQL output.
  • Load rows from Excel or CSV attachments into a table with the Store CSV / SQL step. See Read Excel and CSV attachments.
  • Look a value up, such as a customer code from the sender’s address, with Look up from a database, and use it in folder names, file names and later steps.
  • Watch a database table: each new row goes through your rules, as an email does. See database tables as a source.

Try it: download Mail Attachment Downloader and start with the free edition, or compare the PRO editions, each with a 30-day money-back guarantee.

Related

Questions or suggestions about this post? Contact us.