Skip to content
Merged
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -21,7 +22,12 @@ var MailExampleApp = React.createClass({
Mailer.mail({
subject: 'need help',
recipients: ['support@example.com'],
body: ''
body: '',
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');
Expand Down
34 changes: 34 additions & 0 deletions RNMail/RNMail.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ - (dispatch_queue_t)methodQueue
[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"]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would a switch case be easy to maintain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch statements don´t work with NSStrings, only with integers.

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 {
Expand Down