66import android .net .Uri ;
77import android .text .Html ;
88
9+ import com .facebook .react .bridge .Callback ;
910import com .facebook .react .bridge .ReactApplicationContext ;
1011import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1112import com .facebook .react .bridge .ReactMethod ;
12- import com .facebook .react .bridge .ReadableMap ;
1313import com .facebook .react .bridge .ReadableArray ;
14- import com .facebook .react .bridge .Callback ;
14+ import com .facebook .react .bridge .ReadableMap ;
1515
16- import java .util .List ;
1716import java .io .File ;
17+ import java .util .ArrayList ;
18+ import java .util .List ;
19+
1820
1921/**
2022 * NativeModule that allows JS to open emails sending apps chooser.
@@ -34,12 +36,11 @@ public String getName() {
3436 }
3537
3638 /**
37- * Converts a ReadableArray to a String array
38- *
39- * @param r the ReadableArray instance to convert
40- *
41- * @return array of strings
42- */
39+ * Converts a ReadableArray to a String array
40+ *
41+ * @param r the ReadableArray instance to convert
42+ * @return array of strings
43+ */
4344 private String [] readableArrayToStringArray (ReadableArray r ) {
4445 int length = r .size ();
4546 String [] strArray = new String [length ];
@@ -53,8 +54,9 @@ private String[] readableArrayToStringArray(ReadableArray r) {
5354
5455 @ ReactMethod
5556 public void mail (ReadableMap options , Callback callback ) {
56- Intent i = new Intent (Intent .ACTION_SENDTO );
57- i .setData (Uri .parse ("mailto:" ));
57+ Intent i = new Intent (Intent .ACTION_SEND_MULTIPLE );
58+ i .setType ("message/rfc822" );
59+
5860
5961 if (options .hasKey ("subject" ) && !options .isNull ("subject" )) {
6062 i .putExtra (Intent .EXTRA_SUBJECT , options .getString ("subject" ));
@@ -66,38 +68,46 @@ public void mail(ReadableMap options, Callback callback) {
6668 i .putExtra (Intent .EXTRA_TEXT , Html .fromHtml (body ));
6769 } else {
6870 i .putExtra (Intent .EXTRA_TEXT , body );
69- }
71+ }
7072 }
7173
7274 if (options .hasKey ("recipients" ) && !options .isNull ("recipients" )) {
7375 ReadableArray recipients = options .getArray ("recipients" );
7476 i .putExtra (Intent .EXTRA_EMAIL , readableArrayToStringArray (recipients ));
75- }
77+ }
7678
7779 if (options .hasKey ("ccRecipients" ) && !options .isNull ("ccRecipients" )) {
7880 ReadableArray ccRecipients = options .getArray ("ccRecipients" );
7981 i .putExtra (Intent .EXTRA_CC , readableArrayToStringArray (ccRecipients ));
8082 }
83+
84+ if (options .hasKey ("bccRecipients" ) && !options .isNull ("bccRecipients" )) {
85+ ReadableArray bccRecipients = options .getArray ("bccRecipients" );
86+ i .putExtra (Intent .EXTRA_BCC , readableArrayToStringArray (bccRecipients ));
87+ }
88+
8189 if (options .hasKey ("attachments" ) && !options .isNull ("attachments" )) {
8290 ReadableArray r = options .getArray ("attachments" );
8391 int length = r .size ();
8492 ArrayList <Uri > uris = new ArrayList <Uri >();
8593 for (int keyIndex = 0 ; keyIndex < length ; keyIndex ++) {
8694 ReadableMap clip = r .getMap (keyIndex );
87- if (clip .hasKey ("path" ) && !clip .isNull ("path" )){
95+ if (clip .hasKey ("path" ) && !clip .isNull ("path" )) {
8896 String path = clip .getString ("path" );
8997 File file = new File (path );
90- Uri u = Uri .fromFile (file );
91- uris .add (u );
98+ if (file .exists ()) {
99+ uris .add (Uri .fromFile (file ));
100+ }
92101 }
93102 }
94103 i .putParcelableArrayListExtra (Intent .EXTRA_STREAM , uris );
95104 }
96105
97- if (options .hasKey ("bccRecipients" ) && !options .isNull ("bccRecipients" )) {
98- ReadableArray bccRecipients = options .getArray ("bccRecipients" );
99- i .putExtra (Intent .EXTRA_BCC , readableArrayToStringArray (bccRecipients ));
100- }
106+ 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.
107+ final Intent restrictIntent = new Intent (Intent .ACTION_SENDTO );
108+ Uri data =
Uri .
parse (
"mailto:[email protected] " );
109+ restrictIntent .setData (data );
110+ i .setSelector (restrictIntent );
101111
102112 PackageManager manager = reactContext .getPackageManager ();
103113 List <ResolveInfo > list = manager .queryIntentActivities (i , 0 );
@@ -115,14 +125,12 @@ public void mail(ReadableMap options, Callback callback) {
115125 callback .invoke ("error" );
116126 }
117127 } else {
118- Intent chooser = Intent .createChooser (i , "Send Mail" );
119- chooser .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
120-
121- try {
122- reactContext .startActivity (chooser );
123- } catch (Exception ex ) {
124- callback .invoke ("error" );
128+ Intent chooser = Intent .createChooser (i , "Send Mail" );
129+ chooser .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
130+ try {
131+ reactContext .startActivity (chooser );
132+ } catch (Exception ex ) {
133+ }
125134 }
126135 }
127136}
128- }
0 commit comments