簡體   English   中英

python 3.6 gmail api——發送帶有附件的電子郵件

[英]python 3.6 gmail api ― send email with attachement

這個python 3腳本假設創建一個電子郵件,將一個文件(使用它的url)附加到它並發送它。 它發送電子郵件,但create_message_with_attachment()

類型錯誤:附加對具有非多部分有效負載的消息無效

我確實閱讀了谷歌文檔。 討論它的堆棧線程專注於花哨的附件樣式,同時混合了 Python 版本的不同語法。

下面的代碼是幾個來源的拼湊而成。 我很難在create_message_with_attachment()中將它們連接在一起。

例如,我不知道我是否應該包括這個(它來自 create_message_without_attachment() ,它適用於這個代碼。參見底部)

raw = base64.urlsafe_b64encode(msg.as_bytes())
raw = raw.decode()
body = {'raw': raw}
return body

帶有附件代碼的創建消息:

import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email import encoders

#needed for attachment
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#needed for gmail service
from apiclient import errors, discovery  

#The scope URL for read/write access to the gmail api 
SCOPES = 'https://www.googleapis.com/auth/gmail.send'

CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Send Email'


def get_credentials():
    # If needed create folder for credential
    home_dir = os.path.expanduser('~') #>> C:\Users\me
    credential_dir = os.path.join(home_dir, '.credentials') # >>C:\Users\me\.credentials   (it's a folder)
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)  #create folder if doesnt exist
    credential_path = os.path.join(credential_dir, 'gmail-python-email-send.json')

    #Store the credential
    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()

    if not credentials or credentials.invalid:
    # Create a flow object. (it assists with OAuth 2.0 steps to get user authorization + credentials)
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        credentials = tools.run_flow(flow, store)
        print('Storing credentials to ' + credential_path)

    return credentials



def SendMessage(sender, to, subject, msgHtml, msgPlain):
    credentials = get_credentials() 

    http = httplib2.Http()  # Create an httplib2.Http object to handle our HTTP requests, and authorize it using credentials.authorize()

    # http is the authorized httplib2.Http() 
    http = credentials.authorize(http)

    service = discovery.build('gmail', 'v1', http=http)

    message_with_attach = create_message_without_attachment(sender, to, subject, msgHtml, msgPlain)
    SendMessageInternal(service, "me", message_with_attach)


def SendMessageInternal(service, user_id, message): 
    try:
        message = (service.users().messages().send(userId=user_id, body=message).execute())  ####need  to get user_id before

        message_ID = message['id']
        print(f'Message Id: {message_ID}')
        return [message, message_ID] #return value as list
    except errors.HttpError as error:
        print(f'An error occurred: {error}')    

def create_message_with_attachment(sender, to, subject, msgHtml, msgPlain):

    # multipart container can contain other MIME parts.  (attachment will be independent of the multipart/alternative)
    msg = MIMEMultipart('alternative')
    msg['To'] = to
    msg['From'] = sender
    msg['Subject'] = subject

    # convert both part to a MIME compatible string
    part1 = MIMEText(msgPlain, 'plain') 
    part2 = MIMEText(msgHtml, 'html')

    # create .txt attachment
    filePath=r"C:\Users\me\Desktop\test_Attachment.txt"
    myFile=open(filePath, "rb")
    attachment= MIMEApplication(myFile.read())
    msg.set_payload(myFile) # 
    myFile.close()
    msg.set_payload(myFile) # 
    myFile.close()

    #This will add a header that looks like: "Content-Disposition: attachment; filename="test_Attachment.txt" "
    attachment.add_header('content-disposition', 'attachment', filename = ('utf-8', '', 'test_Attachment.txt'))

    # Attach parts into message container.
    msg.attach(attachment)
    msg.attach(part1)
    msg.attach(part2)

    # Encode the payload using Base64.
    raw = encoders.encode_base64(msg)
    return raw


def main():
    to = "youremail@gmail.com"
    sender = "myemail@gmail.com"
    subject = "subject test1"
    msgHtml = r'Hi<br/>Html <b>hello</b>'
    msgPlain = "Hi\nPlain Email"
    message_text= "this is message text"
    SendMessage(sender, to, subject, msgHtml, msgPlain)


if __name__ == '__main__':
    main()

此功能在此代碼中成功發送無附件的電子郵件:

def create_message_without_attachment (sender, to, subject, msgHtml, msgPlain):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = to
    msg.attach(MIMEText(msgPlain, 'plain'))
    msg.attach(MIMEText(msgHtml, 'html'))

    raw = base64.urlsafe_b64encode(msg.as_bytes())
    raw = raw.decode()
    body = {'raw': raw}
    return body

Edit1(避免重復相同的答案):您將在答案中找到發送帶有(或不帶有)附件的電子郵件所需的代碼(和解釋)。

Edit2:由於 randomfigure 改進了代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM