Skip to content

Commit 4dd97b6

Browse files
Nicolas Morelnmorel
Nicolas Morel
authored andcommitted
Support for multiple attachments
Merged PR chirag04#43 (cherry picked from commit ae34123)
1 parent 3ee4c39 commit 4dd97b6

File tree

2 files changed

+35
-67
lines changed

2 files changed

+35
-67
lines changed

RNMail/RNMail.m

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ + (BOOL)requiresMainQueueSetup
4141
NSString *subject = [RCTConvert NSString:options[@"subject"]];
4242
[mail setSubject:subject];
4343
}
44-
44+
4545
bool *isHTML = NO;
46-
46+
4747
if (options[@"isHTML"]){
4848
isHTML = [options[@"isHTML"] boolValue];
4949
}
@@ -62,70 +62,32 @@ + (BOOL)requiresMainQueueSetup
6262
NSArray *ccRecipients = [RCTConvert NSArray:options[@"ccRecipients"]];
6363
[mail setCcRecipients:ccRecipients];
6464
}
65-
65+
6666
if (options[@"bccRecipients"]){
6767
NSArray *bccRecipients = [RCTConvert NSArray:options[@"bccRecipients"]];
6868
[mail setBccRecipients:bccRecipients];
6969
}
7070

71-
if (options[@"attachment"] && options[@"attachment"][@"path"] && options[@"attachment"][@"type"]){
72-
NSString *attachmentPath = [RCTConvert NSString:options[@"attachment"][@"path"]];
73-
NSString *attachmentType = [RCTConvert NSString:options[@"attachment"][@"type"]];
74-
NSString *attachmentName = [RCTConvert NSString:options[@"attachment"][@"name"]];
71+
if (options[@"attachment"]){
72+
NSArray *attachments = [RCTConvert NSArray:options[@"attachment"]];
7573

76-
// Set default filename if not specificed
77-
if (!attachmentName) {
78-
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
79-
}
74+
for (NSDictionary *attachment in attachments) {
75+
NSString *path = [RCTConvert NSString:attachment[@"path"]];
76+
NSString *type = [RCTConvert NSString:attachment[@"type"]];
77+
NSString *name = [RCTConvert NSString:attachment[@"name"]];
8078

81-
// Get the resource path and read the file using NSData
82-
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
83-
84-
// Determine the MIME type
85-
NSString *mimeType;
86-
87-
/*
88-
* Add additional mime types and PR if necessary. Find the list
89-
* of supported formats at http://www.iana.org/assignments/media-types/media-types.xhtml
90-
*/
91-
if ([attachmentType isEqualToString:@"jpg"]) {
92-
mimeType = @"image/jpeg";
93-
} else if ([attachmentType isEqualToString:@"png"]) {
94-
mimeType = @"image/png";
95-
} else if ([attachmentType isEqualToString:@"doc"]) {
96-
mimeType = @"application/msword";
97-
} else if ([attachmentType isEqualToString:@"ppt"]) {
98-
mimeType = @"application/vnd.ms-powerpoint";
99-
} else if ([attachmentType isEqualToString:@"html"]) {
100-
mimeType = @"text/html";
101-
} else if ([attachmentType isEqualToString:@"csv"]) {
102-
mimeType = @"text/csv";
103-
} else if ([attachmentType isEqualToString:@"pdf"]) {
104-
mimeType = @"application/pdf";
105-
} else if ([attachmentType isEqualToString:@"vcard"]) {
106-
mimeType = @"text/vcard";
107-
} else if ([attachmentType isEqualToString:@"json"]) {
108-
mimeType = @"application/json";
109-
} else if ([attachmentType isEqualToString:@"zip"]) {
110-
mimeType = @"application/zip";
111-
} else if ([attachmentType isEqualToString:@"text"]) {
112-
mimeType = @"text/*";
113-
} else if ([attachmentType isEqualToString:@"mp3"]) {
114-
mimeType = @"audio/mpeg";
115-
} else if ([attachmentType isEqualToString:@"wav"]) {
116-
mimeType = @"audio/wav";
117-
} else if ([attachmentType isEqualToString:@"aiff"]) {
118-
mimeType = @"audio/aiff";
119-
} else if ([attachmentType isEqualToString:@"flac"]) {
120-
mimeType = @"audio/flac";
121-
} else if ([attachmentType isEqualToString:@"ogg"]) {
122-
mimeType = @"audio/ogg";
123-
} else if ([attachmentType isEqualToString:@"xls"]) {
124-
mimeType = @"application/vnd.ms-excel";
125-
}
79+
if (name == nil){
80+
name = [[path lastPathComponent] stringByDeletingPathExtension];
81+
}
82+
// Get the resource path and read the file using NSData
83+
NSData *fileData = [NSData dataWithContentsOfFile:path];
84+
85+
// Agnostic to type (ios mailer can handle it as long as there's a file extension)
86+
NSString *mimeType;
87+
mimeType = @"application/octet-stream";
12688

127-
// Add attachment
128-
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
89+
[mail addAttachmentData:fileData mimeType:mimeType fileName:name];
90+
}
12991
}
13092

13193
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.List;
1717
import java.io.File;
18+
import java.util.ArrayList;
1819

1920
/**
2021
* NativeModule that allows JS to open emails sending apps chooser.
@@ -53,8 +54,8 @@ 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("text/plain");
5859

5960
if (options.hasKey("subject") && !options.isNull("subject")) {
6061
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
@@ -85,17 +86,22 @@ public void mail(ReadableMap options, Callback callback) {
8586
}
8687

8788
if (options.hasKey("attachment") && !options.isNull("attachment")) {
88-
ReadableMap attachment = options.getMap("attachment");
89-
if (attachment.hasKey("path") && !attachment.isNull("path")) {
90-
String path = attachment.getString("path");
91-
File file = new File(path);
92-
Uri p = Uri.fromFile(file);
93-
i.putExtra(Intent.EXTRA_STREAM, p);
89+
ReadableArray attachments = options.getArray("attachment");
90+
ArrayList<Uri> uris = new ArrayList<Uri>();
91+
for (int j = 0; j < attachments.size(); j++) {
92+
ReadableMap attachment = attachments.getMap(j);
93+
if (attachment.hasKey("path") && !attachment.isNull("path")) {
94+
String path = attachment.getString("path");
95+
File file = new File(path);
96+
Uri p = Uri.fromFile(file);
97+
uris.add(p);
98+
}
9499
}
100+
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
95101
}
96102

97103
PackageManager manager = reactContext.getPackageManager();
98-
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
104+
List<ResolveInfo> list = manager.queryIntentActivities(i, PackageManager.MATCH_ALL);
99105

100106
if (list == null || list.size() == 0) {
101107
callback.invoke("not_available");

0 commit comments

Comments
 (0)