How To Make Send An Email From A Game

Hey guys! So this tutorial will show you how to send an email from a roblox game. Now before we get into this, let’s clarify a few things:

  1. You CANNOT ask a player for their email adress to send the email to them (that’s a no-no on roblox)
  2. Although you can use this for whatever you imagine (as long as it doesn’t have to do with the thing above), this tutorial shows how to do this with the intended use case of a feedback system.

Ok now lets get into it!

First things first we need an API,
I’ll be using

Steps to set up this API

First, log in/make an account for RapidAPI, next you wanna go to that link and click on the Pricing tab:


From there choose your plan, I chose the free tier just note that it has a Hard Limit of 50 emails/requests per day

Now go back to the Endpoints tab and just leave it open, we’ll go back to it!

Ok let’s start getting this code going,

First for the Setup we need a Remote Event in ReplicatedStorage, I’ll call it SendFeedback. Then you wanna publish the game (if not published already) and then go to the settings and turn on Allow Access to Remote Servers. Now time for the script in replicated storage,

First we will get our services, we only need HttpService (to send the API request and encode the data in json) and Replicated Storage (to get the remote event)

local HttpService = game:GetService("HttpService") -- Get the http service so we can make calls to the api
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Next we’ll grab the Remote Event:

local SendFeedbackEvent = ReplicatedStorage.SendFeedback

Ok, now we need the API URL, for me (and you if you used the same API):

local API_URL = "https://rapidprod-sendgrid-v1.p.rapidapi.com/mail/send"

Okay for this part we need the Headers and you’ll need to go to your API and grab the API-Key and the API-Host thingy:

KEEP THE API KEY AND HOST TO YOURSELF!!! DO NOT SHARE

local Headers = {
	['X-RapidAPI-Key'] = "Your Key Here", -- Replace
	['X-RapidAPI-Host'] = "Your Host Here" -- Replace
}

Next we will make a function that will connect to the event, the function will take in 3 parameters, the player (automatically passed in by roblox), the Title/Subject of the Email, and the body/contents of the email. We will also set up our payload which is just the data we pass in to the API, things such as who the email is from, who it’s sent to, the subject and the body is included in the payload:

local function SendFeedback(Player, Title, Feedback)
	local payload = {
		["personalizations"] = {
			{
				["to"] = {{["email"] = "yourrecievingemail@here.com"}}, -- Add the email to recieve the emails 
				["subject"] = Title -- The Title of the email the user sent (using the parameter of the remote event)
			}
		},
		["from"] = {["email"] = "yoursendingemail@here.com"}, -- Add your sending email here
		["content"] = {
			{
				["type"] = "text/plain",
				["value"] = Feedback -- This is the feedback the user gave (taking it from the remote event parameter)
			}
		}
	}

One more thing before we actually send the request, we need to encode the payload into JSON:

    local data = HttpService:JSONEncode(payload) -- This is for the API to be able to use

Finally we send the request to the API and exit the function:

    HttpService:PostAsync(API_URL, data, Enum.HttpContentType.ApplicationJson, false, Headers)
end

Last thing to do is connect the event:

SendFeedbackEvent.OnServerEvent:Connect(SendFeedback)

The final code for this:

local HttpService = game:GetService("HttpService") -- Get the http service so we can make calls to the api
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SendFeedbackEvent = ReplicatedStorage.SendFeedback

local API_URL = "https://rapidprod-sendgrid-v1.p.rapidapi.com/mail/send"

local Headers = {
	['X-RapidAPI-Key'] = "Your Key Here", -- Replace
	['X-RapidAPI-Host'] = "Your Host Here" -- Replace
}

local function SendFeedback(Player, Title, Feedback)
	local payload = {
		["personalizations"] = {
			{
				["to"] = {{["email"] = "yourrecievingemail@here.com"}}, -- Add the email to recieve the emails 
				["subject"] = Title -- The Title of the email the user sent (using the parameter of the remote event)
			}
		},
		["from"] = {["email"] = "yoursendingemail@here.com"}, -- Add your sending email here
		["content"] = {
			{
				["type"] = "text/plain",
				["value"] = Feedback -- This is the feedback the user gave (taking it from the remote event parameter)
			}
		}
	}
    local data = HttpService:JSONEncode(payload)
    HttpService:PostAsync(API_URL, data, Enum.HttpContentType.ApplicationJson, false, Headers)
end

SendFeedbackEvent.OnServerEvent:Connect(SendFeedback)

Alright so now to send an email, all you have to do is call this remote event. I will continue with the feedback system example by making a simple UI, mine will contain just 45things, the Frame, 2 Text boxes (1 for the Title/Subject, the other is for the Body/Contents), and a button to send the email (and a Local Script inside of it)
Screen Shot 2022-11-04 at 10.20.38 PM
*The Local Script:

local RS = game:GetService('ReplicatedStorage')
local Event = RS.SendFeedback
local FeedbackFrame = script.Parent.Parent

script.Parent.MouseButton1Click:Connect(function()
	Event:FireServer(FeedbackFrame.Title.Text, FeedbackFrame.Feedback.Text)
end)

I don’t think I need to explain this much, basically whenever the button is pressed, fire the remote even and pass in the entered data.

Important Recommendations

  1. If you are making a sort of feedback system or any system where users control the email, put a timer on the user and only let them send like one a day (especially if you have a low daily limit like 50)
  2. Once again if you are making any sort of system where users control the email/when it’s sent, you should probably make a timer that says the user has to have a certain amount of play time overall in the game to prevent alt accounts and bots spamming the email and using all the API calls
How Was the Tutorial
  • 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

0 voters

5 Likes

I will use it in the future, thanks. This is duplicate?


I’m not sure what you mean by this.

I ask if the text is duplicated by error, since the same text appears in 2 parts.

1 Like

Honestly, RbxGmail, a post made over 5 years ago, is better. It’s also more reliable, shorter, and easier to follow. It’s also officially by Google, and not some shady RapidAPI REST api.

1 Like

The API is perfectly fine to use,

Cool, I had no idea that existed lol.

1 Like

Oh, thanks for telling me, this happened because when I was putting the poll in, I accidentally clicked the quote button, I remove the quote tags but didn’t realize it duplicated the post!

5 years ago would be considered outdated and I think that would reasonably provoke a new updated method to do this.

Do you have any sources for this? (Couldn’t find any posts on search)

Again, do you have any sources for this?

Restful API is an interface constraint for exchanging data over a HTTP request (in the web-end) where requests are stateless. From my experience with managing external databases, Firebase (owned by Google), Supabase (OSS-alternative to Firebase) and Parse (another OSS object storage service)

I’m not sure why you’re bashing them without providing any sources to back-up your information or feedback for improvement.


@domboss37 Might be important to mention that the API Key and Host should be kept private to yourself only. Would also recommend truncating the player’s text to a certain amount (1,000 characters maximum as an example) so that the JSON message sent isn’t too big and also so you’re not getting a flash flood in your inbox the next day after you set it up :wink:.

2 Likes

Thanks for your feedback, i can’t believe I forgot to mention to keep it private (even after blocking mine out), also great idea with the content size cap

Fair, but it works great and isn’t considered deprecated by Google. Anyway, RapidAPI is the same age, if not older.

I think you forgot what an “opinion” is. Anyway, it is shorter and easier to follow if you just read it. And, unlike a RapidAPI that can be taken down at any time, Google Scripts can only be taken down by you.

I don’t hate REST apis as a whole. In fact I use them often. It simply seems that you misunderstood that this was just a noun to say what it is, not particularly because they’re restful. By the way i use firebase too

Keep in mind that it’s made by Google and Google themselves own Gmail and thus it is much more reliable, safe, and trustworthy.

1 Like

“Much more safe”. I wouldn’t say that, the Rapid API is safe aswell.