Introduction / Motivation
Here are some reasons why you might consider sending emails from your game:
- Setup two-factor authentication
- Enable players to give you feedback or bug reports
- Secretly record error messages, crash logs, or suspicious player activity
For these purposes and more, we shall explore two methods of sending an email...
Overview of "Method 1: Direct message"
This method will require us to setup a Gmail account from which we can send an email using .Net libraries. This is the best, fastest, method for sending emails. However, this method will not work on games that are hosted online such as the WebGL target platform (for example a game hosted in-browser on itch.io).
Overview of "Method 2: Server request"
This method involves sending an email to a server script which can then forward our data to the destination address. This is a slower method and we face a greater risk of Gmail labeling our messages as spam. However, this method will work on all platforms.
I have created a simple Unity scene for testing which includes an input field, a button, and the script we will be writing. The script has the text of the input field and the button linked to it as Serialized Fields. There is also a checkbox we can toggle to switch between methods of delivery.
Implementing "Method 1: Direct message"
First we need to create a Gmail account. I highly recommend creating a new one explicitly for this purpose because we will need to include both the username and password of this account in the source code. A naughty hacker could, in theory, get access to the code so please be careful what you include there.
With our Gmail setup, we can now start defining the code for our Emailer.cs file. Some code is preceeded by the comment "Method 2" to let you know it will not be needed for this first approach. Mind the import statements here which include the .Net libraries we will need to verify our email credentials. Of course, make sure you replace my email credentials with yours.
By using the .Net support for emailing along with the Gmail we created, the above method SendAnEmail will reliably get our message where it needs to go! However, a limitation of this method is that it will not work on WebGL games. For these cases, we need to consider an alternative approach.
Implementing "Method 2: Server request"
Method 2 will allow us to send a WWW request to a server-hosted php script that can forward our data. While it is not as fast or reliable as Method 1, it does work on all platforms! The C# side of this approach is included in the code of Emailer.cs (above) and includes creating a WWWForm with the data we want to send, then forwarding it to our server. With that aspect completed, all that is left for us to do is create and host our server script! Let's setup the hosting first.
I used the free hosting website https://www.000webhost.com.
Once you have a server host setup you can use the code of "Emailer.php" (below) to receive the form we sent from our C# code. This code ensures that it is receiving a form with the necessary data before sending an email. The headers include data which is meant to suppress Gmail's spam filters allowing your message to reach an inbox as desired (however in my test, it did get stuck in a spam folder at first).
These are only two of many approaches you may consider for sending emails in your games. Please consider "liking" this post or commenting on which aspects were most helpful / confusing. Your readership is very much appreciated!