Save emails and attachments to a database (SQL Server)
In this post, we will explore saving emails and attachments to a database server like SQL Server by setting up an ODBC data source and using a post download Action to save contents of the email and/or attachments to a database.
The program supports and works with any compatible ODBC data source such as MySQL, Oracle, PostgreSQL and more. This post only explores setting up to save the data in SQL Server but also has information on how to tweak it for other database types.
An overview — emails to databases
The feature of saving emails to databases is just one of the features available with the Mail Attachment Downloader PRO Server version.
Each rule can be setup to save emails to databases using either generated SQL (to one or more tables) or a custom SQL.
You do not need to know SQL to save emails or attachments to a database. And if you do know SQL, it offers the flexibility and customizability to modify the SQL statements to suit your needs.
You can even include extracted data from emails or attachments using custom variables in your SQL statement and save them to a database!
Each rule has several phases — first, search and filter through emails or attachments, then extract data from emails or attachments, saving email bodies or attachments to a customizable file or folder format if needed and lastly post-process the emails or attachments by saving them to a database of your choice.
Used by many Fortune 500 companies today to save emails and attachments to databases. Runs 24×7 in the cloud or on-premise as a Windows Service.
You get a versatile tool that provides flexibility in storing emails to databases in just the way you want to.
Step-by-step tutorial — emails to databases
Setting up a datasource
- Let’s start off by adding a new data source — click on the Database tab -> Add ODBC source button
- Provide a name for the source first. Then pick either the 32-bit ODBC driver or 62-bit ODBC driver (depending on whether you installed the 32-bit or 64-bit version of Mail Attachment Downloader PRO Client or PRO Server).In our case, the driver “SQL Server” from the drop down indicates the 32-bit driver. Then enter the Connection string that specifies how to connect to the server:
Server=<computer name>\<SQL Server name>;Database=<DB name>;Trusted_Connection=yes;
The above connection string is specific to SQL Server that is using a trusted connection. To connect in any other way to SQL Server OR to another database like Oracle, you can find ODBC connection strings here (make sure you select ODBC connection string and not any other on the site).
An example of these filled in is shown below:
Server=TESTPC\GEARMAGE;Database=TestDB;Trusted_Connection=yes;
- Click Test Connection button to make sure this succeeds. Then click OK.
- At this point a data source has been setup.
Rule configuration to save the email to a database
- Then create a new rule or edit an existing rule from the Global filters tab.
- Under Actions after save click Add action and then choose Save to database. This will add an action under After saving file in the list to the left.
NOTE: The screenshot below shows other actions as well, if you have other actions you can chain or nest the save to database action after those actions have taken place as shown below. - Pick the Source as the one you provided in Step-2.
- Specify a new table name (or an existing one) and then click “Create Table” if this is a new table and follow the prompts. At this point a new table schema would be created and the SQL would be automatically filled in based on the questions you answered.
- Click “Test SQL” to test connectivity and table row creation, this will add a new row to the table to make sure everything is setup correctly.
- Click the Save button to save the new rule with the Save to DB action.
- Now you can click the Connect And Download button to test out the ability to save to DB.
That’s it!
You have setup the program to automatically save emails and attachments to a database.
IMPORTANT CONSIDERATIONS
- Make sure you select either the 32-bit ODBC driver or the 64-bit ODBC driver in the dropdown depending on which installation of Mail Attachment Downloader PRO Client or PRO Server you installed (license email has links to both versions of the install). This is supported in all Windows versions including Windows 10. In some cases default installations may only install the 64-bit driver and you may need to manually download and install the 32-bit ODBC driver for your database. Please consult the database documentation and Google on how to do this.
- Your specific connection string can vary depending on the type of database (for example, Oracle has different connection strings than MySQL). A useful site to help you with the ODBC connection string is https://www.connectionstrings.com. Make sure you only choose the ODBC connection string in the site for your database type (like Oracle). You may need to play around with the different strings and test connections to find the one that works. If your connection string includes a Driver name, make sure it matches exactly (space for space) the one you have picked in the dropdown.
- Depending on the type of DB you use, the automatic creation of table/sql may fail. In that case, you might have to provision the schema for the table in the DB manually and then specify the SQL to insert records into the table in the program.
- You can save the attachment content using the following variable in your SQL
{RAW_CONTENT}
Further reading
Some interesting how-to links and posts for further exploration:
- Save rows inside of a CSV or Excel attached in an email to a SQL database!
- Save to multiple folders from multiple accounts using multiple email rules.
- Send emails automatically after downloading files or attachments or body
- Unzip files, Decrypt pdf’s and run script’s
- Extract data and download emails for invoice processing
- Convert email attachments to different formats
- How email rules work in-depth
Contact us
Contact us if you have any questions, suggestions about the functionality described in this article.