Skip to content

Commit

Permalink
Support for multiple attachments
Browse files Browse the repository at this point in the history
Merged PR chirag04#43
  • Loading branch information
Nicolas Morel committed Feb 21, 2017
1 parent 50b8903 commit ae34123
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 51 deletions.
62 changes: 20 additions & 42 deletions RNMail/RNMail.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ - (dispatch_queue_t)methodQueue
NSString *subject = [RCTConvert NSString:options[@"subject"]];
[mail setSubject:subject];
}

bool *isHTML = NO;

if (options[@"isHTML"]){
isHTML = YES;
}
Expand All @@ -57,54 +57,32 @@ - (dispatch_queue_t)methodQueue
NSArray *ccRecipients = [RCTConvert NSArray:options[@"ccRecipients"]];
[mail setCcRecipients:ccRecipients];
}

if (options[@"bccRecipients"]){
NSArray *bccRecipients = [RCTConvert NSArray:options[@"bccRecipients"]];
[mail setBccRecipients:bccRecipients];
}

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"]];
if (options[@"attachment"]){
NSArray *attachments = [RCTConvert NSArray:options[@"attachment"]];

// Set default filename if not specificed
if (!attachmentName) {
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
}
for (NSDictionary *attachment in attachments) {
NSString *path = [RCTConvert NSString:attachment[@"path"]];
NSString *type = [RCTConvert NSString:attachment[@"type"]];
NSString *name = [RCTConvert NSString:attachment[@"name"]];

// Get the resource path and read the file using NSData
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];

// Determine the MIME type
NSString *mimeType;

/*
* Add additional mime types and PR if necessary. Find the list
* of supported formats at http://www.iana.org/assignments/media-types/media-types.xhtml
*/
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";
} else if ([attachmentType isEqualToString:@"vcard"]) {
mimeType = @"text/vcard";
} else if ([attachmentType isEqualToString:@"json"]) {
mimeType = @"application/json";
} else if ([attachmentType isEqualToString:@"zip"]) {
mimeType = @"application/zip";
}
if (name == nil){
name = [[path lastPathComponent] stringByDeletingPathExtension];
}
// Get the resource path and read the file using NSData
NSData *fileData = [NSData dataWithContentsOfFile:path];

// Agnostic to type (ios mailer can handle it as long as there's a file extension)
NSString *mimeType;
mimeType = @"application/octet-stream";

// Add attachment
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
[mail addAttachmentData:fileData mimeType:mimeType fileName:name];
}
}

UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
Expand Down
24 changes: 15 additions & 9 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;
import java.io.File;
import java.util.ArrayList;

/**
* NativeModule that allows JS to open emails sending apps chooser.
Expand Down Expand Up @@ -52,8 +53,8 @@ private String[] readableArrayToStringArray(ReadableArray r) {

@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("text/plain");

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
Expand All @@ -79,17 +80,22 @@ public void mail(ReadableMap options, Callback callback) {
}

if (options.hasKey("attachment") && !options.isNull("attachment")) {
ReadableMap attachment = options.getMap("attachment");
if (attachment.hasKey("path") && !attachment.isNull("path")) {
String path = attachment.getString("path");
File file = new File(path);
Uri p = Uri.fromFile(file);
i.putExtra(Intent.EXTRA_STREAM, p);
ReadableArray attachments = options.getArray("attachment");
ArrayList<Uri> uris = new ArrayList<Uri>();
for (int j = 0; j < attachments.size(); j++) {
ReadableMap attachment = attachments.getMap(j);
if (attachment.hasKey("path") && !attachment.isNull("path")) {
String path = attachment.getString("path");
File file = new File(path);
Uri p = Uri.fromFile(file);
uris.add(p);
}
}
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}

PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
List<ResolveInfo> list = manager.queryIntentActivities(i, PackageManager.MATCH_ALL);

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

0 comments on commit ae34123

Please sign in to comment.