Application.OpenURL for email on mobile

Does Application.OpenURL("mailto:address@domain.com") work for popping up a native email prompt on mobile? doco

It’s well worth noting that if you just want to send a very simple one with NO dynamic text, no body, no unusual characters, just replace the spaces with %20 manually like so:

public void LinkHelpEMAIL()
 {
 string t =
 "mailto:blah@blah.com?subject=Question%20on%20Awesome%20Game";
 Application.OpenURL(t);
 }

exactly like that (do not escape the at symbol). saves typing, tested on both.

I’ll just organize in one post what’s already written around…

Yes, it does work! But it has to be sanitized or else it will crash silently.

void SendEmail ()
{
 string email = "me@example.com";
 string subject = MyEscapeURL("My Subject");
 string body = MyEscapeURL("My Body

Full of non-escaped chars");

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

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

This works both on iOS and Android.

Note, this explanatory example code, does not literally work, as you must escape certain strings. Teaching code only.


Yes. Using Application.OpenURL() you could open safari, maps, phone, sms, mail, youtube, itunes, app store and books store. You could find all the url schemes you could need here These are for Objective-C but if you get the idea of the url scheme it would be really easy to do it on Unity. Here you have an example of a simple script to send a mail with address, subject and body.

C# mail example:

 public string email;
public string subject;
public string body;
// Use this for initialisation .. EXAMPLE ONLY, DOES NOT WORK
void Start () {
    Application.OpenURL("mailto:" + email + "?subject:" + subject + "&body:" + body);
}

this is the same code of @cawas created as a class with static functions for easy usage

using UnityEngine;

/// <summary>
/// - opens the mail app on android, ios & windows (tested on windows)
/// </summary>
public class SendMailCrossPlatform
{
	

	/// <param name="email"> myMail@something.com</param>
	/// <param name="subject">my subject</param>
	/// <param name="body">my body</param>
	public static void SendEmail (string email,string subject,string body)
	{
		subject = MyEscapeURL(subject);
		body = MyEscapeURL(body);
		Application.OpenURL("mailto:" + email + "?subject=" + subject + "&body=" + body);
	}

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

Please note that there is no reliable way to add any attachments with mailto, you’ll have to use underlying platforms email API with native plugins to achieve that.

We’ve recently published a new asset UTMail - Email Composition and Sending Plugin, which supports composing emails with attachments and also sending them directly with SMTP. It works on multiple platforms and provides a convenient API.

Best regards,

Yuriy, Universal Tools team.