Skip to content

Commit 04917f9

Browse files
authored
Merge pull request #2 from adamski/revert-1-multiple-attachments-fix
Revert "Fix multimple attachments android"
2 parents bea7cd4 + 0128cb3 commit 04917f9

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

android/src/main/java/com/chirag/RNMail/RNMailModule.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
import android.net.Uri;
77
import android.text.Html;
88

9-
import com.facebook.react.bridge.Callback;
109
import com.facebook.react.bridge.ReactApplicationContext;
1110
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1211
import com.facebook.react.bridge.ReactMethod;
13-
import com.facebook.react.bridge.ReadableArray;
1412
import com.facebook.react.bridge.ReadableMap;
13+
import com.facebook.react.bridge.ReadableArray;
14+
import com.facebook.react.bridge.Callback;
1515

16-
import java.io.File;
17-
import java.util.ArrayList;
1816
import java.util.List;
19-
17+
import java.io.File;
2018

2119
/**
2220
* NativeModule that allows JS to open emails sending apps chooser.
@@ -36,11 +34,12 @@ public String getName() {
3634
}
3735

3836
/**
39-
* Converts a ReadableArray to a String array
40-
*
41-
* @param r the ReadableArray instance to convert
42-
* @return array of strings
43-
*/
37+
* Converts a ReadableArray to a String array
38+
*
39+
* @param r the ReadableArray instance to convert
40+
*
41+
* @return array of strings
42+
*/
4443
private String[] readableArrayToStringArray(ReadableArray r) {
4544
int length = r.size();
4645
String[] strArray = new String[length];
@@ -54,9 +53,8 @@ private String[] readableArrayToStringArray(ReadableArray r) {
5453

5554
@ReactMethod
5655
public void mail(ReadableMap options, Callback callback) {
57-
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
58-
i.setType("message/rfc822");
59-
56+
Intent i = new Intent(Intent.ACTION_SENDTO);
57+
i.setData(Uri.parse("mailto:"));
6058

6159
if (options.hasKey("subject") && !options.isNull("subject")) {
6260
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
@@ -68,46 +66,38 @@ public void mail(ReadableMap options, Callback callback) {
6866
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
6967
} else {
7068
i.putExtra(Intent.EXTRA_TEXT, body);
71-
}
69+
}
7270
}
7371

7472
if (options.hasKey("recipients") && !options.isNull("recipients")) {
7573
ReadableArray recipients = options.getArray("recipients");
7674
i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
77-
}
75+
}
7876

7977
if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
8078
ReadableArray ccRecipients = options.getArray("ccRecipients");
8179
i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
8280
}
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-
8981
if (options.hasKey("attachments") && !options.isNull("attachments")) {
9082
ReadableArray r = options.getArray("attachments");
9183
int length = r.size();
9284
ArrayList<Uri> uris = new ArrayList<Uri>();
9385
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
9486
ReadableMap clip = r.getMap(keyIndex);
95-
if (clip.hasKey("path") && !clip.isNull("path")) {
87+
if (clip.hasKey("path") && !clip.isNull("path")){
9688
String path = clip.getString("path");
9789
File file = new File(path);
98-
if (file.exists()) {
99-
uris.add(Uri.fromFile(file));
100-
}
90+
Uri u = Uri.fromFile(file);
91+
uris.add(u);
10192
}
10293
}
10394
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
10495
}
10596

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);
97+
if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
98+
ReadableArray bccRecipients = options.getArray("bccRecipients");
99+
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
100+
}
111101

112102
PackageManager manager = reactContext.getPackageManager();
113103
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
@@ -125,12 +115,14 @@ public void mail(ReadableMap options, Callback callback) {
125115
callback.invoke("error");
126116
}
127117
} else {
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-
}
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");
134125
}
135126
}
136127
}
128+
}

0 commit comments

Comments
 (0)