« MBS FileMaker Advent … | Home | MBS FileMaker Plugin,… »

MBS FileMaker Advent calendar - Door 12 - Sending Mails

candy cane Monkeybread Monkey as an elf candy cane
Day 12 - Sending Mails

We have already put together an archive in one of the previous doors. We would now like to be able to send this archive by email with the click of a button. To do this, we create a button in the mail portal.

When the button is pressed, a dialog is displayed. In this dialog we then set our text and our subject. In addition, we would like to have three buttons here with which we can either Cancel, Send the mail without archive attachment or Send with archive.

To create the dialog we use the functions from the dialog area. With these functions we can freely design our dialog. We can add buttons and fields and even display an icon to give the dialog a nicer look.

The dialog exists in the entire project, which means we can set the settings once and then call it in other scripts. It is important to note that if we want to create a new dialog, we must first reset the dialog settings. To do this, we use the function Dialog.Reset. Now we can set up the dialog as we need it. We want a field for the subject and one for the content of the mail. To add text fields, we use the Dialog.AddField function. As the subject is a standard field with a standard height, we do not need to specify anything here except the label. The content is different. We can make some specifications in the parameters. As we have just seen with the subject, we specify the field label, but we can also set a predefined text in the field or display a placeholder text in an empty field. The hiding of the text by dots, as is usual with passwords, can also be set here in the parameters. What interests us with our contents field is the height of the field. If this is higher than 20, it is a text view, which means that it is possible to fill this field with several lines of text. Please note that fields in the dialog are only available on macOS.

We now need the buttons with which we can decide how to proceed. To do this, we use the Dialog.SetButton function and specify the index of the button we want to set. A dialog can have up to 10 buttons. The index starts at 0 and is followed by the button name. To make the dialog look nice, we want to set our monkey as an icon. To do this, we use Dialog.SetIcon and specify our global variable with the image. The dialog is then displayed with Dialog.Run.

Set Variable [ $r ; Value: MBS("Dialog.Reset") ]
Set Variable [ $r ; Value: MBS("Dialog.AddField"; "Subject") ]
Set Variable [ $r ; Value: MBS("Dialog.AddField"; "Contents"; ""; ""; 0; 200) ]
Set Variable [ $r ; Value: MBS("Dialog.SetButton"; 0; "Send with zip") ]
Set Variable [ $r ; Value: MBS("Dialog.SetButton"; 1; "Send without zip") ]
Set Variable [ $r ; Value: MBS("Dialog.SetButton"; 2; "Cancel") ]
Set Variable [ $r ; Value: MBS("Dialog.SetIcon"; $$Overlay_Image) ]
Set Variable [ $r ; Value: MBS("Dialog.Run") ]

As long as the dialog is open, our script stops. Now we have functions for reading our fields and can find out which button was selected. With Dialog.GetFieldText, specifying the index, we read out the two text fields. The function Dialog.GetButtonPressed gives us the index of the button that has been pressed. We can now react differently depending on the button that has been pressed. If our index of the button is 2, the Cancel button has been selected. In this case, we exit the current script. If the index is 0 or 1, we create our email. To do this, we create an email reference in the workspace. We use the function “SendMail.CreateEmail” which returns the reference. Now we can set the subject and the content. Here we need the mail reference and the value we want to set as parameters. Then we define who the email is to be sent to. To do this, we use the SendMail.AddRecipient function and first enter the mail ID in the parameters and then the type of recipient. This is because we can not only simply create a recipient, but also someone in blind copy, for example. Here you have the choice of TO, BCC, CC or ReplyTo. We then enter the email address in another parameter. If we wish, we can also enter the name of the recipient. Now we have to decide whether we want to attach the archive. If the button with the index 0 was pressed, the archive should be assembled in a separate script. We have already seen how to create this archive from the files in a previous door. Only this time we want to have a conatiner value instead of creating the archive on the disk. We leave out the path in Archive.Create and set a file name in Archive.Close as an additional parameter. As our archive is not text and we can only return a text parameter to a calling script, we save the archive in a global variable. We have named this $$SendArchive. We set an OK as the return from the script. Back in our calling script we can then query the script result and if this is OK, we attach the archive with SendMail.AddAttachmentContainer, specifying the mail reference and the file in the global variable.

