Send an email with the application from the Kivy app on Android, preferably by opening the email client

I tried several ways to send an email using Python in a Kivy Android app. The closest I came is to use the Plyer email function ( https://plyer.readthedocs.org/en/latest/#plyer.facades.Email ), however it does not support attachments.

I want my application to open the user's email client and fill in the recipient, subject, body, and attachment fields. The attachment will generate a CSV file by my application.

Does anyone have any suggestions on how to do this? How to change this code to include attachments?

from jnius import autoclass, cast from plyer.facades import Email from plyer.platforms.android import activity Intent = autoclass('android.content.Intent') AndroidString = autoclass('java.lang.String') class AndroidEmail(Email): def _send(self, **kwargs): intent = Intent(Intent.ACTION_SEND) intent.setType('text/plain') recipient = kwargs.get('recipient') subject = kwargs.get('subject') text = kwargs.get('text') create_chooser = kwargs.get('create_chooser') if recipient: intent.putExtra(Intent.EXTRA_EMAIL, [recipient]) if subject: android_subject = cast('java.lang.CharSequence', AndroidString(subject)) intent.putExtra(Intent.EXTRA_SUBJECT, android_subject) if text: android_text = cast('java.lang.CharSequence', AndroidString(text)) intent.putExtra(Intent.EXTRA_TEXT, android_text) if create_chooser: chooser_title = cast('java.lang.CharSequence', AndroidString('Send message with:')) activity.startActivity(Intent.createChooser(intent, chooser_title)) else: activity.startActivity(intent) def instance(): return AndroidEmail() 
+5
source share
2 answers

I know this is an old question, but I tried to solve this problem for a while and came up with a solution, I hope this can save someone else in the future:

 from jnius import autoclass, cast from plyer.platforms.android import activity def sendemail(self, **kwargs): Intent = autoclass('android.content.Intent') AndroidString = autoclass('java.lang.String') Uri = autoclass('android.net.Uri') File = autoclass('java.io.File') intent = Intent(Intent.ACTION_SEND) intent.setType('*/*') recipient = kwargs.get('recipient') subject = kwargs.get('subject') text = kwargs.get('text') create_chooser = kwargs.get('create_chooser') file_to_attach = kwargs.get('file_to_attach') if recipient: intent.putExtra(Intent.EXTRA_EMAIL, [recipient]) if subject: android_subject = cast('java.lang.CharSequence', AndroidString(subject)) intent.putExtra(Intent.EXTRA_SUBJECT, android_subject) if text: android_text = cast('java.lang.CharSequence', AndroidString(text)) intent.putExtra(Intent.EXTRA_TEXT, android_text) if file_to_attach: attachment = File('path/to/your/file/' + file_to_attach) uri = Uri.fromFile(attachment) parcelable = cast('android.os.Parcelable', uri) intent.putExtra(Intent.EXTRA_STREAM, parcelable) if create_chooser: chooser_title = cast('java.lang.CharSequence', AndroidString('Send message with:')) activity.startActivity(Intent.createChooser(intent, chooser_title)) else: activity.startActivity(intent) 

So, from the Android email section , the general intent guide shows that Intent.EXTRA_STREAM is how the application is, and after looking at the code from a similar question, here a few additional additions from this code to the Plyer email code took care of the need to get attached to the email from my application.

Significant additions were:

Uri = autoclass('android.net.Uri') File = autoclass('java.io.File')

changing intent.setType to: ('*/*') to receive files, not just text,

adding the line file_to_attach = kwargs.get('file_to_attach') to get the file from the function call,

and finally:

 if file_to_attach: attachment = File('path/to/your/file/' + file_to_attach) uri = Uri.fromFile(attachment) parcelable = cast('android.os.Parcelable', uri) intent.putExtra(Intent.EXTRA_STREAM, parcelable) 

to attach the file to the letter.

Then, in the function call, just include the file_to_attach argument: sendemail(recipient=' somebody@email.com ',subject='subject',text='some text',file_to_attach='a_file.csv',create_chooser=True)

0
source

Source: https://habr.com/ru/post/1206219/


All Articles