Skip to content

Commit 40eb729

Browse files
committed
Rebase mult-attachment PR
1 parent 3ee4c39 commit 40eb729

File tree

2 files changed

+76
-68
lines changed

2 files changed

+76
-68
lines changed

RNMail/RNMail.m

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ + (BOOL)requiresMainQueueSetup
4141
NSString *subject = [RCTConvert NSString:options[@"subject"]];
4242
[mail setSubject:subject];
4343
}
44-
44+
4545
bool *isHTML = NO;
4646

4747
if (options[@"isHTML"]){
@@ -57,7 +57,7 @@ + (BOOL)requiresMainQueueSetup
5757
NSArray *recipients = [RCTConvert NSArray:options[@"recipients"]];
5858
[mail setToRecipients:recipients];
5959
}
60-
60+
6161
if (options[@"ccRecipients"]){
6262
NSArray *ccRecipients = [RCTConvert NSArray:options[@"ccRecipients"]];
6363
[mail setCcRecipients:ccRecipients];
@@ -67,49 +67,52 @@ + (BOOL)requiresMainQueueSetup
6767
NSArray *bccRecipients = [RCTConvert NSArray:options[@"bccRecipients"]];
6868
[mail setBccRecipients:bccRecipients];
6969
}
70-
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"]];
75-
76-
// Set default filename if not specificed
77-
if (!attachmentName) {
78-
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
79-
}
80-
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/*";
70+
71+
if (options[@"attachments"]){
72+
NSArray *attachments = [RCTConvert NSArray:options[@"attachments"]];
73+
74+
for(NSDictionary *attachment in attachments) {
75+
if (attachment[@"path"] && attachment[@"type"]) {
76+
NSString *attachmentPath = [RCTConvert NSString:attachment[@"path"]];
77+
NSString *attachmentType = [RCTConvert NSString:attachment[@"type"]];
78+
NSString *attachmentName = [RCTConvert NSString:attachment[@"name"]];
79+
80+
// Set default filename if not specificed
81+
if (!attachmentName) {
82+
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
83+
}
84+
// Get the resource path and read the file using NSData
85+
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
86+
87+
// Determine the MIME type
88+
NSString *mimeType;
89+
90+
/*
91+
* Add additional mime types and PR if necessary. Find the list
92+
* of supported formats at http://www.iana.org/assignments/media-types/media-types.xhtml
93+
*/
94+
if ([attachmentType isEqualToString:@"jpg"]) {
95+
mimeType = @"image/jpeg";
96+
} else if ([attachmentType isEqualToString:@"png"]) {
97+
mimeType = @"image/png";
98+
} else if ([attachmentType isEqualToString:@"doc"]) {
99+
mimeType = @"application/msword";
100+
} else if ([attachmentType isEqualToString:@"ppt"]) {
101+
mimeType = @"application/vnd.ms-powerpoint";
102+
} else if ([attachmentType isEqualToString:@"html"]) {
103+
mimeType = @"text/html";
104+
} else if ([attachmentType isEqualToString:@"csv"]) {
105+
mimeType = @"text/csv";
106+
} else if ([attachmentType isEqualToString:@"pdf"]) {
107+
mimeType = @"application/pdf";
108+
} else if ([attachmentType isEqualToString:@"vcard"]) {
109+
mimeType = @"text/vcard";
110+
} else if ([attachmentType isEqualToString:@"json"]) {
111+
mimeType = @"application/json";
112+
} else if ([attachmentType isEqualToString:@"zip"]) {
113+
mimeType = @"application/zip";
114+
} else if ([attachmentType isEqualToString:@"text"]) {
115+
mimeType = @"text/*";
113116
} else if ([attachmentType isEqualToString:@"mp3"]) {
114117
mimeType = @"audio/mpeg";
115118
} else if ([attachmentType isEqualToString:@"wav"]) {
@@ -122,12 +125,12 @@ + (BOOL)requiresMainQueueSetup
122125
mimeType = @"audio/ogg";
123126
} else if ([attachmentType isEqualToString:@"xls"]) {
124127
mimeType = @"application/vnd.ms-excel";
128+
}
129+
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
130+
}
125131
}
126-
127-
// Add attachment
128-
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
129132
}
130-
133+
131134
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
132135

133136
while (root.presentedViewController) {

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,39 @@ public void mail(ReadableMap options, Callback callback) {
6666
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
6767
} else {
6868
i.putExtra(Intent.EXTRA_TEXT, body);
69-
}
69+
}
7070
}
7171

7272
if (options.hasKey("recipients") && !options.isNull("recipients")) {
7373
ReadableArray recipients = options.getArray("recipients");
7474
i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
75-
}
75+
}
7676

7777
if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
7878
ReadableArray ccRecipients = options.getArray("ccRecipients");
7979
i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
8080
}
81+
if (options.hasKey("attachments") && !options.isNull("attachments")) {
82+
ReadableArray r = options.getArray("attachments");
83+
int length = r.size();
84+
ArrayList<Uri> uris = new ArrayList<Uri>();
85+
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
86+
ReadableMap clip = r.getMap(keyIndex);
87+
if (clip.hasKey("path") && !clip.isNull("path")){
88+
String path = clip.getString("path");
89+
File file = new File(path);
90+
Uri u = Uri.fromFile(file);
91+
uris.add(u);
92+
}
93+
}
94+
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
95+
}
8196

8297
if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
8398
ReadableArray bccRecipients = options.getArray("bccRecipients");
8499
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
85100
}
86101

87-
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);
94-
}
95-
}
96-
97102
PackageManager manager = reactContext.getPackageManager();
98103
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
99104

@@ -110,14 +115,14 @@ public void mail(ReadableMap options, Callback callback) {
110115
callback.invoke("error");
111116
}
112117
} else {
113-
Intent chooser = Intent.createChooser(i, "Send Mail");
114-
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
118+
Intent chooser = Intent.createChooser(i, "Send Mail");
119+
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
115120

116-
try {
117-
reactContext.startActivity(chooser);
118-
} catch (Exception ex) {
119-
callback.invoke("error");
120-
}
121+
try {
122+
reactContext.startActivity(chooser);
123+
} catch (Exception ex) {
124+
callback.invoke("error");
121125
}
122126
}
123127
}
128+
}

0 commit comments

Comments
 (0)