Skip to content

Commit 716f28e

Browse files
Support URI for attachment location. (#174)
* Support URI for attachment location. * Add example path & uri to README comments.
1 parent 18689d3 commit 716f28e

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ export default class App extends Component {
124124
ccRecipients: ['[email protected]'],
125125
bccRecipients: ['[email protected]'],
126126
body: '<b>A Bold Body</b>',
127-
customChooserTitle: "This is my new title", // Android only (defaults to "Send Mail")
127+
customChooserTitle: 'This is my new title', // Android only (defaults to "Send Mail")
128128
isHTML: true,
129129
attachments: [{
130-
path: '', // The absolute path of the file from which to read data.
131-
type: '', // Mime Type: jpg, png, doc, ppt, html, pdf, csv
132-
// mimeType - use only if you want to use custom type
133-
name: '', // Optional: Custom filename for attachment
130+
// Specify either `path` or `uri` to indicate where to find the file data.
131+
// The API used to create or locate the file will usually indicate which it returns.
132+
// An absolute path will look like: /cacheDir/photos/some image.jpg
133+
// A URI starts with a protocol and looks like: content://appname/cacheDir/photos/some%20image.jpg
134+
path: '', // The absolute path of the file from which to read data.
135+
uri: '', // The uri of the file from which to read the data.
136+
// Specify either `type` or `mimeType` to indicate the type of data.
137+
type: '', // Mime Type: jpg, png, doc, ppt, html, pdf, csv
138+
mimeType: '', // - use only if you want to use custom type
139+
name: '', // Optional: Custom filename for attachment
134140
}]
135141
}, (error, event) => {
136142
Alert.alert(

RNMail/RNMail.m

+21-9
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,37 @@ + (BOOL)requiresMainQueueSetup
7070
if (options[@"attachments"]) {
7171
NSArray *attachments = [RCTConvert NSArray:options[@"attachments"]];
7272
for (NSDictionary *attachment in attachments) {
73-
if (attachment[@"path"] && (attachment[@"type"] || attachment[@"mimeType"])) {
73+
if ((attachment[@"path"] || attachment[@"uri"]) && (attachment[@"type"] || attachment[@"mimeType"])) {
7474
NSString *attachmentPath = [RCTConvert NSString:attachment[@"path"]];
75+
NSString *attachmentUri = [RCTConvert NSString:attachment[@"uri"]];
7576
NSString *attachmentType = [RCTConvert NSString:attachment[@"type"]];
7677
NSString *attachmentName = [RCTConvert NSString:attachment[@"name"]];
7778
NSString *attachmentMimeType = [RCTConvert NSString:attachment[@"mimeType"]];
7879

79-
NSFileManager *fileManager = [NSFileManager defaultManager];
80-
if (![fileManager fileExistsAtPath:attachmentPath]){
81-
callback(@[[NSString stringWithFormat: @"attachment file with path '%@' does not exist", attachmentPath]]);
82-
return;
83-
}
84-
8580
// Set default filename if not specificed
8681
if (!attachmentName) {
8782
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
8883
}
8984

90-
// Get the resource path and read the file using NSData
91-
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
85+
NSData *fileData;
86+
if (attachmentPath) {
87+
NSFileManager *fileManager = [NSFileManager defaultManager];
88+
if (![fileManager fileExistsAtPath:attachmentPath]){
89+
callback(@[[NSString stringWithFormat: @"attachment file with path '%@' does not exist", attachmentPath]]);
90+
return;
91+
}
92+
// Get the resource path and read the file using NSData
93+
fileData = [NSData dataWithContentsOfFile:attachmentPath];
94+
} else if (attachmentUri) {
95+
// Get the URI and read it using NSData
96+
NSURL *attachmentURL = [[NSURLComponents componentsWithString:attachmentUri] URL];
97+
NSError *error = nil;
98+
fileData = [NSData dataWithContentsOfURL:attachmentURL options:0 error:&error];
99+
if (!fileData) {
100+
callback(@[[NSString stringWithFormat: @"attachment file with uri '%@' does not exist", attachmentUri]]);
101+
return;
102+
}
103+
}
92104

93105
// Determine the MIME type
94106
NSString *mimeType;

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,24 @@ public void mail(ReadableMap options, Callback callback) {
9999
ArrayList<Uri> uris = new ArrayList<Uri>();
100100
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
101101
ReadableMap clip = r.getMap(keyIndex);
102-
if (clip.hasKey("path") && !clip.isNull("path")){
102+
Uri uri;
103+
if (clip.hasKey("path") && !clip.isNull("path")) {
103104
String path = clip.getString("path");
104105
File file = new File(path);
105-
Uri uri = FileProvider.getUriForFile(reactContext, provider, file);
106-
uris.add(uri);
107-
108-
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
109-
String packageName = resolvedIntentInfo.activityInfo.packageName;
110-
reactContext.grantUriPermission(packageName, uri,
111-
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
112-
}
106+
uri = FileProvider.getUriForFile(reactContext, provider, file);
107+
} else if (clip.hasKey("uri") && !clip.isNull("uri")) {
108+
String uriPath = clip.getString("uri");
109+
uri = Uri.parse(uriPath);
110+
} else {
111+
callback.invoke("not_found");
112+
return;
113+
}
114+
uris.add(uri);
115+
116+
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
117+
String packageName = resolvedIntentInfo.activityInfo.packageName;
118+
reactContext.grantUriPermission(packageName, uri,
119+
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
113120
}
114121
}
115122

index.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export namespace Mailer {
88
customChooserTitle?: string;
99
isHTML?: boolean;
1010
attachments?: {
11-
path: string;
12-
type?: string;
11+
path?: string; // Specify either 'path' or 'uri'
12+
uri?: string;
13+
type?: string; // Specify either 'type' or 'mimeType'
1314
mimeType?: string;
1415
name?: string;
1516
}[]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-mail",
3-
"version": "6.0.2",
3+
"version": "6.1.0",
44
"description": "A wrapper on top of MFMailComposeViewController from iOS and Mail Intent on android",
55
"author": {
66
"name": "Chirag Jain",

0 commit comments

Comments
 (0)