04. Training – Part 1 [9:30]

Description: This video explains the following:

  1. How to import mail.
  2. with multiple attachments.

Transcript:

In this video we are going to discuss how to import mail from MacMail into FileMaker allowing you to keep a complete archive correspondence history for all of your email. Let’s discuss the Plug-In functions that are used in order to import your email.

0:30 First, we will call PCMM_OpenAccount( Name) in order to open a specific Mac Mail account that we are working with. Then we will call PCMM_OpenFolder( FolderPath) and pass the folder path, then we will call PCMM_GetRercordCount this returns the number of available records in the currently opened folder. Next we call PCMM_GetFirstRecord and that’s going to open the first record in the currently opened folder.  Then we call PCMM_GetFieldData( FieldName ) pass the field name and this allows us to extract the value contained in a specified field. Examples of field names are “To”, “Body”, and “Subject”. For a complete list of all of the available values please see the documentation. Next we will call PCMM_GetAttachmenCount, this is going to return the number of attachments the currently opened mail record has. If we do have attachments then we will want to call PCMM_GetAttachmentName( Index ) and this is going to allow us to get the specific name of the attachment. Then we call PCMM_SaveAttachment( FilePath ; Index ) that’s going to save the specific attachment, and lastly if we still have records in our found count we’re going to call PCMM_GetNextRecord, repeat these steps, and continue to loop through and get all of the mail records in the specified folder.

1:51 Let’s open our FileMaker demo file and go through some of these scripts here as a live example. First I’m going to refresh my account list and I will specify the Melinda Account. Then I’m going to navigate to the Mail tab and I will refresh my folder list here this is retrieving all of the folders that are available in the Melinda MacMail account. I’m going to select my Inbox/Core folder and this is going to import all emails from my Inbox/Core folder. Now before I hit the Import Mail button I’m going to go ahead and turn on debugger. Let’s go ahead now and hit the Import Mail button and I will open up the Data Viewer here and we’re going to perform the script ImportMail. I’m going to skip over all of these scripts here, just because those are from our demos that are performing additional Plug-In checker scripts. And first we’re going to go right down to begin to validate that an account has actually been selected just a check that we have in our demo file. Then we’re going to validate that a folder has been selected and next we’re going to actually open the account specified, we’re going to open the folder specified, and as you recall the account is the PCIMelinda and the folder is Inbox/Core. Next, I’m going to get my record count, and so if we go ahead and get the record count you can see my record count is “2”, so I currently have 2 mail records in the Inbox/Core folder. And let’s just pause for a moment and go over to MacMail and we can see that I do indeed in my Inbox/Core folder have two records that exist here. Let’s go back into FileMaker and continue through. And this is just a prompt that we give the user to ask where they want to save attachments into FileMaker or if they don’t want to save attachments.  I’m not going to get into that scripting because that actually doesn’t include any Plug-In functions, that is strictly FileMaker script steps and you’re welcome to open the demo file and explore that further. I’m going to go ahead and select Save because one of my emails does indeed have an attachment and we want to look at the Plug-In functions that are required to get those attachments into FileMaker. These are all FileMaker script steps that we’re just doing some housekeeping here, so no need to really explain these because you are a FileMaker Developer! So we’re really just interested in the Plug-In functions that we are working with.

4:36 So what we’re going to do is – actually let’s just put our marker right down here – and we’re going to start with our first Plug-In function which is GetFirstRecord. So what we’re going to do is call GetFirstRecord, of course check for errors, and let’s continue down…enter our loop and what we’re going to do now is just get all of the fields for this record, you’re going to start to see them populate, we’re getting the “Sender” field, the “To” field, “CC” field, “BCC”, we just continue with however many fields that you want to get in your solution or whatever is required by your clients. And you can see them all starting to populate over here in the Data Viewer.

5:11 And next we’re going to get the attachment count right here. So let’s go ahead and get the attachment count and we can see that my attachment count is “2”, so I currently have two attachments in this email and that means we are actually going to perform the script ImportAttachments right here.  We’re actually going to bring these attachments from MacMail into FileMaker, and that’s done first by calling GetAttachmentName right here, that’s going to get the attachment name. As you can see the name of the attachment is shown here on the left. And then let’s go navigate down here again just to FileMaker scripts steps determining where we’re going to store the attachment during the transition from MacMail to FileMaker, you have to have some sort of temporary holding tank. And so, depending on your FileMaker version and our demo, we use the desktop if you’re using 8 or 8.5, and if you’re using 9 or greater then we use a temporary folder. And again that’s just a middle man folder in order to transport the attachment from MacMail into FileMaker. You have to have some middle folder, or middle man if you will. So next we’re going to call the SaveAttachment function here and that’s actually going to save the attachment into FileMaker. And we’ll just continue here, we’re going to have to go through this loop one more time because you’ll recall we had two attachments. So let’s go through and we’ll get our second attachment here, save that in FileMaker and now we can exit our loop.

6:50 And we can continue on getting our next record because we did have two records in this folder to import. So here we’re going to then call GetNextRecord. And there still is one record left to obtain, so again we’re going to just perform the same steps. We’re getting all of the field data, specifying the specific fields that we want, getting our attachment count; in this case the attachment count is zero here. And we’ll continue here, there is no next record, as you can see we’re returning end. That’s going to allow us to exit the loop and we can close the window. And if we go ahead and look in FileMaker I now have my two emails that are brought in from my Inbox/Core folder into FileMaker, and here they are with the attachments intact. The actual attachments are actually contained in FileMaker for safe keeping so you can have a complete archive correspondence history. And if we want to go ahead and go into Manage Scripts and take a look at the ImportEmail script you can see here I am opening the account, then I’m opening the folder, we are getting a record count, then we are getting the first record.  And always properly trapping for errors, we find that developers get into trouble due to a lack of error trapping, so make sure you do include error trapping in your solution. Then we are getting all of the field data’s for all of the required fields that we desire. Then if there are attachments we go ahead and get the attachment count, and then as you can see we go into that ImportAttachments script which you saw in action previously. And if there are additional records then we’re going to call GetNextRecord and continue on through the loop.  And if we were to go ahead and look at the ImportAttachment script here you can see we are getting the attachment name and storing the attachments in a temporary directory depending on your FileMaker version this is just the way we’ve designed it in our demo file. And then SaveAttachment actually saves that attachment into MacMail. And that’s it – those are all of the Plug-In functions that are required in order to import mail from MacMail into FileMaker. And here are the functions again for a quick reference, I do encourage you however to look at all of our videos, documentation, and reverse engineer our demo file. Send us an email if you have any questions, give us a phone call, or browse our forum if you have any questions or want to share anything that you’ve found. We hope you enjoy connecting MacMail and FileMaker and thank you for watching.