Sending Gmail with an attachment that allows for 2 factor authentication in Python -


i'm trying alter code can send attachments via google , 2 factor authentication.

the below code works send emails i'm struggling getting work send attachments.

any guidance appreciated.

thanks!

import httplib2 import os import oauth2client oauth2client import client, tools import base64 email.mime.multipart import mimemultipart email.mime.text import mimetext apiclient import errors, discovery email.mime.base import mimebase email import encoders    scopes = 'https://www.googleapis.com/auth/gmail.send' client_secret_file = 'client_secret.json' application_name = 'gmail api python send email'  def get_credentials():     home_dir = os.path.expanduser('~')     credential_dir = os.path.join(home_dir, '.credentials')     if not os.path.exists(credential_dir):         os.makedirs(credential_dir)     credential_path = os.path.join(credential_dir, 'gmail-python-email-send.json')     store = oauth2client.file.storage(credential_path)     credentials = store.get()     if not credentials or credentials.invalid:         flow = client.flow_from_clientsecrets(client_secret_file, scopes)         flow.user_agent = application_name         credentials = tools.run_flow(flow, store)         print('storing credentials ' + credential_path)     return credentials  def sendmessage(sender, to, subject, msghtml, msgplain):     credentials = get_credentials()     http = credentials.authorize(httplib2.http())     service = discovery.build('gmail', 'v1', http=http)     message1 = createmessage(sender, to, subject, msghtml, msgplain)     sendmessageinternal(service, "me", message1)  def sendmessageinternal(service, user_id, message):     try:         message = (service.users().messages().send(userid=user_id, body=message).execute())         print('message id: %s' % message['id'])         return message     except errors.httperror error:         print('an error occurred: %s' % error)  def createmessage(sender, to, subject, msghtml, msgplain, files=[]):     msg = mimemultipart('alternative')     msg['subject'] = subject     msg['from'] = sender     msg['to'] =     msg.attach(mimetext(msgplain, 'plain'))     msg.attach(mimetext(msghtml, 'html'))      '''part = mimebase('application', "octet-stream")     part.set_payload(open("attachment.txt", "rb").read())     encoders.encode_base64(part)      part.add_header('content-disposition', 'attachment; filename="text.txt"')      msg.attach(part)'''      raw = base64.urlsafe_b64encode(msg.as_bytes())     raw = raw.decode()     body = {'raw': raw}     return body  def main():     = "email address"     sender = "email address"     subject = "automated test email"     msghtml = "hi<br/>html email"     msgplain = "hi\nplain email"     sendmessage(sender, to, subject, msghtml, msgplain)  if __name__ == '__main__':     main()   


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -