Ionic 4 shareViaEmail pdf attachment error

Hello,
I am trying to use the shareViaEmail feature of the social-sharing plugin to be able to send a pdf document via email. I am having an issue with implementing this.

This is my function call:

this.socialSharing.shareViaEmail(‘Hello, I wanted to share this document with you.’, //message
null, //subject
null, //to
null, //cc
null, //bcc
http://www.pdf995.com/samples/pdf.pdf’ . //attachments
).catch(err => {
this.showSuccessToast(“There was a problem, please try again later”);
});

I have installed the plugin, imported the module and listed it in the providers as well. There are no compilation errors.

With ‘http://www.pdf995.com/samples/pdf.pdf’ given as the attachment argument, it always goes to the catch block. If I have ‘null’ as the attachment argument, it is able to open up the native email application, but obviously I am not able to attach my pdf to the email.
Can someone help me figure out what is the issue?
Thank you so much!

Hi @TechNoSavvy!
Welcome to the community! :grinning: :call_me_hand:t5:
I saw many fields as null. So…

null, //message
null, //subject
null, //to
null, //cc
null, //bcc

Please, can you post your entire code here using the formatation tag </> of the text editor. Thanks!

Hello @TomCosta! Thank you so much for replying!

I am unsure if I can post my entire code due to plagiarism purposes. Currently I am just trying to implement Ionic’s socialSharing plugin using the shareViaEmail function as shown at this link from Ionic Docs:

This is the example shown on that page for the shareViaEmail function usage:

// Share via email
this.socialSharing.shareViaEmail('Body', 'Subject', ['recipient@example.org']).then(() => {
  // Success!
}).catch(() => {
  // Error!
});

And this is my code again:

this.socialSharing.shareViaEmail(‘Hello, I wanted to share this document with you.’, //message
null, //subject
null, //to
null, //cc
null, //bcc
‘http://www.pdf995.com/samples/pdf.pdf’ . //attachments
).catch(err => {
this.showSuccessToast(“There was a problem, please try again later”);
});

And accordingly I have implemented it as shown in the code snippet above. The null fields correspond to the fields shown (comments in the same line). So for example, in my example I have only provided the message and attachment parameters so that the other parameters (passed as null) are empty and the user can fill in those fields (subject, to, cc, bcc) when the email application is opened through the share button.

However, my issue is that providing the pdf link as the attachment gives me an error (does not open up the native email application), but keeping the attachment field as null does not give me errors and opens the email app (without the attachment). So I am trying to find a solution so that I can attach a pdf file in an email as a share option.
Any help will be appreciated! Thank you so much!

1 Like

Can you provide the Error Object that you get?

1 Like

Basically the catch block is called and so it shows me “There was a problem, please try again later” which is my message in the catch block. If I print the err received by the catch block to console, it shows : “cordova_not_available”.

Can you please try if this Plugin works for you? The shareViaEmail Code is inspired by this:

1 Like

I’ll try it out, thanks!

Hello everyone, my issue was actually relating to some cleartext support disabled in Android 9, so it’s solved now. Thanks for your help!

Great @TechNoSavvy!
Hope you have found a definitively solution.
Have a nice time!