RobloxGmail - Send Emails through Gmail using a Google Apps Script Web App

In this tutorial, I’m going to show you how to send emails from a roblox script, using Google Apps Script. The code for this is extremely simple, but I thought I would make this post since I saw a lack of open source material for sending emails.

I used Google Apps Script to create this, for two main reasons:

  • A. Since Google Apps Script is already a Google program, it has a sendEmail from Gmail function already ingrained , making it extremely easy to send Emails.
  • B. Google Apps Script is completely free, unlike setting up a decent web server can be, and works consistently with a max of 100 Emails a day with a non-paid account.

Without further ado, lets hop right into it.


Creating the Google Scripts App Web App:

Step 1: Setting Up Your Project File Inside of Google Scripts App

The first step is to create a new project inside of Google Scripts App, to run the JavaScript that handles the sending of the email. Log into the Google account that you want to use to send you emails( I recommend you set up an alternate account specifically for sending bot emails, so that people can’t impersonate you if they get hold of your ScriptId.). This will be the email address that all of the emails you send come from. Then go to https://script.google.com/home, and click “New project”.


This should open up a new project, that displays a default function:

Name the project as you like in the top left hand corner.

You are now ready to insert the JavaScript code.

Step 2: Paste in the JavaScript Code Into the Project

Remove the default code inside the script editor, and replace it with this fairly simple code:

Pastebin: https://pastebin.com/pTqGaRk3

Source:

function doGet(e) {

    var data = e.parameter["q"];
    data = JSON.parse(data)

    var emailAddress = data[0]
    var subject = data[1]
    var message = data[2]

    try {
        MailApp.sendEmail(emailAddress, subject, message);
        return ContentService.createTextOutput(true);
    } catch (error) {
        return ContentService.createTextOutput(error.message);
    };

};


The reason this code is as simple as it is, is because since Google Scripts App is a google application, it has the built in service MailApp that is specifically made for sending emails.

Step 3: Publish the code as a web app
Click “Publish”, then “Deploy as web app”.
Now you just have to Deploy your code so that it can be called on by your lua Script.


Once the deploy box pops up, set “Who has access to the app”, to “Anyone, even anonymous”.

Then click “Deploy”.

Since this program will have control over sending emails, you may be asked to give it permissions.
image
Click “Review permissions”.

Log into the appropriate Google account, and when the authorization box pops up, click “Advanced”, then click “Go to your app name (unsafe)”.



Then click Allow and the app will be published.

Then when the published page appears, copy the ScriptId, which is between /macros/s/ and /exec. You will need it for later.

Step 4: Set Up the Roblox End
Open Roblox Studio,and then either create a ModuleScript with the code below, or grab this module here.

local ScriptId = "" --paste the ScriptId of your web app URL here

local Url = "https://script.google.com/macros/s/" .. ScriptId .. "/exec" --creating the url from the ScriptId
local HttpService = game:GetService("HttpService")

local Module = {}

function Module:SendEmail(Email, Subject, Message) --Email address of the recipient, Subject of message, Body of message
	local Success, ErrorStatement  = pcall(function()
		Result = HttpService:GetAsync(Url .. "?q="..game.HttpService:JSONEncode({Email, Subject, Message})) --Result will either return true if the Email was sent successfully, or an error message string explaining what went wrong, if it failed to send.
	end)
	if Success then
	    return Result
	else
		return ErrorStatement
	end
end

return Module

Replace the ScriptId variable’s value in the ModuleScript, with a string of the ScriptId you copied earlier from your Google Apps Script project, and you are good to go!

Example Code:

Once you have finished setting everything up, it’s time to actually send an email.

local RobloxGmail = require(game.ServerStorage.RobloxGmail)

local Output = RobloxGmail:SendEmail("exampleemail@gmail.com","subject text","body text")  --Email address of the recipient, Subject of message, Body of message

print(Output)

This code will send an email through the Gmail account that the Google Apps Script program is created in. The SendEmail function returns true if the email was sent successfully, otherwise returning a string contain the error.


The downside to this method being free, and easy to set up and configure, is that without paying Google only gives you 100 email recipients per day. You can view the quotas for Google Apps Script here.

This tutorial might feel like it was too simple to make into a instructional post, but I think that it can be very useful for a variety of things( feedback reports, reporting game data, ect. ).

I hope this helps a lot of people out!

71 Likes

Wow this is incredible! I didn’t even know stuff like this was possible. Definitely bookmarking for future projects.

6 Likes

This is a tutorial that even beginners can do!

2 Likes

I have a question, are we allowed to ask players for their emails in-game? For some reason it sounds to me like it’s against TOS to do that.

5 Likes

I’m pretty sure. I believe that qualifies as private information.

1 Like

Hmm, then how else can we acquire the player’s email so we can send an email to them? Is this tutorial meant to be used in-game or is it more of a “tech demo” sort of thing?

Edit: Oh wait, I misinterpreted the tutorial. I thought that this was supposed to be used so players can email each other or so the developer can email players. Oops :joy:

1 Like

I believe that this was made as a thing you can use in-game, to get statistics and bug reports on your game.

Not to interact with players.

3 Likes

Whats even the use for robloxGmail? Other then sending error reports.

1 Like

Anything. All it is is an API for Gmail, so you could use it however your imagination decides.

2 Likes

As previously said, a lot. You can for example notify yourself whenever a certain player joins, when someone spends X or more Robux, when your game reaches X amount of players, when moderators in your game (if any) report issues to you, feedback forms.

You can also tie this up with a service like pushover.net, however, they have a web API themselves, so it’d be easier and less costly to use that one over email, but the option is there, at least.

1 Like

Consider setting up some kind of authentication, all it takes is a wrong person to find your ScriptId for it to be abused.

1 Like

This is a very good idea. I almost included some kind of verification system in the tutorial, but decided to leave this for each individual to add if they want. I didn’t want to overcomplicate the tutorial.

Yes, the purpose behind this tutorial, was not for it to be used to make a chat or messaging system, but for smaller scale tasks like analytics, or notification. Of course, I’m sure there are many possibilities for things you can create with a system like this, but that was my thinking.

1 Like

I’m glad you think it’s easy to follow! My mission while creating this, was to enable even beginner programmers to be able to send emails. Normally, without Google Apps Script, accomplishing a task like this would be a little tricky, and require you to set up a web server.

i hope scam games dont start using this but its cool anyways.

4 Likes

I know this is off topic, but do you take commissions? I’m trying to make a discord Roblox Status bot.

Unfortunately, I’m not accepting commissions at the moment. However, if you DM me, I could give you some pointers in the right direction for making it yourself.

This is very cool! I have some ideas of how I could use for this… >:)

Hey, great tutorial! I am trying to use it for a feedback chat system, and it gets all the way to the end, then says that it is not found.Screen Shot 2020-10-05 at 1.46.05 PM It takes me to the line where I print the output. I am not getting any emails either. sorry for the bump :slight_smile:

If I understood correctly, you said that that print was being generated from the result of the GetAsync().That probably has something to do with what you are sending to the Google Apps Script program, or a issue with the Google Apps Script program itself.

If this is being returned from the pcall in your roblox code on the other hand, then its most likely a problem with the requesting.

Would you be able to DM the code you are using on both ends of your program, so I could try to help you out?