Set Variable [ $Subject ; Value: MBS( "Dialog.GetFieldText"; 0 ) ]
Set Variable [ $Contents ; Value: MBS( "Dialog.GetFieldText"; 1 ) ]
Set Variable [ $Button ; Value: MBS("Dialog.GetButtonPressed") ]
If [ $Button=2 ]
	Exit Script [ Text Result: "Cancel" ]
Else If [ $Button=0 or $Button=1 ]
	#Create Email
	Set Variable [ $EMail ; Value: MBS("SendMail.CreateEmail") ]
	Set Variable [ $r ; Value: MBS("SendMail.SetSubject"; $EMail; $Subject) ]
	Set Variable [ $r ; Value: MBS("SendMail.SetPlainText"; $EMail; $Contents) ]
	# Can be TO, BCC, CC or ReplyTo
	Set Variable [ $r ; Value: MBS("SendMail.AddRecipient"; $EMail; "TO"; Email::Emailaddress; Giftee::first_name & " " & Giftee::last_name) ]
	If [ $Button=0 ]
		# With Zip 
		Perform Script [ Specified: From list ; “ConatinerArchive” ; Parameter:    ]
		If [ Get(ScriptResult)="OK" ]
			Set Variable [ $r ; Value: MBS("SendMail.AddAttachmentContainer"; $EMail; $$SendArchive) ]
		Else
			Show Custom Dialog [ "Error" ; "Archive not created" ]
			Exit Script [ Text Result: "Error" ]
		End If
	End If

Next, we can specify a sender with SendMail.SetFrom, again specifying the reference, the sender mail address and our name. So that we can also send the mail correctly, we still have to specify the mail account via which we send the mail, so we set the password, the server and the user name with SendMail.SetSMTPPassword, SendMail.SetSMTPServer and SendMail.SetSMTPUserName, specifying the mail reference and the corresponding value.

# Set From 
Set Variable [ $r ; Value: MBS("SendMail.SetFrom"; $EMail; $Mailadress; "Monkey the Christmas Elf") ]
# Password
Set Variable [ $r ; Value: MBS("SendMail.SetSMTPPassword"; $EMail; $$MailPassword) ]
# Server
Set Variable [ $r ; Value: MBS("SendMail.SetSMTPServer"; $EMail; $$MailServer) ]
# User
Set Variable [ $r ; Value: MBS("SendMail.SetSMTPUserName"; $EMail; $$MailUserName) ]

At this point, we need to create a new CURL session. To do this, we use CURL.New.This function provides us with another reference, which we then pass to the SendMail.PrepareCURL function together with the MailID reference. This function then sets the settings that we have already made for the mail in the CURL. We then use CURL.Perform to send the mail. If we get an OK back from this function, everything went well during sending. 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 catch it with the MBS("IsError") function, because there was no error in the function but in the connection. For this reason, we have to check whether the function has returned OK. If not, we want to display a dialog.

Set Variable [ $curl ; Value: MBS("CURL.New") ]
Set Variable [ $r ; Value: MBS("SendMail.PrepareCURL"; $EMail; $Curl) ]
Set Variable [ $r ; Value: MBS("CURL.Perform"; $curl) ]
	If [ $r ≠ "OK" ]
		Show Custom Dialog [ "Error" ; "E-Mail can't send!:¶" & $r ]
	End If
	

We are now finished and can send our mails.


Monkeybread Software Logo with Monkey with Santa hat
11 👈 12 of 24 👉 13
Claris FileMaker Plugin
12 12 24 - 09:30