Visit us on www.bitwisebranding.com for Branding, Digital Marketing and Design Solutions.

Send Email From Your Game - Unity3D

S
By Neel Gajjar - Unity Developer In Unity Development

In your game, it might be possible that user directly wants to send an email to feedback or any suggestions to game support.

For that, there is a simple code implementation,


Attach the SendMailClass script to your active game object & use sendEMailToSingleRecipient() for sending email to single recipient.

Use sendEMailToMultypleRecipient() to send multiple recipient.

In sendEMailToMultypleRecipient() we are using an array of email id which is a string.


Code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class SendMailClass : MonoBehaviour {


   //recipient's email id

    string email_string = "abc@bitwiseonline.com";

    //recipient's email id array

    string[] email_string_array = new string[]{"abc@bitwiseonline.com","xyz@bitwiseonline.com"};

    //email subject

    string subject_string = "Your subject text";

    //email body

    string body_string = "Your body text";


   //for sending mail to single recipient

    public void sendEMailToSingleRecipient(){        

       string email = email_string;

        string subject = EscapeURLFunction(subject_string);

        string body = EscapeURLFunction(body_string);

        //Open the native default app

        Application.OpenURL ("mailto:" + email + "?subject=" + subject + "&body=" + body);

    }


   //for sending mail to multiple recipient

    public void sendEMailToMultypleRecipient(){        

       string email = "";

        foreach (string email_str in email_string_array) {

            email += email_str + ",";

        }

        string subject = EscapeURLFunction(subject_string);

        string body = EscapeURLFunction(body_string);

        //Open the native default app

        Application.OpenURL ("mailto:" + email + "?subject=" + subject + "&body=" + body);

    }


   string EscapeURLFunction (string url){

        return WWW.EscapeURL(url).Replace("+","%20");

    }


   void OnGUI() {

        if (GUI.Button (new Rect (10, 10, 250, 50), "Send mail to single recipient"))

            sendEMailToSingleRecipient ();

        else if (GUI.Button (new Rect (10, 70, 250, 50), "Send mail to multiple recipient"))

            sendEMailToMultypleRecipient ();

    }

}


This code will be useful for Android & iOS game.

Also, see the attachments for APK & Project sample.

Leave us a comment