我正在使用httplib2和Mailgun API来发送电子邮件附件,我使用Google驱动器下载了该附件,电子邮件正在发送,但没有附件。。下面是我的代码。。在
DRIVE = discovery.build('drive', 'v3', http=http_auth)
request = DRIVE.files().export_media(fileId=file_id, mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
logging.info("Download %d%%." % int(status.progress() * 100))
messages = {
"from": sender,
"to": recipient,
"subject": 'Attachment Mail from Mailgun',
"text": 'Testing',
"attachment": fh.getvalue()
}
url = URL
data = {
"from": messages['from'],
"to": messages['to'],
"subject": messages['subject'],
"text": messages['text'],
"attachment": messages['attachment']
}
pl = urllib.urlencode(data)
http = httplib2.Http()
http.add_credentials('api', API)
resp, content = http.request(
url, 'POST', pl,
headers={"Content-Type": "application/x-www-form-urlencoded"})
def send_mailgun(收件人、主题、html、文件、文件名、抄送、密件抄送):
MAILGUN_URL='https://api.mailgun.net/v3/DOMAIN/messages'MAILGUN\ukey='KEY-secret'
data={“subject”:主题,“from”:“from_EMAIL”,“to”:to,“html”:html}
如果抄送!=“”:数据[“cc”]=cc
如果密件抄送!=“”:数据[“密件抄送”]=密件抄送
如果文件名和文件名!比例=“”请求.post(MAILGUN_URL,auth=(“api”,MAILGUN_KEY),files=[(“attachment”,(file_name,file))],data=data)其他:resp=请求.post(MAILGUN_URL,auth=(“api”,MAILGUN_密钥),data=data)
我们使用
mailgun
API发送电子邮件,使用Appengine
并从cloud storage
读取,相同的原则将适用于google drive
:我建议的第一件事是研究StringIO。它允许您以比BytesIO更简单的方式模拟在appengine沙盒中处理文件,但是两者都产生python所称的支持
.read()
的file objects
,因此这两种方法都可以使用。在一旦您将文件作为
file like object
,您只需要将其正确地传递给API。下面的例子使用了requests库,因为它使发布文件变得非常容易,并且与appengine兼容。在请注意,在本例中,
open(FILE_PATH_1, 'rb')
是file like object
,您只需将其替换为file对象:不过现在已经晚了。。。我早就解决了。。以下是我所做的:
相关问题 更多 >
编程相关推荐