How to send email attachments?

前端 未结 13 923
梦谈多话
梦谈多话 2020-11-22 01:14

I am having problems understanding how to email an attachment using Python. I have successfully emailed simple messages with the smtplib. Could someone please e

相关标签:
13条回答
  • 2020-11-22 01:49
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.MIMEImage import MIMEImage
    import smtplib
    
    msg = MIMEMultipart()
    msg.attach(MIMEText(file("text.txt").read()))
    msg.attach(MIMEImage(file("image.png").read()))
    
    # to send
    mailer = smtplib.SMTP()
    mailer.connect()
    mailer.sendmail(from_, to, msg.as_string())
    mailer.close()
    

    Adapted from here.

    0 讨论(0)
提交回复
热议问题