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" ));
@@ -99,6 +101,29 @@ public void mail(ReadableMap options, Callback callback) {
99101 i .putExtra (Intent .EXTRA_BCC , readableArrayToStringArray (bccRecipients ));
100102 }
101103
104+ if (options .hasKey ("attachments" ) && !options .isNull ("attachments" )) {
105+ ReadableArray r = options .getArray ("attachments" );
106+ int length = r .size ();
107+ ArrayList <Uri > uris = new ArrayList <Uri >();
108+ for (int keyIndex = 0 ; keyIndex < length ; keyIndex ++) {
109+ ReadableMap clip = r .getMap (keyIndex );
110+ if (clip .hasKey ("path" ) && !clip .isNull ("path" )) {
111+ String path = clip .getString ("path" );
112+ File file = new File (path );
113+ if (file .exists ()) {
114+ uris .add (Uri .fromFile (file ));
115+ }
116+ }
117+ }
118+ i .putParcelableArrayListExtra (Intent .EXTRA_STREAM , uris );
119+ }
120+
121+ 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.
122+ final Intent restrictIntent = new Intent (Intent .ACTION_SENDTO );
123+ Uri data =
Uri .
parse (
"mailto:[email protected] " );
124+ restrictIntent .setData (data );
125+ i .setSelector (restrictIntent );
126+
102127 PackageManager manager = reactContext .getPackageManager ();
103128 List <ResolveInfo > list = manager .queryIntentActivities (i , 0 );
104129
@@ -115,13 +140,13 @@ public void mail(ReadableMap options, Callback callback) {
115140 callback .invoke ("error" );
116141 }
117142 } else {
118- Intent chooser = Intent .createChooser (i , "Send Mail" );
119- chooser .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
143+ Intent chooser = Intent .createChooser (i , "Send Mail" );
144+ chooser .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
145+ try {
146+ reactContext .startActivity (chooser );
147+ } catch (Exception ex ) {
148+ }
120149
121- try {
122- reactContext .startActivity (chooser );
123- } catch (Exception ex ) {
124- callback .invoke ("error" );
125150 }
126151 }
127152}
0 commit comments