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: local ScriptId = "" --paste the ScriptId of your web app URL herelocal Url = - Pastebin.com
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.
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!