Unity - Send Email From Your Game



If you have developed a game and is in the Playstore, you might want to let your users contact you, be it for suggestions, feedback, ideas or whatever. Now, the question is How do we allow our users contact us?
Display our Email Id? Well, the chances of getting the feedback/suggestions/ideas with this is very less. 

How do we do this then?
Well, we will just add a Unity button and on clicking this button, it would navigate to the user's default Mail App. 

Create a New Scene and add a new Unity Button in the scene. Add an OnClick listener to it. 

Create a new C# Script and name it as SendMail. Attach this script to Main Camera. Open the script and paste the below code.

using UnityEngine;
using System.Collections;

public class SendMail : MonoBehaviour {

 public void EmailUs () 
 {
  //email Id to send the mail to
  string email = "admin@thegamecontriver.com";
  //subject of the mail
  string subject = MyEscapeURL("FEEDBACK/SUGGESTION");
  //body of the mail which consists of Device Model and its Operating System
  string body = MyEscapeURL("Please Enter your message here\n\n\n\n" +
   "________" +
   "\n\nPlease Do Not Modify This\n\n" +
   "Model: "+SystemInfo.deviceModel+"\n\n"+
      "OS: "+SystemInfo.operatingSystem+"\n\n" +
   "________");
  //Open the Default Mail App
  Application.OpenURL ("mailto:" + email + "?subject=" + subject + "&body=" + body);
 }  

 string MyEscapeURL (string url) 
 {
  return WWW.EscapeURL(url).Replace("+","%20");
 }
}


Save the script and move to Unity. To the OnClick listener added to the button. Add the Main Camera in the Gameobject field, SendMail.EmailUs in the Function field.


Unity - Send Email From App



Once you save the scene and build it. Run it on your device and on clicking the Send Mail Button, you should be navigated to your Mail App. The screenshot demonstrates the gist of this post

Unity - Send Email From Game

See you around.
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

9 comments:

  1. Looks great, but I get a parser error (Unexpected symbol '<') on line 20.
    Coding is not my strong point. Can you help?

    ReplyDelete
  2. Very clear, works fine. Thanks!

    ReplyDelete
  3. Nice approach, better than using SmtpClient for sure.

    ReplyDelete
  4. Hi, Thank you, It´s been very helpful. I would like to know how can attach a file in the mail?

    ReplyDelete