- Home /
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.
@ina This can also help you > Opening URL and send Email from Unity
For complex actions, Cross Platform Native Plugins allows option to send nicely formatted HT$$anonymous$$L body and attachments. This plugin is free to use and supports iOS and Android.
Answer by cregox · Dec 04, 2013 at 06:42 PM
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\r\nFull 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.
(PS I "think" you can also use Uri.EscapeDataString(body); .. not sure)
PS : you can leav string email = "";
empty and the user will decide later
In case someone wants to make function as tiny as possible:
public static void SendEmail(string email, string subject, string body) =>
Application.OpenURL("mailto:{0}?subject={1}&body={2}".F(email, subject.$$anonymous$$yEscapeURL(), body.$$anonymous$$yEscapeURL()));
static string $$anonymous$$yEscapeURL(this string url) => UnityWebRequest.EscapeURL(url).Replace("+", "%20");
And then you can call it like
ExternalCommunication.SendEmail("somebody@gmail.com", "About stuff",
"Something to talk about");
2019: It seems it doesn't work on the latest unity 2019 1 4 1 Have tried on iOS and Android. The xcode log writes Filtering mail sheet accounts for bundle ID: com.xxx.xxx, source account management: 1 2019-05-31 20:56:42.158166+0300 appname[3637:1425152] [$$anonymous$$ailShare] can send mail: 1 mail send
And nothing happend
Answer by David · May 06, 2011 at 05:14 PM
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);
}
There is nothing iPhone-specific about these schemes. They work fine with Android (and web, and standalone).
hey
Application.OpenURL("mailto:" + email + "?subject=" + subject + "&body=" + body); }
not :, correct is =
It's not a very well written answer and looks even more like it should be a comment to david's answer, but it's correct ;)
This answer worked for me:
http://answers.unity3d.com/questions/341147/encode-string-to-url-friendly-format.html
Basically you use WWW.EscapeURL, then you replace "+" with "%20". Worked like a charm!
$$anonymous$$y application went to suspend mode.
Once i send the email, native email setup should off! How it is possible to restrict to Go suspend mode ?
Answer by slake_it · Oct 29, 2015 at 09:55 AM
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");
}
}
Don't take my word for it, but something about making "static functions for easy usage" rubs me the wrong way and re$$anonymous$$ds me of this: http://etodd.io/2015/09/28/one-weird-trick-better-code/
$$anonymous$$aybe, and just maybe, you rather use the Toolbox pattern design for doing easy to use global access things like this: http://programmers.stackexchange.com/a/218322/4261
I think it's just important to be aware of what static
means, what it should be used for and I'm pretty confident using it as globals is not it. That being said, do go ahead and make whatever works! ;P
Answer by Yuriy-Ivanov · Jun 18, 2017 at 06:56 PM
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.
Your answer
Follow this Question
Related Questions
Sending Mobile Email via Unity 2 Answers
Optimal # of Faces for many mesh scene for mobile 1 Answer
Help improving performance with 40-80 rigid bodies. 1 Answer
Joystick trouble... 0 Answers
Unity Cloth on Mobile? 2 Answers