katzer / cordova-plugin-email-composer

Edit and send email messages
Apache License 2.0
345 stars 336 forks source link

Attachments problem in Outlook #373

Open aaale83 opened 2 years ago

aaale83 commented 2 years ago

Sometimes the plugin doesn't attach every attachments in the array (usually total minus one). This happens completely random.

Tested tablets: Galaxy Tab S2, Galaxy Tab A 8 inches, Galaxy Tab S6 lite Tested Android versions: 7, 11, 11 Default email app: Outlook

A couple of Galaxy Tab A 8 inches users switched to Gmail app and apparently solved the problem, this solution didn't work for the Galaxy Tab S2 users.

When an array of files is attached i get this log:

03-31 15:19:59.240 13728 13728 D CordovaInterfaceImpl: Sending activity result to plugin
03-31 15:19:59.240 13728 13728 D CordovaActivity: Incoming Result. Request code = 0
03-31 15:19:59.238 13728 13728 D CordovaActivity: Paused the activity.
03-31 15:19:59.223 13728 14748 W Bundle  :  at java.lang.Thread.run(Thread.java:923)
03-31 15:19:59.223 13728 14748 W Bundle  :  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
03-31 15:19:59.223 13728 14748 W Bundle  :  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
03-31 15:19:59.223 13728 14748 W Bundle  :  at de.appplant.cordova.emailcomposer.EmailComposer$4.run(EmailComposer.java:175)
03-31 15:19:59.223 13728 14748 W Bundle  :  at org.apache.cordova.CordovaInterfaceImpl.startActivityForResult(CordovaInterfaceImpl.java:68)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.app.Activity.startActivityForResult(Activity.java:5340)
03-31 15:19:59.223 13728 14748 W Bundle  :  at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:352)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.app.Activity.startActivityForResult(Activity.java:5382)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1721)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.content.Intent.migrateExtraStreamToClipData(Intent.java:12048)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:9012)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1145)
03-31 15:19:59.223 13728 14748 W Bundle  :  at android.os.BaseBundle.getCharSequenceArrayList(BaseBundle.java:1343)
03-31 15:19:59.223 13728 14748 W Bundle  : java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
03-31 15:19:59.223 13728 14748 W Bundle  : Attempt to cast generated internal exception:
03-31 15:19:59.220 13728 14748 W Bundle  : Key android.intent.extra.TEXT expected ArrayList<CharSequence> but value was a java.lang.String.  The default value <null> was returned.
03-31 15:19:59.154 13728 15191 D PluginManager: getPlugin - put: EmailComposer
03-31 15:19:58.098 13728 14748 D TEST    : cdvfile://localhost/files/28384_rapportino.pdf: 463056
03-31 15:19:57.012 13728 14748 D TEST    : cdvfile://localhost/files/28383_rapportino.pdf: 223247

Code i'm using:

var email_ref_client = "james@example.org";
var email_cc = "alex@example.org";
var subject = "Test";
var body = "PDF reports";
var email_client = "outlook";

var files = [];
files.push('app://files/28384_rapportino.pdf');
files.push('app://files/28383_rapportino.pdf');

cordova.plugins.email.open({
    to: email_ref_client,
    cc: email_cc,
    subject: subject,
    body:  body,
    app: email_client,
    attachments: files,
}, function() {
    window.location.href = 'main.html';
});
jfoclpf commented 2 years ago

Sending files directly from the file system is a bit buggy with Outlook, have you tried converting the files to base64 and attach them as base64? With me always works.

aaale83 commented 2 years ago

Good suggestion thanks! i've just changed code in the app, i'm giving a feedback in the next couple of days.

aaale83 commented 2 years ago

Problem seems to persist.

Yesterday stats were:

Total technicians using my app: 10 Total emails sent: 28 Total attachments sent: 41 Total missing attachments: 1

Here is a code example, am i doing something wrong?

Open file function to base 64:

function openFileToBase64(fileName) {

    return new Promise(resolve => {

        window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory + 'files/', function(dirEntry) {

            dirEntry.getFile(fileName, { create: false }, function(fileEntry) {

                fileEntry.file(function (file) {

                    var reader = new FileReader();

                    reader.readAsDataURL(file);

                    reader.onloadend = function() {

                        resolve(this.result);    
                    };

                }, function (err) { console.error('error getting fileentry file!' + err); });

            });

        });

    });

}

Send email function:

async sendEmail() {

    var filenames = ["28384_rapportino.pdf", "28383_rapportino.pdf"];
    var files = [];
    var base64file = '';

    for (var x=0; x<filenames.length; x++) {

        base64file = await openFileToBase64(filenames[x]);
        base64file = base64file.split(",");
        files.push("base64:" + filenames[x] + "//" + base64file[1]);

    }

    var email_ref_client = "james@example.org";
    var email_cc = "alex@example.org";
    var subject = "Test";
    var body = "PDF reports";
    var email_client = "outlook";

    cordova.plugins.email.open({
        to: email_ref_client,
        cc: email_cc,
        subject: subject,
        body:  body,
        app: email_client,
        attachments: files,
    }, function() {
        window.location.href = 'main.html';
    });

}

A couple of questions: 1) Is there a way i can deeply debug the problem and somehow help? 2) Is switching to Gmail app on all tablets and configure them to use office365 Exchange server a definitive solution to solve the problem?

jfoclpf commented 2 years ago

On a first glance you seem to do everything OK, this is what I use to convert images to base64 because in iOS the filesystem is also tricky. But your problem seems to reside in the fact that some files are not attached, which seems to be a different problem thus.

Yes, I would strongly suggest you to use Gmail instead, I'm using Gmail in Android for ages with this plugin and never had any issues. In Xiaomi devices you just have to activate MUI optimization to attach files.

If you want to deeply debug this plugin, we would deeply appreciate your PR. Check this file here, and this dir. You would need to know Java.

jfoclpf commented 2 years ago

Try also to search in the list of forks if everyone have already solved that issue https://github.com/katzer/cordova-plugin-email-composer/network/members If you find anything relevant, please do kindly share you results

aaale83 commented 2 years ago

Is there a minimum Android version requirement? Could Android 7 be a problem despite using Gmail App ?

jfoclpf commented 2 years ago

Is there a minimum Android version requirement? Could Android 7 be a problem despite using Gmail App ?

I'm not aware of any. I am using this in Android 9, 10 and 11 it and works as expected.

The only restriction I've been seeing (Android 9, 10 and 11 + Gmail) is with Xiaomi devices wherein you have to activate MIUI optimization.

But yes, Android 7 is already quite old. Test it at least with 9.