Python Learning notes Send mail __python

Source: Internet
Author: User

Before sending a message, you need to introduce it in the file header

From Sgmllib import Sgmlparser
Import Smtplib
From Email.mime.text import Mimetext
From email. Mimemultipart Import Mimemultipart
Import datetime

#定义发送邮件的函数
def send_mail (message):
    today = Datetime.date.today ()
     currentday=today.strftime ('%y-%m-%d ')
    msg = Mimemultipart ()
      '
    att = Mimetext (open (R ' D:\report\aa.xlsx ', ' RB '). Read (), ' Base64 ', ' gb2312 ') #设置附件的目录
    att[' content-type '] = ' application/octet-stream '
     att[' content-disposition '] = ' attachment;filename= ' imd_ebm.xlsx ' #设置附件的名称
     msg.attach (att)
     "
    content = str (message) #正文内容
    body = mimetext (content, ' plain ', ' GBK ') #设置字符编码
    msg.attach (body )

Msgto = [' 123@126.com '] # recipient address multiple contacts, format [' aa@163.com '; ' Bb@163.com ']
Msgfrom = ' 321@126.com ' # return address,
msg[' subject ' = ' Finish at ' currentday #主题
msg[' Date ']=time.ctime () #时间
#msg [' Cc ']= ' bb@junbao.net ' #抄送人地址 multiple addresses do not work


Mailuser = ' 321 ' # User name
mailpwd = ' 321 ' #密码

    try:
        smtp = Smtplib. SMTP ()
        smtp.connect (R ' smtp.126.com ') # SMTP settings
         smtp.login (Mailuser, mailpwd) #登录
         smtp.sendmail (Msgfrom, Msgto, msg.as_string ()) #发送
         smtp.close ()
        print "Success Mail"
     except Exception, E:
        print e

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.