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

integrate with react-native-image-picker #87

Open
wants to merge 5 commits 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
69 changes: 48 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
A React Native wrapper for Apple's ``MFMailComposeViewController`` from iOS and Mail Intent on android
Supports emails with attachments.

### Installation
## Installation

There was a breaking change in RN >=40. So for React Native >= 0.40: use v3.x and higher of this lib. otherwise use v2.x

```bash
npm i --save react-native-mail
npm i --save react-native-mail # npm syntax
yarn add react-native-mail # yarn syntax
```

### Add it to your android project
### Automatic Installation
You can automatically link the native components or follow the manual instructions below if you prefer.

```bash
react-native link
```

### Manual Installation: Android

* In `android/setting.gradle`

Expand Down Expand Up @@ -85,7 +93,7 @@ public class MainApplication extends Application implements ReactApplication {



### Add it to your iOS project
### Manual Installation: iOS

1. Run `npm install react-native-mail --save`
2. Open your project in XCode, right click on `Libraries` and click `Add
Expand All @@ -97,40 +105,59 @@ public class MainApplication extends Application implements ReactApplication {

## Example
```javascript
var Mailer = require('react-native-mail');
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/

import React, { Component } from 'react';
import { View, Alert, Button } from 'react-native';
import Mailer from 'react-native-mail';

var MailExampleApp = React.createClass({
handleHelp: function() {
export default class App extends Component {

handleEmail = () => {
Mailer.mail({
subject: 'need help',
recipients: ['[email protected]'],
ccRecipients: ['[email protected]'],
bccRecipients: ['[email protected]'],
body: '',
body: '<b>A Bold Body</b>',
isHTML: true,
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 [email protected]');
}
Alert.alert(
error,
event,
[
{text: 'Ok', onPress: () => console.log('OK: Email Error Response')},
{text: 'Cancel', onPress: () => console.log('CANCEL: Email Error Response')}
],
{ cancelable: true }
)
});
},
render: function() {
}

render() {
return (
<TouchableHighlight
onPress={row.handleHelp}
underlayColor="#f7f7f7">
<View style={styles.container}>
<Image source={require('image!announcement')} style={styles.image} />
</View>
</TouchableHighlight>
<View style={styles.container}>
<Button
onPress={this.handleEmail}
title="Email Me"
color="#841584"
accessabilityLabel="Purple Email Me Button"
/>
</View>
);
}
});
}


```

### Note
Expand Down
21 changes: 16 additions & 5 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 = [options[@"isHTML"] boolValue];
}
Expand All @@ -57,7 +57,7 @@ - (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];
Expand All @@ -73,12 +73,23 @@ - (dispatch_queue_t)methodQueue
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
}

// Get the URL string, which is *not* a path (e.g. because it's file:// based)
NSString *attachmentURLString = [RCTConvert NSString:options[@"attachment"][@"path"]];
// Create a URL from the string
NSURL *attachmentURL = [[NSURLComponents componentsWithString:attachmentURLString] URL];

// Get the resource path and read the file using NSData
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
NSError *error = nil;
NSData *fileData = [NSData dataWithContentsOfURL:attachmentURL options:0 error:&error];

if(fileData == nil) {
// handle error
}


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