Sending HTML email using Python

前端 未结 10 1272
小鲜肉
小鲜肉 2020-11-22 04:57

How can I send the HTML content in an email using Python? I can send simple text.

10条回答
  •  甜味超标
    2020-11-22 05:16

    Simplest solution for sending email from Organizational account in Office 365:

    from O365 import Message
    
    html_template =     """ 
                
                
                    
                
                
                        {}
                
                
            """
    
    final_html_data = html_template.format(df.to_html(index=False))
    
    o365_auth = ('sender_username@company_email.com','Password')
    m = Message(auth=o365_auth)
    m.setRecipients('receiver_username@company_email.com')
    m.setSubject('Weekly report')
    m.setBodyHTML(final_html_data)
    m.sendMessage()
    

    here df is a dataframe converted to html Table, which is being injected to html_template

提交回复
热议问题