From 4af2da11a11021ea6ade24e0c0e4da4823128167 Mon Sep 17 00:00:00 2001 From: jesse Date: Fri, 26 Feb 2016 12:29:08 -0900 Subject: [PATCH] can now have multiple attachments --- RNMail/RNMail.m | 68 +++++++++---------- .../java/com/chirag/RNMail/RNMailModule.java | 22 +++++- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/RNMail/RNMail.m b/RNMail/RNMail.m index ca9b411..e29e765 100644 --- a/RNMail/RNMail.m +++ b/RNMail/RNMail.m @@ -31,54 +31,50 @@ - (dispatch_queue_t)methodQueue MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init]; mail.mailComposeDelegate = self; _callbacks[RCTKeyForInstance(mail)] = callback; - + if (options[@"subject"]){ NSString *subject = [RCTConvert NSString:options[@"subject"]]; [mail setSubject:subject]; } - + if (options[@"body"]){ NSString *body = [RCTConvert NSString:options[@"body"]]; [mail setMessageBody:body isHTML:NO]; } - + if (options[@"recipients"]){ NSArray *recipients = [RCTConvert NSArray:options[@"recipients"]]; [mail setToRecipients:recipients]; } - - if (options[@"attachment"] && options[@"attachment"][@"path"] && options[@"attachment"][@"type"]){ - NSString *attachmentPath = [RCTConvert NSString:options[@"attachment"][@"path"]]; - NSString *attachmentType = [RCTConvert NSString:options[@"attachment"][@"type"]]; - NSString *attachmentName = [RCTConvert NSString:options[@"attachment"][@"name"]]; - - // Set default filename if not specificed - if (!attachmentName) { - attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension]; - } - - // Get the resource path and read the file using NSData - NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath]; - - // Determine the MIME type - NSString *mimeType; - - if ([attachmentType isEqualToString:@"jpg"]) { - mimeType = @"image/jpeg"; - } else if ([attachmentType isEqualToString:@"png"]) { - mimeType = @"image/png"; - } else if ([attachmentType isEqualToString:@"doc"]) { - mimeType = @"application/msword"; - } else if ([attachmentType isEqualToString:@"ppt"]) { - mimeType = @"application/vnd.ms-powerpoint"; - } else if ([attachmentType isEqualToString:@"html"]) { - mimeType = @"text/html"; - } else if ([attachmentType isEqualToString:@"pdf"]) { - mimeType = @"application/pdf"; - } - - // Add attachment - [mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName]; + if (options[@"attachment"]){ + NSArray *attachments = [RCTConvert NSArray:options[@"attachment"]]; + + for(NSDictionary *attachment in attachments){ + NSString *path = [RCTConvert NSString:attachment[@"path"]]; + NSString *type = [RCTConvert NSString:attachment[@"type"]]; + NSString *name = [RCTConvert NSString:attachment[@"name"]]; + + if (name == nil){ + name = [[path lastPathComponent] stringByDeletingPathExtension]; + } + // Get the resource path and read the file using NSData + NSData *fileData = [NSData dataWithContentsOfFile:path]; + + // Determine the MIME type + NSString *mimeType = @"image/png"; + if ([type isEqualToString:@"jpg"]) { + mimeType = @"image/jpeg"; + } else if ([type isEqualToString:@"doc"]) { + mimeType = @"application/msword"; + } else if ([type isEqualToString:@"ppt"]) { + mimeType = @"application/vnd.ms-powerpoint"; + } else if ([type isEqualToString:@"html"]) { + mimeType = @"text/html"; + } else if ([type isEqualToString:@"pdf"]) { + mimeType = @"application/pdf"; + } + [mail addAttachmentData:fileData mimeType:mimeType fileName:name]; + } } UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; diff --git a/android/src/main/java/com/chirag/RNMail/RNMailModule.java b/android/src/main/java/com/chirag/RNMail/RNMailModule.java index 32f86cc..3675d0a 100644 --- a/android/src/main/java/com/chirag/RNMail/RNMailModule.java +++ b/android/src/main/java/com/chirag/RNMail/RNMailModule.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.net.Uri; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -12,6 +13,8 @@ import com.facebook.react.bridge.Callback; import java.util.List; +import java.util.ArrayList; +import java.io.File; /** * NativeModule that allows JS to open emails sending apps chooser. @@ -33,7 +36,7 @@ public String getName() { @ReactMethod public void mail(ReadableMap options, Callback callback) { - Intent i = new Intent(Intent.ACTION_SEND); + Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE); i.setType("message/rfc822"); if (options.hasKey("subject") && !options.isNull("subject")) { @@ -53,6 +56,23 @@ public void mail(ReadableMap options, Callback callback) { } i.putExtra(Intent.EXTRA_EMAIL, recipients); } + if (options.hasKey("attachment") && !options.isNull("attachment")) { + ReadableArray r = options.getArray("attachment"); + int length = r.size(); + ArrayList uris = new ArrayList(); + for (int keyIndex = 0; keyIndex < length; keyIndex++) { + ReadableMap clip = r.getMap(keyIndex); + if (clip.hasKey("path") && !clip.isNull("path")){ + String path = clip.getString("path"); + File fileInput = new File(path); + // fileInput.setReadable(true, false);// + Uri u = Uri.fromFile(fileInput); + uris.add(u); + } + } + i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); + } + PackageManager manager = reactContext.getPackageManager(); List list = manager.queryIntentActivities(i, 0);