Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can now have multiple attachments (both ios and android) #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 32 additions & 36 deletions RNMail/RNMail.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
22 changes: 21 additions & 1 deletion android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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")) {
Expand All @@ -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<Uri> uris = new ArrayList<Uri>();
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<ResolveInfo> list = manager.queryIntentActivities(i, 0);
Expand Down