diff --git a/README.md b/README.md index f0d9597..550cf13 100644 --- a/README.md +++ b/README.md @@ -125,11 +125,11 @@ export default class App extends Component { bccRecipients: ['supportBCC@example.com'], body: 'A Bold Body', 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, diff --git a/android/src/main/java/com/chirag/RNMail/RNMailModule.java b/android/src/main/java/com/chirag/RNMail/RNMailModule.java index 613e1bc..0734745 100644 --- a/android/src/main/java/com/chirag/RNMail/RNMailModule.java +++ b/android/src/main/java/com/chirag/RNMail/RNMailModule.java @@ -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. @@ -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]; @@ -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")); @@ -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 uris = new ArrayList(); 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"); + } + + 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 list = manager.queryIntentActivities(i, 0); @@ -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(); } } } -}