2017-03-04 7 views
1

이 python 3 스크립트는 이메일을 작성하고 URL을 사용하여 하나의 파일을 첨부하여 전송하는 것으로 가정합니다. 그것은 이메일을 보내지 만, 뭔가 내가 구글 문서를 읽어 않았다 create_message_with_attachment()python 3.6 gmail api - 첨부 파일이있는 이메일 보내기

TypeError: Attach is not valid on a message with a non-multipart payload

으로 잘못. 스택 스레드는 멋진 첨부 파일 스타일에 초점을 맞추고, 그 위에는 파이썬 버전의 다른 구문을 혼합합니다.

코드는 여러 출처의 패치 워크입니다. 나는 create_message_with_attachment()에서 그들을 함께 모으기 위해 고투한다. 나는이를 포함해야하는 경우 모르겠어요 예를 들어

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

(. 그것은 하단에이 코드 Cf를 작동 create_message_without_attachment()에서의) 첨부 코드로 생성 메시지 :

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 = "[email protected]" 
    sender = "[email protected]" 
    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 
+1

로그를 추가 할 수 있습니까? 'msg.set_payload (contents)'와'encoder를 추가하여보고 된 티켓에서 [solution] (https://github.com/google/google-api-python-client/issues/93)을 따라 할 수 있습니다. encode_base64 (msg)'. 희망이 도움이 –

+0

@ Mr.Rebot는 소중한 링크에 대해 많은 감사합니다! 나는 그 코드에 대한 나의 해석을 사용하여 코드를 업데이트했다. (그 사람은 전체 코드를 게시하지 않았기 때문에 "나는 또한 아래 두번째 라인을 추가해야했다"는 것을 정말로 이해하지 못했다.) 코드는'TypeError : Attach가 유효하지 않다. non-multipart 페이로드가있는 메시지' 작동하는 함수에서'raw = .... '부분을 추가해야하는지 이해할 수 없습니다 (내 질문 하단의 create_message_without_attachment() 참조) – JinSnow

+0

여기에서 답변을 찾을 수 있습니다. http://stackoverflow.com/a/37267330/1486850 – JinSnow

답변

0

다음은 첨부 파일과 함께 또는 첨부 파일없이 전자 메일을 보내는 데 필요한 코드 및 설명입니다.

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

#needed for attachment 
import smtplib 
import mimetypes 
from email import encoders 
from email.message import Message 
from email.mime.audio import MIMEAudio 
from email.mime.base import MIMEBase 
from email.mime.image import MIMEImage 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.application import MIMEApplication 
#List of all mimetype per extension: http://help.dottoro.com/lapuadlp.php or http://mime.ritey.com/ 

from apiclient import errors, discovery #needed for gmail service 




## About credentials 
# There are 2 types of "credentials": 
#  the one created and downloaded from https://console.developers.google.com/apis/ (let's call it the client_id) 
#  the one that will be created from the downloaded client_id (let's call it credentials, it will be store in C:\Users\user\.credentials) 


     #Getting the CLIENT_ID 
      # 1) enable the api you need on https://console.developers.google.com/apis/ 
      # 2) download the .json file (this is the CLIENT_ID) 
      # 3) save the CLIENT_ID in same folder as your script.py 
      # 4) update the CLIENT_SECRET_FILE (in the code below) with the CLIENT_ID filename 


     #Optional 
     # If you don't change the permission ("scope"): 
      #the CLIENT_ID could be deleted after creating the credential (after the first run) 

     # If you need to change the scope: 
      # you will need the CLIENT_ID each time to create a new credential that contains the new scope. 
      # Set a new credentials_path for the new credential (because it's another file) 
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, 'cred send mail.json') 

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

    if not credentials or credentials.invalid: 
     CLIENT_SECRET_FILE = 'client_id to send Gmail.json' 
     APPLICATION_NAME = 'Gmail API Python Send Email' 
     #The scope URL for read/write access to a user's calendar data 

     SCOPES = 'https://www.googleapis.com/auth/gmail.send' 

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

    return credentials 




## Get creds, prepare message and send it 
def create_message_and_send(sender, to, subject, message_text_plain, message_text_html, attached_file): 
    credentials = get_credentials() 

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

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

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

    ## without attachment 
    # message_without_attachment = create_message_without_attachment(sender, to, subject, message_text_html, message_text_plain) 
    # send_Message_without_attachement(service, "me", message_without_attachment, message_text_plain) 


    ## with attachment 
    message_with_attachment = create_Message_with_attachment(sender, to, subject, message_text_plain, message_text_html, attached_file) 
    send_Message_with_attachement(service, "me", message_with_attachment, message_text_plain,attached_file) 

# def create_message_without_attachment (sender, to, subject, message_text_html, message_text_plain): 
    # #Create message container 
    # message = MIMEMultipart('alternative') # needed for both plain & HTML (the MIME type is multipart/alternative) 
    # message['Subject'] = subject 
    # message['From'] = sender 
    # message['To'] = to 

    # #Create the body of the message (a plain-text and an HTML version) 
    # message.attach(MIMEText(message_text_plain, 'plain')) 
    # message.attach(MIMEText(message_text_html, 'html')) 

    # raw_message_no_attachment = base64.urlsafe_b64encode(message.as_bytes()) 
    # raw_message_no_attachment = raw_message_no_attachment.decode() 
    # body = {'raw': raw_message_no_attachment} 
    # return body 



