How To Send Two Emails in Same Trigger

About this Tutorial

Without having to create two functions and two triggers you can combine two email sends into one function.

Video Transcript

0:00 You're going to learn in this video how to send two emails in the same trigger. This is a fairly simple concept, doesn't take much time to go through it, but I want to create a situation in which, ah, you would, maybe, have to send two emails to two different people, and maybe on a weekly basis, on a
0:16 weekly trigger. We're going to set up the Apps Script, um, and I want to stress to you that this is a fairly simple idea, but it is not intuitive.
0:28 Ah, if you are a beginner at Apps Script, or even just trying to do Apps Script the very first time, ah, or even if you've been doing Apps Script for a while and copying and pasting, ah, Apps Scripts into your Apps Script, the code into your Apps Script, this may not be a very simple concept to you, 
0:46 or you may not understand that it could occur, that you could do this. We're just going to basically line up the two emails, one first, and then under it, the second one.
0:56 And I'll go through a few of the issues that you may run into because. I just ran into this with a client.
1:03 They asked, is this even possible? I was like, I didn't know, you didn't know this was possible. And it was, ah, done within minutes, not hours.
1:12 So we're going to go up to Extensions, Apps Script and create an Apps Script here. We want to send two emails of different data.
1:20 We have, say, a total sales and a monthly sales. And we're sending this every single Friday, just an update to two different people.
1:29 So let's create a name for this, sales updates. We're going to create a function that will be triggered as a timed trigger.
1:38 So we're going to say send email. We could say send emails here, or just send email. Maybe you only set up one, and now you want to create another one.
1:46 Ah, and then we will get the data, which will be variable data to send equals spreadsheet app dot get Spreadsheet.
2:00 Get sheet by name, and in one case we want to get the total. Get range. We want, here, A1 to B6.
2:17 A1 to B6. Get values. And we want to send that as a body of a text, ah, as an email, uh, text.
2:27 So we will start gmail app dot get actually. Send email. Here we just need three things. We need the recipient, who it's to.
2:36 So in my case, I'm going to send it to myself, actually. Owner, variable owner equals. Get active spreadsheet. Get owner. Get email.
2:52 So if you want to put your email in here, you just have to change this to text or whoever you're sending it to and we'll do this, the subject will be, uh, total updates, total sales updates.
3:09 What I'm going to do at the end of this is add a timestamp plus new date. And then we need the body.
3:20 This is the body or the text of the email. Now we could potentially just send data to send And let's see what that looks like, because that might not be very nice.
3:34 Let's make sure it saves. We are going to click run. We'll have to authorize it the very first time we run it before we create that trigger.
3:46 As we can see, very simply, it doesn't look very nice, so I'm going to actually create a little bit of HTML here.
3:53 So instead of HTML, I actually just created a plain text with this slash n for new lines and instead of a data to send, we're going to do this.
4:01 The text body. So let's see what that looks like. And that's a pretty fairly simple text email. Here, uh, we have this.
4:12 Uh, we will now send a second email. So this is sending one email to one person. It's this Gmail app here, dot send email, that will actually send the email.
4:26 So what you might think, if we create this as a trigger, let's, uh, go and do that, send email. Triggers.
4:34 Let's move my face over here, add trigger, choose which function, send email. Event source will be time driven. We will send this every week, every Friday at 5pm.
4:48 There we go. And I will click save. So once you've created that trigger, you think, you may think, that I need to create a whole nother function, and create a whole nother trigger.
5:00 In order to do this. But what we can do with our program is we are already sending this. All we need to do is send another email in here.
5:10 So inside of this function, we're just going to copy this send email and we'll redo everything. So instead of total sales, we'll call this monthly sales update.
5:22 And instead of text body, what I want to do is copy all of this and put it just between here.
5:31 In our case, we are going to be sending it to the same person ourselves just for, uh, to see it, but you can always send it to a different person here.
5:41 But this data to send will be called monthly. This is what I like to do. I like to create new variables that have a different text in them.
5:51 And we're going to call this monthly. And this has exactly, by the way, the same numbers here, or not numbers, but the same area. We're going to, we just have these formulas that are looking at whatever today's date is, putting the text of the month there, this month, and then counting whatever this 
6:11 month's counts are up to date. These are actually fun, pretty, pretty fun formulas. Alright, so we have the same area and we want to rename this.
6:25 To rename this very easily, I will use command D to select all of those text bodies. And make sure I'm at the end of each one and call this monthly.
6:38 Data to send monthly will be updated here as well. There, so we've updated our variables so that our variables above this for this total email are one.
6:51 And then we have different variables for the monthly. And that's how we can create these two emails and send two emails in one function and only use one trigger.
7:06 So let's see. Now I will, uh, hit run again to test this out. And now we have two different emails.
7:15 We have total sales emails and monthly sales updates. We do need to update the text here. This should be monthly.
7:25 It's always good to check. And there we go. So, that's how you can create two emails or send two emails with one function, in one trigger, without having to create another trigger, without having to create a whole nother function.
7:38 We can run this function, send email, with those two emails. Again, this is not very intuitive if you are a beginner at Apps Script, but I wanted to share this with you, at least this fairly simple concept, uh, because it did come up actually recently and thought it would be very helpful for you.
7:58 If you If are watching this on bettersheets.co, you can get this. Spreadsheet down below. If you're watching this somewhere else, become a member of bettersheets.co to get that spreadsheet.
8:07 You can get the sheet, and you can get the Apps Script as well, because there's some actually pretty funny, funky, interesting formulas here if you're looking for, like, to see what text, uh, is to date this month's, uh, this month's name in text instead of a number, we can use this formula here.
8:25 I thought that'd be interesting for you. Watch other videos here on bettersheets.co or on youtube.com slash better sheets.