Chapter 12 email automation. In this chapter, we'll be going through an overview of what email automation is. We'll go through some input activities and some output activities as well. Overview email is probably the most prevalent communication method incorporates today. We all have been in situations where we get hundreds of mails over time and need to skim through them to get any relevant information. It usually takes us a chunk of our valuable time, which certainly could be put to better use.
Well, things could have been better had we all been allotted a secretary to work for us. Luckily, RPA can take care of this problem. We can implement a rule driven approach for bot to traverse through emails and perform certain actions if the required conditions are met, alongside repetative non productive tasks such as sending regular project updates system info logs, enterprise application reports, performance tracks etc. can be automated easily. I'm sure all the knowledge gained through previous chapters must be coming pretty handy at this point. UiPath provides dedicated activities to automate email operations.
It has amazing capabilities to filter messages based on certain criteria. Download attachments from receive mails, use message templates for standardized responses and supports SMTP pottery as well as IMF protocols. It also incorporates activities specific to MS Outlook and exchange mail servers. From RP a workflow standpoint, there could be one of the two scenarios where email operations come into play. input. This is the case when an email in the inbox or any folder of the mail server serves as an input to another process on system.
Example specific information provided in title or body section of an email attachments that come along with it and more output. This is the case where the email serves as an output to the process under consideration. Example system alerts generated from an application are sending email to the concerned users report sent out to authorized personnel to track data and analyze it among other options. So, all activities specific to mail can be found in the activities panel under like the available applique activities, app integration and mail. We'll be going through that in the UiPath studio in our sample case, let's discuss the categorization of supported email services. First is smtp.
SMTP refers to Simple Mail Transfer Protocol. It has a limited ability to queue up messages at the receiving end UiPath we can only use this to simply transmit messages. Part Three. Part Three refers to Post Office Protocol. It's one of the oldest TCP IP protocol used for receiving mails from mailbox server and download them into local machines. It is pretty outdated, but it's still supported by most email servers.
I map I map refers to Internet Message Access Protocol. It is only used to receive messages. However, it supports multiple device access to the mail server and attachments from the mail aren't automatically downloaded to the local system. This makes it quicker to check emails and perform operations like marking all emails as read or moving mails between folders or labels and so on. Ms exchange exchange is Microsoft's mail server platform that usually caters to businesses or large enterprise needs. It supports emailing calendaring contact, creating task scheduling, and many such features collaboratively.
UiPath integrates perfectly with Ms exchange and provides my required activities for various operations. Outlook. MS Outlook is the most preferred email client that corporates use to access their Microsoft Exchange Server UiPath connects to the MS Outlook through an API of desktop application does activities in it are pre configured with no need for RDS tasks such as mail server setups, config changes and other such technicalities. already existing outlook accounts are leveraged by the activities here. If MS Outlook is already properly been set up in a system, it is highly recommended to use its activities in bot creation process as well. alongside it is compatible with almost every issue Main protocol input activities.
There are quite a few mail servers as we just mentioned, which are supported within UiPath. And we can find all the mail activities by simply writing mail in the activities panel, like SMTP, Port three, Outlook, I map and exchange. Right. All of these are identical in terms of providing capability to retrieve mail messages, along with the specific capabilities for other operations of course, right. But I map BOC three and SMTP would require some configuration parameters to be set as input to connect to their respective mail servers. Outlook and exchange on the other hand, will work with the default values and support features to help administrators configure user profile settings and connection properties automatically.
So if it has been properly configured already on your local system, it will connect to that Application locally, and you'll be able to access your mail servers without having any conflict parameters been set up. Okay, let's take an example for IMAP mail retrieval, because all of these are pretty much the same. So the use case that we are going to take is that using ima protocol, we will be taking out five unread emails from our email account. Right. So for that, we need to provide a server name and port number and all the other input details. So suppose using the I map, mail server, I want to access my Google Mail gmail account, right?
So once you go, you can find these anywhere online, the, like SMTP details. These are those details that you either get from your ID serve it services of your enterprise or you know from your mail provider, which in our case, let's say is Google, right. So here are the map details, where it's saying that you know, the port number is 993, right and the server or the host is I map.gmail.com. So that is all what we have to write down in here. If you see mail folders in box, because let's say we want to access our inbox, right, and the port is 993, right, and the server is I'm app.gmail.com. Right here, you have to enter your email id and password as well.
Like, info at or could be anything you know, through our pa to gmail.com. And in here, you need to have your password as well which could be any thing mean it has to be the correct password, of course. And then what you can do is if you want to check only the unread messages, then you can select this check box. If you want to delete the messages, then whichever are gonna fit the criteria will be, excuse me, I haven't put that in double quotes yet. Okay, so the error, most of the errors have gone, right. And if you want to retrieve all those mail messages, then you can simply create a new variable, and it will be of type mail messages, and you can get all those messages from them.
Right? So I cannot use the password. But what you can do is you can, like if you want to have just the top 500 emails, simply in the top parameter, put five, right? And that's how you get let's say we create a variable and name it as I map messages, right? So, all those which will be fitting this criteria would be stored in this I map messages you know, variable output variable. Okay?
Now to get the retrieve these these mail messages, let's say one by one because this is gonna be like five males, right? So it's gonna be a part of, it's gonna be like a list of males, it's gonna be one male after the other. So how do you retrieve it? As always, we're going to use for each loop, right? This one, there's not a data table. So we're not going to use for each row we're going to use for each right and this item will automatically be chosen.
I map messages, right. So this item will be off mail messages type itself, or the row type. Right row of the iMac messages type of the mail messages So you can find this, if you want to check this out. I messages is the list and of mail message. So you can check this. Once you go to system.net dot mail message within the variable type, you can find the mail messages type as well.
I can show you system.net dot mail message dot mail dot mail message, same thing, right? So, that's how we retrieve the emails. And simply if you want to print, let's say, instead of item we can make it male as well that way it becomes more, you know, sophisticated to look at and to simply let's say print the subject of each and every male right, we can use a right line activity and the best way to access any possible functions you may have for This is gonna be so see because this mail is right now of type argument, right? So that's why you're not able to see the functions for the meal type. You gotta choose for the types gives me and mail messages already their mail subject, you can access that right. Now if you want to check.
There are like if you want to see the attachments or you want to get the body section you want to see who all people we have BCC or CC in the email, then that's how we can you know, get these information by simply using a dot operator. We can get sender or reply to list or priority of that email or to whoever that email was sent and headers encoding you can play around with all these parameters. And that's how you do it right. So, once you have let's say God the subject of each and every mail, you can directly write printed on the output panel which is what this activity is doing. Right? And say further.
Let's go here. Right. Further let's say you within this each of these emails if there are any attachments, you want to save those. Then there are two of these activities which are irrespective of the mail server that we have chosen. Right? Save either the save the mail message itself or save the attachments you can use save attachment activity, and automatically, the attachment would be saved and you will get a object Like where the, you know, this is the mail message.
So you want to print mail, right? Because this is what is trading through the list of mail messages and the folder path where you want your attachment to be saved. And here's the attachment, you know, as the output if you want that. So, there are times when a lot of operations need to be implemented to get the relevant piece of information we are seeking to display or for further processing. Right. Let's take a quick example for that as well.
Right, let's consider that the subject of the email that the users are getting from, let's say an enterprise application on a daily basis is in a particular format. And the format is something like this. Like system name, which could be like you know, Oracle 11, g dot two dot one dot one, something like that Oracle prod system, space, 11 G or Oracle, you know, something like that in the system name could be anything. Oracle prod, underscore 11 G, that's it. That's the system name, just for an example, say, then there's a space, and then the person that is sent to so say, the last name is Smith, and the first name is Kevin and separated by a comma. That's how the subject of the mail is.
Could be, right. That's how it has been configured in the enterprise application itself to send out the emails on a daily basis with the reports attached and the subject something like this. And to me, okay, and then it has a date time. Right? And then it says, whether it's a pass or fail, whether I mean, there was some issue or something like that, or everything has been good so far. And in your, you know, in in the business problem that you're solving Right now, you need to retrieve just the last name of the user.
Right? Could have been anything but, and once we go through this example, I'm just making all this stuff up right now. Right? We have to get the last name from all the mail messages that are being retrieved one by one. Okay. So what we'll do is, let's take this out to save attachments.
And we will use an assign activity. Right. And in here, we'll have last name, something like that. Right. This is the variable name that we have given, which you've also created in here. It could have been a string type as well.
Doesn't matter right now. Instead of you know, what you could have done is either mail that subject, you could have assigned and put this subject in a subject variable. But you don't need that. You don't want to use another variable just for an example sake right. So we are removing one extra step, or we are optimizing this. And we are going to use mail dot subject.
Right. And because it's a string right now, so we can use split function, there it is, right? And you want to split first using like the spaces. Right? And then you convert it to a character array, that space part you want to click, because I'll show you how. See, once you are putting the spaces right, you've delimited the whole string with spaces.
So now this one is index zero. This whole thing is index one This is index two and this is index three right. So, you will be accessing index one now, with all this information. So, what you can do is like once you have you know split this based on the delimiter you also convert it to a character array to get ready it becomes very difficult to write in these you know text boxes. So, what we should do is, we should open the expression editor This makes it easier right. And then we access the first element right.
That is all good because we are changing it to the we are delimiting it with the space we are making it a character array. We are taking the first character which is our first you know the index for now, which is last name comma first name and now we will further spread it Because now what we can do is, if we split this based on the comma, then we will have last name and first name at different indexes. Does that make sense? So, comma, right. And we use.to care array, which is visible here that textbox was really very difficult to write on. And this time, we'll take which index if you can guess, zero, that is correct.
So now we have the last name in our hand. Anything that is proceeding that comma is going to be a part of this array. You can do it because the last name there is some error. Let's see. So, you see, because this is of generic type, and what you should do is change it to string and you should be all set. So now the last name has got the you know, so these sort of Operations you might need to perform, you know, to get, and not just specific to email activities.
But it could have been for any data operations that you have to go for to get your, you know, one piece of information that you're looking for, from, you know, it's like finding a needle in the haystack. So that's what I'm going to say that you know, you have to play around with these data operations. string is a little complex, I would ask you to try new and different ways of you know, finding methods to get from a huge string text you can, you have to find the relevant piece of information and you've tried out with different functions dotnet functions that are provided within within the UiPath studio or online as well. You can just search for the dotnet predefined functions, and you can play around with the text data types, and it's going to be really, really helpful. outlet outlook mail retrieval, the activity to grab emails from outlook mail server is get outlook mail messages, which you can see right here.
Right. Similar to i get i map mail messages activity, it extracts data from the mail server based on the options provided to it in the Properties panel. However, very little effort is required in this case, as there are no configuration settings that are needed. And we need an output variable to store retrieve emails. That's it. Right By default, the activity accesses input folder, which can also be changed from the input properties right here.
So, alongside along along all this, one other positive aspect of using Outlook mail, retrieving activity is the provision of this filter property. Right. This can greatly reduce the effort of filtering the mails from the mail server and storing only relevant output. So the conventional way of doing This is by using many controls like FL statements within the loops, flickering between one criteria and another. And that too is going to work once we have downloaded the, like a large chunk of mails and then going through it, right because we need to have them locally available. So with this, the actual filtering happens at the server side itself and the output that we get is very crisp.
So for for an instance, right, let's consider that you intend to retrieve all unread emails from past two hours, right. And since the timestamp data for a meal message is not readily available in the email header, right and cannot be accessed directly through the dot operator, I will show you right, we need to dig deeper a little bit to retrieve this information. So this is where it gets challenging. And this is where, you know, the programming experience if somebody has it comes very useful for Right. So in the headers directory of the mail message, the date function can be pulled up can be used to pull up the timestamp information. Right?
This is how it is going to look like. First, let's say the get outlook mail messages here. Let's say we want only the unread mails, right, I don't care if it is top 30 or top five or top one, I already have put in an email on my server just to just for this exercise, so we don't have to put any email ID or password or anything. The main folder already is inbox, right? And I want to save the mill masters in a variable called outlook messages. Right?
So I save it in the outlook messages. And this looks pretty fine. Now in the for each loop, because I have to traverse through the mails which I may get. In this case I'm going to get only one because I have dropped most email that I'm looking for. But just, you know, to play along, let's say we have this item which we want to change it to mail message type, and it wants to traverse to the outlook messages, which is the list of the mail messages that we are going to get from the outlook mail server. Right.
And here what we can do is we can use a filter or let's say we want to display just the headings of the same you know, have the emails item dot subject or something. Okay, right. Yeah, I mean, we could be anything subject or could be header or all the information that is there, but there is no, you know, that timestamp information you cannot see any timestamp For our date time or something like that in here, right? So if there is no direct function, this is where we're going to use the filtering messages, right? This is where the filter is. And this is going to be a little tricky.
So I would ask you to first take a note of this, and then probably play around with it. And I'm gonna tell you where to find these things as well. Right? So in double quotes, we right received time. Right. And if it is greater than, and in here, we don't actually close it, we start off with a single code as well, before the double quotes, we started a single code.
And that's because to use the functions for the date time, they need to be in the single passes to further filter to, you know, to be able to access those functions. So we use a concatenation operator. And we have already used now before. Now, dot add hours, right? To add the hours. And for last two hours, if you want, you want to put minus two, you want to add minus two hours, right?
And we change it to string. And then in here we put in the format, whichever format we want. And that again, depends on the server as well as to which font it is taking. And you got to play around with this a little bit here, and then you concatenate and you finish off the single double quotes. Okay? So this seems to be like this.
Okay, so I close the double quotes first and then the single quote and the double quotes based on what I've started Right, these double quotes were started and like we had these and the single core as well. So we have to have three of these in total. And so we use this filter where we are filtering the database on the main messages based on the receiving time of theirs. And we are just displaying their, you know, subjects. So I'm going to tell you where to look for these functions as well in a few minutes. But if you run this, I don't have outlook even open right now.
Right. But what I should be able to get is sample test email, which is like the first most email that I have in my server right now in the outlook server. So I'm able to access those based on any parameters I may have, you know, based on any first name last names, which you know, we might have to play around to get the information and then probably do operate more on it by saying that you know, if the first name is Or the last name is this or a first name is this and last name is this then we want to do the sort of operations. So that's how the whole workflow would be generated. Right? There are quite a few things that you can do.
So, you got to play around and use as many data operations as possible even for mail messages types, right. And just so, you know, we can look for VB dotnet functions. And if you go in here, this is where you can find these, you know, different functions. And you can check out whichever is the math function, which are the string functions which are very commonly used right? To filter character format number to lowercase you can use our case and trim. We have been using trim for a while right?
They've given some samples as well. So, the format is going to be pretty much the same because UiPath is based on the dotnet itself. So, all these functions have already been inherited. And this is how, you know, we find these functions use them if you want to find the length of, of, of a string, you know, how many number of characters are there in that string, you can use the Len function and so on. Right. So, this is it for for the outlook filters and keywords, you know, like those filter statements, what you can do is outlook VBA items, find something like that.
So, these are going to be specific to the Microsoft Outlook, right. So, you can see in here like if you want to use filters company name equals to Microsoft, these are the types of things that are supported in here. Find method is there we can have find next method, we have item method. And, you know, you got to see how dotnet functionality if there is any pre will dotnet functionality that can accomplish the job that you're working on. And if so, then try to use them you might have to look for and try out new and more stuff. To get really, really good at this.
This is gonna require a lot of practice. It's not a, you know, a full fledged list that I can give you it's not like a one on one thing. It's very practice oriented, you practical oriented, you gotta work on it to get better. Okay, so the last topic in this chapter is output activities. sending an email is supported by SMTP, Outlook, and exchange mail servers. You can see right here All right, send SMTP mail message, send outlook mail message, or send exchange mail message, right.
As before outlook and exchange would require minimal to no server setup or config changes, while SMTP would require a server name port number, along with the login credentials to access the mail server. Other than that, the other than these like the mail server settings, the three activities have pretty same structure. Let's take send SMTP mail message right in here. And we need to provide the port number whatever it is, right? just making this up right now. Then the server name which could be.this.com something and the email id is gonna be this@this.com and say password is gonna be something like this.
Right. So we have given us This data and then once we go to it, this is how each and every send mail message activity looks like. This is going to be the recipient information, the email id, this is going to be the subject that we want to give for the for the email, let's say info pro rpa.com right and the subject could be testing could be anything, right? And the body can be like, like you know, really big text with multiple lines, right? Something like this, right? Now, um, this works fine.
You can always send the message if all the you know connecting parameters are okay. And this will simply get the job done. A few things first, doesn't this look a little clumsy right the body of this mail message activity is containing text, which is huge. And usually that's how the emails are in the body section, we could we could have like, a few characters to probably thousands of characters, right? So to make this more comprehensible, better and optimal, right, we can use a template, right? So a template is like, you know, any file that is stored locally, and that's gonna have some data within it.
Right? So say, I have a notepad file called template dot txt, which has this data. This email confirms that currently the email functionality is up and running in UiPath. Right. And I saved it and it is there in on the desktop itself. This is where the file is, right.
So now what we can do is, we can use this activity called read text file. And in here, once we put the text file, we're going to provide the information like where this file is located, which in this case is slash template dot txt. Right. And let's see, we store that data in. No, sorry. Okay.
And we saved template. That's the output variable where once the data is read, this is where it's going to be stored. Right? And the stock node. Let's put this in the back because you want to read the data first, right? And now, in the body section, just like you know, we do it for any other activity instead of a string in double quotes.
We can Simply use the variable write a very easy to understand process because we have done much more complex stuff in the previous chapters. Right? Now, there is a way that we can use, like some dynamic content within this template. Right. So, so dynamic content is, let's say for example, let's open this Notepad. txt document file again.
Right. Is it here? Yeah. So this email confirms that currently, and let's say in currently, right, we put a parameter like this, which is like how we put in the, you know, a placeholder. This is for a placeholder for any dynamically generated content. So suppose we want to use the current date, time and time functionality.
It is dynamically generated content right? Because it depends on at what time the workflow is running. And we want the bot to get data from from the local system, the date time in system date time information and put it in this template and then subsequently in the body section of the email as well. So we do this right and in save, right, this is how it plays this the placeholders for any dynamically generated content. And then the for the functions, the predefined functions for those to get that dynamically generated content. We use that in the body section itself using a function called string dot format.
So like quick trips, quick tricks, right? This is how we make our process more efficient, more optimal and comprehensible in this Like the last trade is probably professional. So you put it like this string dot format, we put the template, which is the file where we want and the now function, which is gonna get the current data, right, and you see no errors as such, everything is good. That's how you use you put in the placeholders in the template file, you put values from this function. So this is all dynamically generated content, and a very good and optimal way to use these functions right. Now, suppose you want to include some attachments as well.
Right? Then these attachments you provide first a direction is going to be in because it's an input right? And the type is going to be string because here the value is going to be the location where the attachment is available locally in the system, and it will be attached to the email itself, and then sent to the recipient. So just to show you how it works, let's say we have an image file or we create an image file from the workflow itself, there could be a case, right? There might be a lot of sequence of steps that are involved before this. And we have activity like take screenshot, that's gonna take screenshot of Vi computing some it could be like, you know, whether we are accessing the target application, whether we are in middle of a workflow depends what screenshot you want, it's going to take the, the screenshot, and we saved that screenshot in, let's say, screen, underscore one, something like that.
This is like internal to the workflow itself. It doesn't matter much. And so as soon as Let's ask for this program, as soon as the program starts, it's gonna take a screenshot first, and then what it's gonna do is, let's say We cannot use a screen shot of variable type directly, we have to save the screenshot locally in the system first. So we use this function called save image. And in save image, we provide these images screen one, right this is the image that we just copied from the screenshot activity and the file name. We put in as, let's say the desktop, right?
So there's no excuse me, this was this. All right, yeah. And slash screen one dot jpg. It's a JPG file. could be any other fine as well, but usually we go with the JPG files, right. So now the image that was taken by the screenshot activity would be saved as screen one dot JPG on locally on our desktop.
Right? Now, what we can do is in here, in the attach files, this is where we provide that value. So it's gonna be this screen one dot jpg, which will be created automatically by the web and the bot will be working on right. So you click OK. And no errors as such. If the credentials are okay, the polling server Okay, you can definitely test out this activity or I would ask you to try this with the outlook and you will find it much easier because you don't have to do any of this configuration. But other than that, everything is pretty much the same.
Alright, so that's how you attach the files. That's how you use a template. within the body section. You can add some dynamically generated content you Provide a legible subject and the recipient email address. And you should be all set to send out the emails. All right.