def create_Message_with_attachment(sender, to, subject, message_text_plain, message_text_html, attached_file): 
    """Create a message for an email. 

    message_text: The text of the email message. 
    attached_file: The path to the file to be attached. 

    Returns: 
    An object containing a base64url encoded email object. 
    """ 

    ##An email is composed of 3 part : 
     #part 1: create the message container using a dictionary { to, from, subject } 
     #part 2: attach the message_text with .attach() (could be plain and/or html) 
     #part 3(optional): an attachment added with .attach() 

    ## Part 1 
    message = MIMEMultipart() #when alternative: no attach, but only plain_text 
    message['to'] = to 
    message['from'] = sender 
    message['subject'] = subject 

    ## Part 2 (the message_text) 
    # The order count: the first (html) will be use for email, the second will be attached (unless you comment it) 
    message.attach(MIMEText(message_text_html, 'html')) 
    message.attach(MIMEText(message_text_plain, 'plain')) 

    ## Part 3 (attachement) 
    # # to attach a text file you containing "test" you would do: 
    # # message.attach(MIMEText("test", 'plain')) 

    #-----About MimeTypes: 
    # It tells gmail which application it should use to read the attachement (it acts like an extension for windows). 
    # If you dont provide it, you just wont be able to read the attachement (eg. a text) within gmail. You'll have to download it to read it (windows will know how to read it with it's extension). 

    #-----3.1 get MimeType of attachment 
     #option 1: if you want to attach the same file just specify it’s mime types 

     #option 2: if you want to attach any file use mimetypes.guess_type(attached_file) 

    my_mimetype, encoding = mimetypes.guess_type(attached_file) 

    # If the extension is not recognized it will return: (None, None) 
    # If it's an .mp3, it will return: (audio/mp3, None) (None is for the encoding) 
    #for unrecognized extension it set my_mimetypes to 'application/octet-stream' (so it won't return None again). 
    if my_mimetype is None or encoding is not None: 
     my_mimetype = 'application/octet-stream' 


    main_type, sub_type = my_mimetype.split('/', 1)# split only at the first '/' 
    # if my_mimetype is audio/mp3: main_type=audio sub_type=mp3 

    #-----3.2 creating the attachement 
     #you don't really "attach" the file but you attach a variable that contains the "binary content" of the file you want to attach 

     #option 1: use MIMEBase for all my_mimetype (cf below) - this is the easiest one to understand 
     #option 2: use the specific MIME (ex for .mp3 = MIMEAudio) - it's a shorcut version of MIMEBase 

    #this part is used to tell how the file should be read and stored (r, or rb, etc.) 
    if main_type == 'text': 
     print("text") 
     temp = open(attached_file, 'r') # 'rb' will send this error: 'bytes' object has no attribute 'encode' 
     attachement = MIMEText(temp.read(), _subtype=sub_type) 
     temp.close() 

    elif main_type == 'image': 
     print("image") 
     temp = open(attached_file, 'rb') 
     attachement = MIMEImage(temp.read(), _subtype=sub_type) 
     temp.close() 

    elif main_type == 'audio': 
     print("audio") 
     temp = open(attached_file, 'rb') 
     attachement = MIMEAudio(temp.read(), _subtype=sub_type) 
     temp.close()    

    elif main_type == 'application' and sub_type == 'pdf': 
     temp = open(attached_file, 'rb') 
     attachement = MIMEApplication(temp.read(), _subtype=sub_type) 
     temp.close() 

    else:        
     attachement = MIMEBase(main_type, sub_type) 
     temp = open(attached_file, 'rb') 
     attachement.set_payload(temp.read()) 
     temp.close() 

    #-----3.3 encode the attachment, add a header and attach it to the message 
    encoders.encode_base64(attachement) #https://docs.python.org/3/library/email-examples.html 
    filename = os.path.basename(attached_file) 
    attachement.add_header('Content-Disposition', 'attachment', filename=filename) # name preview in email 
    message.attach(attachement) 


    ## Part 4 encode the message (the message should be in bytes) 
    message_as_bytes = message.as_bytes() # the message should converted from string to bytes. 
    message_as_base64 = base64.urlsafe_b64encode(message_as_bytes) #encode in base64 (printable letters coding) 
    raw = message_as_base64.decode() # need to JSON serializable (no idea what does it means) 
    return {'raw': raw} 



# def send_Message_without_attachement(service, user_id, body, message_text_plain): 
    # try: 
     # message_sent = (service.users().messages().send(userId=user_id, body=body).execute()) 
     # message_id = message_sent['id'] 
     # # print(attached_file) 
     # print (f'Message sent (without attachment) \n\n Message Id: {message_id}\n\n Message:\n\n {message_text_plain}') 
     # # return body 
    # except errors.HttpError as error: 
     # print (f'An error occurred: {error}') 




def send_Message_with_attachement(service, user_id, message_with_attachment, message_text_plain, attached_file): 
    """Send an email message. 

    Args: 
    service: Authorized Gmail API service instance. 
    user_id: User's email address. The special value "me" can be used to indicate the authenticated user. 
    message: Message to be sent. 

    Returns: 
    Sent Message. 
    """ 
    try: 
     message_sent = (service.users().messages().send(userId=user_id, body=message_with_attachment).execute()) 
     message_id = message_sent['id'] 
     # print(attached_file) 

     # return message_sent 
    except errors.HttpError as error: 
     print (f'An error occurred: {error}') 


def main(): 
    to = "[email protected]" 
    sender = "[email protected]" 
    subject = "subject test1" 
    message_text_html = r'Hi<br/>Html <b>hello</b>' 
    message_text_plain = "Hi\nPlain Email" 
    attached_file = r'C:\Users\Me\Desktop\audio.m4a' 
    create_message_and_send(sender, to, subject, message_text_plain, message_text_html, attached_file) 


if __name__ == '__main__': 
    main()