How To Send Two Emails in Same Trigger in Apps Script

Learn how to send two emails in the same trigger using Google Apps Script, a simple yet effective method for automating email updates to different recipients on a scheduled basis.

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 you would maybe have to send two emails to do to two different people and maybe on a weekly basis on a weekly trigger . We're going to set up the Apps Script . And I want to stress to you that this is a fairly simple, Idea, but it is not intuitive. If you are a beginner at Apps Script or even just trying to do Apps Script the very first time or even if you've been doing Apps Script for a while and copying and pasting Apps Scripts into your Apps Script , the code into your Apps Script , this may not be a very simple concept to you, or you may not understand that it could occur, that you could do this. How To Send Two Emails in Same Trigger Simply Email Yourself From a Google Sheet How To Send Two Emails in Same Trigger Email When Cell Changes

We're just going to basically line up the two emails. One first, and then under it, the second one. And I'll go through a few of the issues that you may run into because I just ran into this with a client. They asked, is this even possible? I was like, I didn't know, you didn't know this was possible. And it was done within minutes, not hours. So we're going to go up to Extensions, Apps Script , and create an Apps Script here. Okay. We want to send two emails of different data . 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. 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 .

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. And then we will get the data , which will be variable data to send equals spreadsheet app dot get active spreadsheet , get sheet by name, and one case we want to get the total get range . We want here a one to be six. A one to B six get values and we wanna send that as a body of a text as an email a text.

So we will start Gmail app.gi. Actually send email here where you just need three things. We need the recipient who it's to. So in my case I'm gonna send it to myself. Actually owner , variable owner equals get. Active spreadsheet , get owner , get email. 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 total updates, total sales updates. What I'm going to do at the end of this is add a timestamp .

plus new date. And then we would need the body. 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. 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 . As we can see, very simply, it doesn't look very nice, so I'm gonna actually create a little bit of HTML here. So instead of HTML, I actually just created a plain text with this slash n for new lines, and instead of a dated send, we're gonna do the text body. So let's see what that looks like. And that's a pretty fairly simple text email.

Here we have This 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. So what you might think, if we create this as a trigger , let's go and do that, send email. Triggers. 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. 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 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. So inside of this function , we're just going to copy this send email and we'll read. Do everything. So instead of Total Sales, we'll call this Monthly Sales Update. And instead of Text Body, what I want to do is copy all of this and put it just between here. In our case, we are going to be sending it to the same person ourselves just for to see it. But you can always send it to a different person here. But this data to send will be called monthly.

This is what I like to do, I like to create new variables that have different text in them. 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 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 month's counts are up to date. These are actually fun, pretty fun formulas. Alright, so we have the same area, and we want to rename this. 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.

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. 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 . So let's see. Now I will hit run again to test this out. And now we have two different emails. We have total sales, emails, and monthly sales updates. We do need to update the text here. This should be monthly. 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 other function , 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 because it did come up actually recently, and thought it would be very helpful for you. If you're watching this on BetterSheets. co, you can get the spreadsheet down below. If you're watching this somewhere else, become a member of BetterSheets. co to get the spreadsheet. You can get the sheet, and you can get the Apps Script as well. Cause there's some actually pretty funny funky, interesting formulas here.

If you're looking for to see what text is to date this month's text this month's name in text instead of a number , we can use this. Formula here. Thought that would be interesting for you. Watch other videos here on bettersheets. co or on youtube. com slash bettersheets. Bye.