How do I Attach a File to an E-Mail?

This lesson describes how to attach files to an e-mail and launch a new compose window from within LiveCode. This lesson requires prior knowledge of lesson How do I Create an HTML E-Mail?. The examples in this lesson demonstrate code changes or additions to the examples that were covered in How do I create an HTML E-Mail?.

Note: This lesson assumes that you are using Thunderbird as your default e-mail client. Instructions on setting up Thunderbird are not provided here.

Introduction

This lesson builds on the information covered in lesson How do I Create an HTML E-Mail? and adds functionality that allows you to insert files into an e-mail. We are using the code base from the preceding lesson that should have already provided you with a working LiveCode stack to create HTML e-mails.

Creating the Input Fields

Creating the Input Fields

The first step is to add a button to our existing user interface that allows us to select files we want to attach. Add a new button and give it the name Attach.  You may have to rearrange the contents of your user interface slightly, but no other changes should be needed.

Setting up the Variables

Adding the new Attach button to the stack gives us a new card script that has to be populated. We have to create a global variable that stores the attachment selections we may make. This approach differs slightly from the methodology we used before. The rationale behind this approach provides us with the ability to insert more than one attachment into a single e-mail. This makes it necessary to store the path to each file before constructing lvLaunchString. We declare the global variable gvAttachment and add it to the top of script button Launch and the top of script button Attach. This declaration has to appear in the code on the cards before the on mouseUp declarations.

# declare the variable that holds the attachment locations

global gvAttachment

Selecting Attachments to be Sent

A convenient way to select files is to use a file selection dialog. This allows the user to navigate through their filesystem using a purpose build interface, rather than having to write the path and file names into a Text Field.

The following code opens a file selection dialog and adds any selection to gvAttachment.

Add this code to the on mouseUp call of button Attach.

# open the file selection dialog

answer file "Select a File to Attach"

if it is empty then exit mouseUp

 

# add the selected attachment to other possible attachments

if gvAttachment is not empty then put gvAttachment & "," into gvAttachment

put gvAttachment & it into gvAttachment

Extracting Data from the Input Fields

In order to add the selected attachments to the e-mail, we have to add one statement to the block of code that populates lvLaunchString. The last line of the following code is new and should be added to your code in the on mouseUp call of button Launch.

# populate the launch string with data from the user interface

put empty into lvLaunchString

populateLaunchString lvLaunchString, "to=", the text of field "toField"

populateLaunchString lvLaunchString, "cc=", the text of field "ccField"

populateLaunchString lvLaunchString, "subject=", the text of field "subjectField"

populateLaunchString lvLaunchString, "body=", the text of field "bodyField"

populateLaunchString lvLaunchString, "attachment=", gvAttachment -- this line is new

Launching the Compose Window

Launching the Compose Window

The last change to the stack empties the content of gvAttachment, once the Thunderbird compose window has been launched. This is necessary to prevent previously selected attachments from being added to new e-mails. The last line of the following code is new and has to be added to your code in the on mouseUp call of button Launch.

# launch the thunderbird compose window

set the hideConsoleWindows to true

open process lvLaunchString for update

close process lvLaunchString

put empty into gvAttachment -- this line is new

All information from the LiveCode user interface is placed in the Thunderbird compose window. There is also a frame on the top right hand corner that lists two LiveCode attachments. These were added by selecting two files through the functionality that is now provided by the Attach button.

Note: This lesson has shown you how to create HTML formatted e-mails with multiple attachments via the Thunderbird command line interface. It is important to be aware that the examples here do not test the content of the Text Fields for constructs that Thunderbird may interpret as command tokens and arguments. Any production application that is designed around this lesson must ensure that Text Field data does not contain values that Thunderbird may interpret as a command.

0 Comments

Add your comment

E-Mail me when someone replies to this comment