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

Copy attachments as temporary files before sending #6

Open
wants to merge 17 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ export default class App extends Component {
bccRecipients: ['[email protected]'],
body: '<b>A Bold Body</b>',
isHTML: true,
attachment: {
attachments: [{
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) => {
Alert.alert(
error,
Expand Down
97 changes: 72 additions & 25 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import android.net.Uri;
import android.text.Html;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
* NativeModule that allows JS to open emails sending apps chooser.
Expand All @@ -34,12 +36,11 @@ public String getName() {
}

/**
* Converts a ReadableArray to a String array
*
* @param r the ReadableArray instance to convert
*
* @return array of strings
*/
* Converts a ReadableArray to a String array
*
* @param r the ReadableArray instance to convert
* @return array of strings
*/
private String[] readableArrayToStringArray(ReadableArray r) {
int length = r.size();
String[] strArray = new String[length];
Expand All @@ -53,8 +54,9 @@ 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 = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");


if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
Expand All @@ -78,27 +80,53 @@ public void mail(ReadableMap options, Callback callback) {
ReadableArray ccRecipients = options.getArray("ccRecipients");
i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
ReadableArray bccRecipients = options.getArray("bccRecipients");
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}

if (options.hasKey("attachments") && !options.isNull("attachments")) {
ReadableArray r = options.getArray("attachments");
int length = r.size();
ArrayList<Uri> uris = new ArrayList<Uri>();
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
ReadableMap clip = r.getMap(keyIndex);
if (clip.hasKey("path") && !clip.isNull("path")){
if (clip.hasKey("path") && !clip.isNull("path")) {
String path = clip.getString("path");
File file = new File(path);
Uri u = Uri.fromFile(file);
uris.add(u);

String name, suffix = "";
if (clip.hasKey("name"))
name = clip.getString("name");
else
name = file.getName();

if (clip.hasKey("type"))
suffix = "." + clip.getString("type");

File temporaryFile = null;
try {
temporaryFile = File.createTempFile(name, suffix, reactContext.getExternalCacheDir());
copy (file, temporaryFile);
} catch (IOException e) {
e.printStackTrace();
Log.e("RNMail", "Error copying to temporary file");
Copy link
Owner Author

Choose a reason for hiding this comment

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

Should probably fail here as temporaryFile will be null

}

temporaryFile.setReadable(true, false);
if (temporaryFile.exists()) {
if (temporaryFile.length() == 0)
Log.d ("RNMail", "Warning, attaching empty file!");
uris.add(Uri.fromFile(temporaryFile));
} else {
Log.e("RNMail", "Attachment file does not exist");
}
}
}
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
ReadableArray bccRecipients = options.getArray("bccRecipients");
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}

PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);

Expand All @@ -115,14 +143,33 @@ public void mail(ReadableMap options, Callback callback) {
callback.invoke("error");
}
} else {
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
callback.invoke("error");
}

}
}

protected static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
callback.invoke("error");
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}
}