2014-11-05 61 views
5

我已經嘗試了幾種方式在Android上的Kivy應用程序中使用Python發送電子郵件。我最近來的是使用Plyer的電子郵件功能(https://plyer.readthedocs.org/en/latest/#plyer.facades.Email),但它似乎不支持附件。發送帶附件的電子郵件,附帶Android上的Kivy應用程序,最好通過打開電子郵件客戶端

我希望我的應用程序能夠打開用戶的電子郵件客戶端並填充收件人,主題,正文和附件字段。附件將是由我的應用程序生成的.csv文件。

有沒有人有如何做到這一點的建議?我如何修改此代碼以包含附件?

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() 

回答

1

android module具有使一個文件名支持。你可以直接使用它,或者通過調用android api的相關部分將這個特性添加到plyer中。儘管如此,我並沒有確切地回憶起你是如何做到的。

相關問題