- 论坛徽章:
- 54
|
回复 5# zl624867243 - #!/usr/bin/env python
- #_*_ coding:gbk _*_
- import datetime
- import sys
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage
-
- def send_mail(filename=[], picname=[], content_txt='', content_html=''):
- smtpserver = 'smtp.163.com'
- username = 'svn@163.com'
- password = '123123'
-
- msg = MIMEMultipart()
- msg['Subject'] = '中文邮件'
- msg['From'] = "svn@163.com"
- msg['To'] = "test@163.com"
-
- # attchment
- if len(filename) > 0:
- for i in filename:
- att = MIMEText(open(i, 'rb').read(), 'base64', 'gb2312')
- att["Content-Type"] = 'application/octet-stream'
- att["Content-Disposition"] = 'attachment; filename="%s"' % i.split('\\')[-1]
- msg.attach(att)
- # attchment picture
- if len(picname) > 0 and content_html != '':
- for i in range(0,len(picname)):
- #content_html = '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image%s"><br>good!' % i
- msg_content_html = MIMEText(content_html,'html','gb2312')
- msg.attach(msg_content_html)
-
- with open(picname[i], 'rb') as f:
- msgImage = MIMEImage(f.read())
-
- msgImage.add_header('Content-ID', '<image%s>' % (i + 1))
- msg.attach(msgImage)
-
- # content text
- if content_txt != '':
- msg_content_txt = MIMEText(content_txt,_subtype='plain',_charset='gb2312')
- msg.attach(msg_content_txt)
- # content html
- if content_html != '' and len(picname) == 0:
- msg_content_html = MIMEText(content_html,_subtype='html',_charset='gb2312')
- msg.attach(msg_content_html)
-
- smtp = smtplib.SMTP()
- smtp.connect(smtpserver)
- smtp.starttls()
- smtp.login(username, password)
- smtp.sendmail(msg['From'], msg['To'], msg.as_string())
- smtp.quit()
- send_mail(content_txt='这是中文测试邮件,仅仅是测试而已!')
复制代码 |
|