Nowadays, it is very common to use e-mails to exchange digital messages from one author to several recipients. These e-mails can also have a list of files attached that will be sent along with the message.
Kettle offers job entries and transformation steps that allow the sending of e-mails with attached files. In this recipe, you will use the Email job entry to send an e-mail with a set of files attached with a particular condition: the files must have been modified after a given date.
You need a directory named filesToAttach
containing sample files. You will also need access to an SMTP server. You can use the smtp.gmail.com
server. You also need at least one valid account to play with.
Carry out the following steps:
filesToAttach
folder in the File or directory textbox. Type .*
in the Regular Expression textbox and click on the Add button to populate the Selected files grid. lastmodifiedtime
, the operator'
, and enter a constant value date to compare with, for example, 2010/11/01
. Don't forget to select the Conversion format expression to match the date format. filename
in the Filename field, and General
in the Type of file to prompt.You need to fill the Authentication user and Authentication textboxes with a valid user and password. If your account is [email protected]
, then you have to type user
in the Authentication user textbox, and your e-mail password in the Authentication textbox.
The default Port for the SMTP Server is 465
, but if you use a secure connection through TLS, then the default is 587
.
General
in the Select file type listbox. files.zip)
. filesToAttach
folder that fulfill the date condition.The first task in the example was to create a transformation that gets the files that will be sent as an attachment with the e-mail. For this, you used the Get Files Names step to get the files inside the filesToAttach
folder, and then the Filter rows step to filter the files with a lastmodifiedtime
greater than a specific date, in this case 2010/11/01
. Only the files that pass this condition will be set in the result. Remember to uncheck the Add filename to result checkbox in the Get Files Names step, because you don't want all the files, only the result after the row filtering.
The job run this transformation, and then executes the email job entry. This job is quite self-explanatory. Under the Addresses tab, you must complete the destination and sender e-mails addresses, and the e-mail content under the Email Message tab.
The example uses Gmail's SMTP server (smtp.gmail.com), but you can use any e-mail server as long as you have access to the information required in the Server tab. Take a look at the firewall configuration if you have problems reaching the specified address.
Under the Attached Files tab, you selected the file type option named General
; this indicates that all the files included in the result list will be attached to the e-mail. In addition, you configured the entry to zip all files into a single file.
If the previous jobs or transformations also added files to results, then those files will be attached as well. Therefore, a Delete filenames from result job entry might be required at the beginning of this job.
You used the Mail validator job entry to verify the e-mail address structure. If the structure is not correct, then the email job entry will not run. You could use the SMTP check? checkbox here, if you want to validate the existence of the accounts.
You will find more features about sending e-mails in the following subsections:
Let's assume that you want to include the log of the transformation inside the attached ZIP file. This is very simple; just double-click on the Transformation entry job and set the log file under the Logging settings tab, as shown in the following screenshot:
You can specify a more detailed level of log in the Loglevel listbox if you want.
Then, you need to include this file as an attached file. Double-click on the email job entry, select the Attached Files tab, and select the Log
item in the Select file type listbox. As you need to select both General
and Log
items, you have to do it by pressing the Ctrl key.
After running the job, an e-mail will still be sent, but this time, the ZIP file attached will also contain a new file named logFile.log
with the log information.
There is a transformation step named Mail, in the Utility category, which is very similar to the Mail job entry. The main difference is that the Mail step sends one e-mail for each existent row.
Its configuration has listboxes instead of textboxes to refer to different settings (addresses, server configuration, subject, body, and so on). Here you should refer to existing fields instead of typing them.
In some cases, you could use either the step or the job entry with similar results, but there are particular scenarios where one approach is better than the other. For example:
For an advanced example of using the Mail step for mailing, you can follow this link: http://kjube.blogspot.com/2011/01/mailing-new-years-cards.html. The blog post by KJube explains a transformation that sends best wishes for the New Year to a list of people. The following two considerations arise about the example provided:
3.146.176.145