Automating Emails Using FileMaker 18's Send Mail Script Step

Subscribe to Our Blog

As FileMaker developers, it’s common practice to need to contact the client records stored in the database, often scheduled daily, or weekly, or by a specific event. Without FileMaker’s ability to automate emails, this could become very time consuming. This blog post will cover the essential scripting techniques required to save time by automating emails and optimize communication with clients by attaching files.



The Send Mail script step

Right off the bat, the most important thing to understand is the Send Mail script step. Make sure to read all of the documentation for the FileMaker 18 version of this script step. There were no big changes to this script step, with the recent release of 18, but an important change to this script step was introduced with FileMaker 17, and that is the ability to attach multiple attachments to an email.

There is a great deal of options that come along with this script step. The first option is specified on the same line as the script step, the option to leave dialog on or off. Leaving dialog on will bring up the default email application, and leave it open for review, rather than sending it. In some email applications, the new message is left in the Drafts folder. If With Dialog is left off, the email is composed and placed in the email applications Outbox, ready to be sent.

Opening the options dialog, presents you with much more choices that you will need to understand to be able to use this script step properly. The first option being Send Via:, with the options of Email Client, or SMTP Server.

SMTP server

The Simple Mail Transfer Protocol, also known as SMTP, is a communication protocol for email transmission. SMTP is used for sending, not receiving mail. 

At this point, you may still be wondering why you would choose SMTP. SMTP is one less layer of complexity that your application must deal with. Using the mail client introduces additional points of failure over sending via SMTP directly. You can avoid issues you would otherwise need to consider, such as, is FileMaker compatible with the mail client? and is the mail client configured correctly?

To send emails through SMTP with FileMaker, you must provide sender information, as well as SMTP Server information. When you choose SMTP Server in the Send Mail Options dialog, a new dialog box opens for the SMTP options. Explanations of each field within this dialog can be found in the chart below.

Send Mail script step options

Once you’ve decided if you will be sending mail through the
E-Mail Client or an SMTP Server, the next option to consider is if you want to
send one email using data from the current record or, send multiple
emails (one for each client in the found set)
. Sometimes, all emails
generated need to be sent to one static email address, but things are not
always that simple. You will often need to specify the destination emailfield
with a field name, or a calculation.

Keep in mind that when utilizing scripting, you can send emails to multiple recipients, while the send email using data from the current record is selected. For the To, CC, BCC and Subject fields, you will have the options to Specify by Field, Or Specify by Calculation. For situations where each record has one field per record, storing one email address each, all you need to do is specify that field.

In situations where you need to send the email to multiple emails stored on one record, you need to make sure that the emails are separated properly, using carriage returns (¶). This can still be done either through a field, or a calculation, just make sure, if using specify by field that the field is formatted correctly. You may want to use a calculation field that cannot be edited by users, to concatenate the list of emails.

The body

Unlike the previously covered email fields, the Body
presents a third option, to Insert Text from a File. Selecting this
option will open a dialog that asks you to choose a file. Note that to be able
to select a file, it must be a .txt file.

A useful technique for creating a longer and/or more dynamic Body is using a Let statement to set a variable. You may also need to use a Let statement to specify a To, CC, or BCC, or Subject, but I use it almost every time I specify the calculation for a Body. Below is an example of a calculation I would use for a $body variable in a script to send an email to a client.

Using a Let statement like this will make the calculation easier to read and make changes to, if you need to come back to it. Also, this will help anyone else who needs to make changes to the calculation. When you don’t make your Body calculation dynamic like this, you will likely soon be overwhelmed by the amount of concatenations you need to read to make what could be a simple change. The following calculation displays the same output, but is much harder to read and change:

No matter how many indents and carriage returns you add to this calculation, it will never be easy for a human to read. So, help your coworkers out by using Let statements and variables to construct your email options.

Attachments

The last option on the Send Mail [] options dialogue is to Attach Files. The button is found at the bottom left of the Send Mail dialog box and it opens up its own dialog box. If you need to just attach one file, the Add File… button at the top right will pull up the Finder or File Explorer. Selecting a file from this window will properly assemble the path needed. You still have the ability to construct these paths manually, but it is recommended to use variables for the construction.  

As mentioned earlier, FileMaker 17 introduced the ability to add multiple attachments to an email. This can be done in many ways, the first being to simply select multiple files using Command/Control or Shift. Doing this will format the multiple paths correctly, using carriage returns.

This correct file path should be noted if you plan on attaching multiple files by constructing the paths yourself with a variable. The carriage returns will need to be included either within the variable, if you will be building or multiple paths within one variable (done using list function in the first image below), or in the specify file box, if you have a different variable for each path. (second image below)

Conclusion

Understanding the Send Mail script step is absolutely necessary in FileMaker development. Without FileMaker’s ability to automate emails, you could end up needing an employee whose entire job is writing emails to clients, or users of a file. It's important to understand how to set up email automation in a modular fashion so that other developers can understand and modify the scripts, if need be.

Hopefully this blog post helped you understand the essential scripting techniques required to save time by automating emails and optimize communication with clients by attaching files.