diff --git a/android/src/main/java/com/chirag/RNMail/RNMailModule.java b/android/src/main/java/com/chirag/RNMail/RNMailModule.java index 613e1bc..fc32b30 100644 --- a/android/src/main/java/com/chirag/RNMail/RNMailModule.java +++ b/android/src/main/java/com/chirag/RNMail/RNMailModule.java @@ -6,15 +6,17 @@ import android.net.Uri; import android.text.Html; +import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReadableMap; -import java.util.List; import java.io.File; +import java.util.ArrayList; +import java.util.List; + /** * NativeModule that allows JS to open emails sending apps chooser. @@ -34,12 +36,11 @@ public String getName() { } /** - * Converts a ReadableArray to a String array - * - * @param r the ReadableArray instance to convert - * - * @return array of strings - */ + * Converts a ReadableArray to a String array + * + * @param r the ReadableArray instance to convert + * @return array of strings + */ private String[] readableArrayToStringArray(ReadableArray r) { int length = r.size(); String[] strArray = new String[length]; @@ -53,8 +54,9 @@ private String[] readableArrayToStringArray(ReadableArray r) { @ReactMethod public void mail(ReadableMap options, Callback callback) { - Intent i = new Intent(Intent.ACTION_SENDTO); - i.setData(Uri.parse("mailto:")); + Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE); + i.setType("message/rfc822"); + if (options.hasKey("subject") && !options.isNull("subject")) { i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject")); @@ -99,6 +101,29 @@ public void mail(ReadableMap options, Callback callback) { i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients)); } + if (options.hasKey("attachments") && !options.isNull("attachments")) { + ReadableArray r = options.getArray("attachments"); + int length = r.size(); + ArrayList uris = new ArrayList(); + for (int keyIndex = 0; keyIndex < length; keyIndex++) { + ReadableMap clip = r.getMap(keyIndex); + if (clip.hasKey("path") && !clip.isNull("path")) { + String path = clip.getString("path"); + File file = new File(path); + if (file.exists()) { + uris.add(Uri.fromFile(file)); + } + } + } + i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); + } + + i.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it. + final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO); + Uri data = Uri.parse("mailto:?to=some@email.com"); + restrictIntent.setData(data); + i.setSelector(restrictIntent); + PackageManager manager = reactContext.getPackageManager(); List list = manager.queryIntentActivities(i, 0); @@ -115,13 +140,13 @@ public void mail(ReadableMap options, Callback callback) { callback.invoke("error"); } } else { - Intent chooser = Intent.createChooser(i, "Send Mail"); - chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Intent chooser = Intent.createChooser(i, "Send Mail"); + chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + try { + reactContext.startActivity(chooser); + } catch (Exception ex) { + } - try { - reactContext.startActivity(chooser); - } catch (Exception ex) { - callback.invoke("error"); } } }