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

[android] Fix attachments and add option to set custom label for chooser #42

Open
wants to merge 2 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ var MailExampleApp = React.createClass({
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
}
},
chooserLabel: '' // Android only, the label displayed on chooser
}, (error, event) => {
if(error) {
AlertIOS.alert('Error', 'Could not send mail. Please send a mail to [email protected]');
Expand All @@ -134,7 +135,8 @@ var MailExampleApp = React.createClass({
```

### Note
On android callback will only have error(if any) as the argument. event is not available on android.
- If you use attachment on android, file must be located in external storage directory or world readable (unsafe).
- On android callback will only have error(if any) as the argument. event is not available on android.

## Here is how it looks:
![Demo gif](https://github.com/chirag04/react-native-mail/blob/master/screenshot.jpg)
18 changes: 15 additions & 3 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ private String[] readableArrayToStringArray(ReadableArray r) {

@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
Intent i;
if (options.hasKey("attachment") && !options.isNull("attachment")) {
i = new Intent(Intent.ACTION_SEND);
i.setType("vnd.android.cursor.dir/email");
} else {
i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
}

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
Expand Down Expand Up @@ -104,7 +110,13 @@ public void mail(ReadableMap options, Callback callback) {
callback.invoke("error");
}
} else {
Intent chooser = Intent.createChooser(i, "Send Mail");
String chooserLabel;
if (options.hasKey("chooserLabel") && !options.isNull("chooserLabel")) {
chooserLabel = options.getString("chooserLabel");
} else {
chooserLabel = "Send Mail";
}
Intent chooser = Intent.createChooser(i, chooserLabel);
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
Expand Down