react-native-mail icon indicating copy to clipboard operation
react-native-mail copied to clipboard

Added URI support

Open ujwal-setlur opened this issue 7 years ago • 8 comments

Currently, emails cannot be attached on android unless the attachment is on an external storage. This can be resolved by:

  1. Using a FileProvider such as a react-native-file-provider so that internal files are exposed as contentUri

  2. Code changes in RNMailModule.java to check if the path is a URI or a file path

      import android.webkit.URLUtil;

      if (attachment.hasKey("path") && !attachment.isNull("path")) {
        String path = attachment.getString("path");
        Uri p;
        // Check for valid URI
        if (URLUtil.isValidUrl(path)) {
          p = Uri.parse(path);
        }
        // Else this is an absolute file path
        else {
          File file = new File(path);
          p = Uri.fromFile(file);
        }
        i.putExtra(Intent.EXTRA_STREAM, p);
      }

ujwal-setlur avatar Aug 07 '17 23:08 ujwal-setlur

Please merge

sakarit-zz avatar Oct 09 '17 19:10 sakarit-zz

is this working?

ghost avatar Oct 09 '17 22:10 ghost

Well... the code above works, but I couldn't get the attachments to Gmail app. I tried using react-native-file-provider and added the provider settings to AndroidManifest.xml, but it just doesn't work (spent a whole day trying to get it to work). It seems that Gmail app only accepts attachments from the external storage area..

I gave up and tried react-native-mail-compose. It works for Android, but the file has to be read as base64 string so I'm using react-native-fetch-blob for it (I already had it in my project).

sakarit-zz avatar Oct 09 '17 22:10 sakarit-zz

i need to attach pdf's into the gmail app, so you recommend to use react-native-mail-compose?

ghost avatar Oct 09 '17 22:10 ghost

Yes! Use it with react-native-fetch-blob so you can read the files as base64 encoded strings out of the box.

https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise

sakarit-zz avatar Oct 09 '17 22:10 sakarit-zz

thx so much :D

ghost avatar Oct 09 '17 22:10 ghost

It worked for me with Gmail

ujwal-setlur avatar Apr 26 '19 17:04 ujwal-setlur

Please merge this!

ThomasStubbe avatar Dec 10 '19 15:12 ThomasStubbe