From 3bc0c45a835d3314cf666cc23ab73420bb964774 Mon Sep 17 00:00:00 2001 From: Christopher Date: Thu, 3 Sep 2015 12:13:12 -0700 Subject: [PATCH 1/4] Add option for attachments --- RNMail/RNMail.m | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/RNMail/RNMail.m b/RNMail/RNMail.m index 0abfccc..b556c20 100644 --- a/RNMail/RNMail.m +++ b/RNMail/RNMail.m @@ -47,6 +47,39 @@ - (dispatch_queue_t)methodQueue [mail setToRecipients:recipients]; } + if (options[@"attachmentPath"] && options[@"attachmentType"]){ + NSString *attachmentPath = [RCTConvert NSString:options[@"attachmentPath"]]; + NSString *attachmentType = [RCTConvert NSString:options[@"attachmentType"]]; + NSString *attachmentName = [RCTConvert NSString:options[@"attachmentName"]]; + + // 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]; + } + UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [root presentViewController:mail animated:YES completion:nil]; } else { From 47ea00b50f99d942c3604c6e73f7f4c2d50fab74 Mon Sep 17 00:00:00 2001 From: Christopher Date: Thu, 3 Sep 2015 12:13:34 -0700 Subject: [PATCH 2/4] Update readme --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3bea2a0..98b5b36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # react-native-mail -A react-native wrapper on top of apple's ``MFMailComposeViewController``. +A React Native wrapper for Apple's ``MFMailComposeViewController``. +Supports emails with attachments. ### Add it to your project @@ -21,7 +22,10 @@ var MailExampleApp = React.createClass({ Mailer.mail({ subject: 'need help', recipients: ['support@example.com'], - body: '' + body: '', + attachmentPath: '', // The absolute path of the file from which to read data. + attachmentType: '', // Mime Type: jpg, png, doc, ppt, html, pdf + attachmentName: '', // Optional: Custom filename for attachment }, (error, event) => { if(error) { AlertIOS.alert('Error', 'Could not send mail. Please send a mail to support@example.com'); From dce09195cf48e10c1c7f939b2fd7d2370fac17c8 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 7 Sep 2015 11:51:55 -0700 Subject: [PATCH 3/4] Changes based on comments --- README.md | 8 +++++--- RNMail/RNMail.m | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 98b5b36..a8ba9a5 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,11 @@ var MailExampleApp = React.createClass({ subject: 'need help', recipients: ['support@example.com'], body: '', - attachmentPath: '', // The absolute path of the file from which to read data. - attachmentType: '', // Mime Type: jpg, png, doc, ppt, html, pdf - attachmentName: '', // Optional: Custom filename for attachment + attachment: { + path: '', // The absolute path of the file from which to read data. + type: '', // Mime Type: jpg, png, doc, ppt, html, pdf + name: '', // Optional: Custom filename for attachment + } }, (error, event) => { if(error) { AlertIOS.alert('Error', 'Could not send mail. Please send a mail to support@example.com'); diff --git a/RNMail/RNMail.m b/RNMail/RNMail.m index b556c20..8a027bc 100644 --- a/RNMail/RNMail.m +++ b/RNMail/RNMail.m @@ -47,10 +47,10 @@ - (dispatch_queue_t)methodQueue [mail setToRecipients:recipients]; } - if (options[@"attachmentPath"] && options[@"attachmentType"]){ - NSString *attachmentPath = [RCTConvert NSString:options[@"attachmentPath"]]; - NSString *attachmentType = [RCTConvert NSString:options[@"attachmentType"]]; - NSString *attachmentName = [RCTConvert NSString:options[@"attachmentName"]]; + if (options[@"attachment"][@"path"] && options[@"attachment"][@"name"]){ + 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) { @@ -62,6 +62,7 @@ - (dispatch_queue_t)methodQueue // Determine the MIME type NSString *mimeType; + if ([attachmentType isEqualToString:@"jpg"]) { mimeType = @"image/jpeg"; } else if ([attachmentType isEqualToString:@"png"]) { From 1bc048d3dd48ef34a661ec56840e7c22f5df44a4 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 8 Sep 2015 10:32:21 -0700 Subject: [PATCH 4/4] Typo fix + small update --- RNMail/RNMail.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RNMail/RNMail.m b/RNMail/RNMail.m index 8a027bc..ca9b411 100644 --- a/RNMail/RNMail.m +++ b/RNMail/RNMail.m @@ -47,7 +47,7 @@ - (dispatch_queue_t)methodQueue [mail setToRecipients:recipients]; } - if (options[@"attachment"][@"path"] && options[@"attachment"][@"name"]){ + 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"]];