The Imperfect ‘Send Email’ Action in Android

Looking at issues across devices using the create email intent

Elye
Elye
Nov 4 · 3 min read
Picture by Yogas Design on Unsplash

Sending email is an essential part of any app. In even the simplest app, sending an email can be used to provide feedback on your app. Android provides the capability using Intent Action.

However, the various approaches provided in creating email Intent have differing issues across different devices. They are not showstopper issues but could be irritating if we want a fully polished app. Knowing them can help us see how they could be mitigated.


Testing Scenario

Before proceeding, below are my test scenarios.

  1. Pixel 2 SDK 8.1 — first launch
  2. Pixel 2 SDK 8.1 — background/foreground the email app (Don’t keep activity)
  3. Samsung Galaxy S7 Edge SDK 7.0 — first launch
  4. Samsung Galaxy S7 Edge SDK 7.0 — background/foreground the email app (Don’t keep activity)

For the first three approaches, I use the text below.

val addresses = listOf(
"fake@address.com", "some@add.com").toTypedArray()
val subject = "Some title"
val text = "Some message\n\nNew Line\n\n"

For the last approach, I use the text below.

val addresses = listOf(
"fake@address.com", "some@add.com").toTypedArray()
val subject = "Some title"
val text = "Some message<br><br>New Line<br><br>"

Approach 1. Intent Data + Extra

private fun mailtoTypeEmailCreation(
addresses: Array<String>, subject: String, text: String) {
val intent = Intent(Intent.ACTION_SENDTO).apply {
val mailto = "mailto:" + addresses.joinToString(",")
data = Uri.parse(mailto)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, text)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}

In this approach, we use mailto in the intent.data together with the emails. The content uses EXTRA_SUBJECT and EXTRA_TEXT.

This looks good only for Pixel on its first email launch. It doesn’t work when using onRestore. It doesn’t work for Samsung at all.


Approach 2. All Intent Extras

private fun latestExampleEmailCreation(
addresses: Array<String>, subject: String, text: String) {
val intent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, text)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}

This is the official approach shared in the Android developer guide.

This looks good only for both Pixel and Samsung first launch. onRestore works for Samsung. But on Pixel Gmail onRestore, the Subject and Message went missing.


Approach 3. All Intent Data

private fun intentDataEmailCreation(
addresses: Array<String>, subject: String, text: String) {
val intent = Intent(Intent.ACTION_SENDTO).apply {
type = "message/rfc822"
val uriText = String.format("mailto:%s?subject=%s&body=%s",
addresses.joinToString(","), subject, text)
data = Uri.parse(uriText)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}

This makes full use of syntax of Mailto, as per https://www.labnol.org/internet/email/learn-mailto-syntax/6748/

On the Pixel, this is perfect as both first launch and onRestore all works. But it doesn’t work on Samsung’s Gmail, as the /n is ignored


Approach 4. All Intent Data With /n Replaced by <br>

This is the same as Approach 3., with the exception of using HTML format of syntax (i.e. replacing /n with <br>).

With this approach, it all works well on Samsung. However on Pixel the <br> is preserved and doesn’t make a new line.


So there’s no perfect solution as you could see above. The best is Approach 2., where the only problem is the Pixel email onRestore doesn’t preserve the Subject and Message. It’s the Gmail app issue perhaps, so let Gmail fix it instead.

You can get the code on GitHub.


Thanks for reading.

Better Programming

Advice for programmers.

Elye

Written by

Elye

Learning and Sharing Android and iOS Development

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade