How do I send HTML Formatted emails, through the gmail-api for python

前端 未结 2 485
不知归路
不知归路 2021-02-02 07:50

Using the sample code from the GMail API Example: Send Mail, and after following rules for authentication, it\'s simple enough to send a programmatically generated email, via a

2条回答
  •  面向向阳花
    2021-02-02 08:02

    Try this:

        def CreateMessage(emailSubject, emailTo, emailFrom, message_body, emailCc, html_content=None):
            try:
                message = MIMEMultipart('alternative')
                message['to'] = emailTo
                message['from'] = emailFrom
                message['subject'] = emailSubject
                message['Cc'] = emailCc
                body_mime = MIMEText(message_body, 'plain')
                message.attach(body_mime)
                if html_content:
                    html_mime = MIMEText(html_content, 'html')
                    message.attach(html_mime)
                return {
                    'raw': base64.urlsafe_b64encode(
                        bytes(
                            message.as_string(),
                            "utf-8")).decode("utf-8")}
            except Exception as e:
                print('Error in CreateMessage()', e)
                return '400'
    

提交回复
热议问题