« Search scripts in Fil… | Home | MBS Plugin Advent cal… »

MBS Plugin Advent calendar: 21 - SendMail

Door 21 - SendMail

Fact of the day
The first e-mail as we know it today was sent by Ray Tomlinson in 1971.

Did you know that you can also use the MBS FileMaker Plugin to create mails according to your wishes and send them to one or more people? I will show you how this works in today's door.

First of all, we create all the fields we need in our project. Today we want to add a graphic header and footer to our email. We put the image files for each of these in a container. Then we need a field for the subject and a field for the recipient. In this example we only send the mail to one recipient, but I will explain how you can send the mail to other recipients. We also need a field for the mail text. We also want to send an attachment with the mail. We also need a container for this. In addition, we can now also create fields containing our data for the email layout. The layout can then look like this, for example:

Now we can write the script. First we convert the text we want to send by mail into HTML. For this we have the Text.TextToHTML function. The cool thing about this function is that we not only transfer the text into the HTML, but also the formatting of the text is transferred and thus an HTML with the matching styling tags is created.

# Read HTML Text from field
Set Variable [ $HTML ; Value: MBS( "Text.TextToHTML"; DoorTwentyOne::Text; 1) ] 

In the next step, we place a placeholder for the header in front of the HTML and append a placeholder for the footer at the end. We replace the strings $$Header$$ and $$Footer$$ in a moment. Then we build the rest of the HTML structure around it.

Set Variable [ $HTML ; Value: "$$Header$$" & $HTML & "$$Footer$$" ] 
Set Variable [ $HTML ; Value: "<html><body>" & $HTML & "</body></html>" ] 

We then replace the header and footer with an img tag that refers to an attached image in our mail. With the cid in front of the image name we say that we want to attach the image and also integrate and display it in the mail. We will come to the attaching of these images in a moment. For now, we'll just create the template for the images.

Set Variable [ $HTML ; Value: Substitute ($HTML; "$$Header$$"; "<img src=\"cid:Header.png\">") ] 
Set Variable [ $HTML ; Value: Substitute ($HTML; "$$Footer$$"; "<img src=\"cid:Footer.jpg\">") ] 

There is a bit more to a mail than just the content HTML, which is why we create a mail in the working memory. With the reference as a parameter in other functions, we can then set further information for this mail.

Set Variable [ $Mail ; Value: MBS("SendMail.CreateEmail") ]

In order to be able to send the email, we need a valid mail account for which we have to enter information. We need the user name (SendMail.SetSMTPUserName), which is usually also the mail address, the password (SendMail.SetSMTPPassword) for the account, as well as the SMTP outgoing server ("SendMail.SetSMTPServer). In the SendMail.SetSMTPServer function we can also specify whether we want to use TLS encryption. This is what we want in our example and for this reason we specify 1 in the parameters.

Set Variable [ $r ; Value: MBS( "SendMail.SetSMTPUserName"; $Mail; DoorTwentyOne::UserName ) ] 
Set Variable [ $r ; Value: MBS( "SendMail.SetSMTPPassword"; $Mail; DoorTwentyOne::Password ) ] 
Set Variable [ $r ; Value: MBS( "SendMail.SetSMTPServer"; $Mail; DoorTwentyOne::SSLServer ; 1 ) ] 

Now we can set the individual information for the email itself. For example, we set the recipient of the email with SendMail.AddTo. In addition to this one recipient that we use here in the example, you can, for example, also loop through data records in a relational table and use this function in each loop call to add another recipient. But you can not only add standard recipients, you also have other functions for other types of recipients. You can add recipients in the CC with the SendMail.AddCC function or in the BCC with SendMail.AddBCC. If you would like to use different types in this loop, you can use the SendMail.AddRecipient function. Here you simply enter the recipient type as an additional parameter. You can choose between the types TO, BCC, CC or ReplyTo.

Now, of course, we also want to determine the content of the mail. With SendMail.SetSubject we specify the subject for the e-mail. We can also set the HTML text that we have already put together. To do this we use the SendMail.SetHTMLText function.

# Recipient
Set Variable [ $r ; Value: MBS( "SendMail.AddTo"; $Mail; DoorTwentyOne::Recipient ) ] 
# Subject
Set Variable [ $r ; Value: MBS( "SendMail.SetSubject"; $Mail; DoorTwentyOne::Subject ) ] 
# Content
Set Variable [ $r ; Value: MBS("SendMail.SetHTMLText"; $Mail; $HTML) ] 

Now we need to add our attachments. As already mentioned, we now also need to attach our inline graphics. To do this, we use the SendMail.AddAttachmentContainer function. In the parameters, we specify the mail reference and the container in which the image is currently located. We also enter the file name, the type and the InlineID that we have previously defined in the HTML.

Set Variable [ $r ; Value: MBS("SendMail.AddAttachmentContainer"; $Mail; DoorTwentyOne::Header; "Header.png"; "image/png"; "Header.png") ] 
Set Variable [ $r ; Value: MBS("SendMail.AddAttachmentContainer"; $Mail; DoorTwentyOne::Footer; "Footer.png"; "image/png"; "Footer.png") ] 

You can use the same function to attach normal attachments that are not displayed in the email. You do not need all the parameters here. The mail ID and the container in which the attachment is located are sufficient here.

Set Variable [ $r ; Value: MBS( "SendMail.AddAttachmentContainer"; $Mail; DoorTwentyOne::Attachment ) ] 

Now we have to create the CURL connection via which we want to send the mail. First we create a new CURL connection with CURL.New. With SendMail.PrepareCURL we then set the settings that we have already made for the mail in the CURL. We then use CURL.Perform to send the mail.

Set Variable [ $CURL ; Value: MBS("CURL.New") ] 
Set Variable [ $r ; Value: MBS("SendMail.PrepareCURL"; $Mail; $CURL) ] 
Set Variable [ $r ; Value: MBS("CURL.Perform"; $CURL) ] 

If we were able to send the mail, we receive an "OK" back from the function. If there was a problem with the connection, a text with an error number is returned instead of the OK. We have to be careful here. If there was an error during sending, we cannot intercept this with the MBS("IsError") function, because there was no error with the function but in the connection. For this reason, we must check here whether the function has returned OK and, if not, display an error message to inform the user.

If [ MBS("IsError") ] 
	Show Custom Dialog [ "Error" ; "An error occurred while sending the email" ] 
End If
If [ $r ≠ "OK" ] 
	Show Custom Dialog [ "Error" ; "An error occurred while sending the email:¶" & $r ] 
End If

This is what such an error message might look like. In this case, the corresponding sender address was written incorrectly.

Last but not least, we have to release the reference of the CURL connection and the reference to our mail.

Set Variable [ $r ; Value: MBS("CURL.Release"; $CURL) ] 
Set Variable [ $r ; Value: MBS("SendMail.Release"; $Mail) ] 

Now you can send your Christmas mail to your hearts desire. Have fun with it.

Monkeybread Software Logo with Monkey with Santa hat
20 👈 20 of 24 👉 22
Claris FileMaker Plugin
21 12 23 - 07:54