Sending email via gmail & python

前端 未结 7 681
走了就别回头了
走了就别回头了 2020-11-30 22:52

What is the recommended way of sending emails with gmail and python?

There are a lot of SO threads, but most are old and also smtp with username & password is n

7条回答
  •  感情败类
    2020-11-30 23:10

    I modified this as follows to work with Python3, inspired by Python Gmail API 'not JSON serializable'

    import httplib2
    import os
    import oauth2client
    from oauth2client import client, tools
    import base64
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from apiclient import errors, discovery
    
    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 to ' + 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 as error:
            print('An error occurred: %s' % error)
    
    def CreateMessage(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
    
    def main():
        to = "to@address.com"
        sender = "from@address.com"
        subject = "subject"
        msgHtml = "Hi
    Html Email" msgPlain = "Hi\nPlain Email" SendMessage(sender, to, subject, msgHtml, msgPlain) if __name__ == '__main__': main()

提交回复
热议问题