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:
- You CANNOT ask a player for their email adress to send the email to them (that’s a no-no on roblox)
- 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)
*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
- 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)
- 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
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
0 voters