http://www.runoob.com/python/python-email.html
更多查看:https://docs.python.org/2/library/email-examples.html
python sendmail文本 text
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#发送text文本
import smtplib
from email.mime.text import MIMEText
mailto = ['952289652@qq.com','huanglinglin@myhexin.com']
mailhost = 'mail.myhexin.com'
mailuser = 'huanglinglin@myhexin.com'
mailpasswd = 'Hll952289652'
def send_mail(tolist, sub, content):
me = 'huanglinglin'+'<'+mailuser+'>'
msg = MIMEText(content, _subtype='plain', _charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ';'.join(tolist)
try:
server = smtplib.SMTP_SSL(mailhost, 465)
print 'connecting'
# server.connect(mailhost, 465)
print 'connect ok'
server.login(mailuser, mailpasswd)
server.sendmail(me, tolist, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto, 'testtest','this is test...'):
print "ok"
else:
print "failed"
#sendmail--- html
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
mailto = ['huanglinglin@myhexin.com']
mailhost = 'mail.myhexin.com'
mailuser = 'huanglinglin@myhexin.com'
mailpasswd = 'Hll952289652'
def send_mail(tolist, sub, content):
me = 'huanglinglin'+'<'+mailuser+'>'
msg = MIMEText(content, _subtype='html', _charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ';'.join(tolist)
try:
server = smtplib.SMTP_SSL(mailhost, 465)
print 'connecting'
# server.connect(mailhost, 465)
print 'connect ok'
server.login(mailuser, mailpasswd)
server.sendmail(me, tolist, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto, 'testtest','<h1>test</h1><a href=\'http://www.cnblogs.com/xiaowuyi\'>xiwuyi</a>'):
print "ok"
else:
print "failed"
#sendmial 附件
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#发送带附件邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件1
att1 = MIMEText(open('/root/cgi.py', 'rb').read(), 'base64', 'utf-8')
att1['Content-Type'] = 'application/octet-stream'
att1['Content-Disposition'] = 'attachment; filename="tcgi.py"'#这里的 filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)
#构造附件2
#...
mailto = ['952289652@qq.com','huanglinglin@myhexin.com']
mailhost = 'mail.myhexin.com'
mailuser = 'huanglinglin@myhexin.com'
mailpasswd = 'Hll952289652'
#加邮件头
msg['to'] = ';'.join(mailto)
msg['from'] = mailuser
msg['subject'] = 'hello world'
try:
server = smtplib.SMTP_SSL(mailhost, 465)
# server.connect(mailhost, 465)
server.login(mailuser, mailpasswd)
server.sendmail(mailuser, mailto, msg.as_string())
server.quit()
print 'True'
except Exception, e:
print str(e)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#发送带附件邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
#实例
msg = MIMEMultipart('alternative')
mailto = ['952289652@qq.com','huanglinglin@myhexin.com']
mailhost = 'mail.myhexin.com'
mailuser = 'huanglinglin@myhexin.com'
mailpasswd = 'Hll952289652'
msg['To'] = ';'.join(mailto)
msg['From'] = mailuser
msg['Subject'] = 'test'
html = """\
<html>
<head><title>测试</title></head>
<body>
<p>大家好!<br>
我爱你们。<br>
点击进入<a href="http://www.tuicool.com/articles/byINN3">WOW</a><br>
![](cid:image1)
</p>
</body>
</html>"""
content = MIMEText(html, 'html', 'utf-8')
msg.attach(content)
#构造图片
file = '/tmp/elk1.png'
image = MIMEImage(open(file, 'rb').read())
image.add_header('Content-ID', '<image1>')
msg.attach(image)
#构造附件
f = MIMEText(open(file,'rb').read(),'base64','utf-8')
f['Content-Type'] = 'application/octet-stream'
f['Content-Disposition'] = 'attachment; filename=\'123.png\''
msg.attach(f)
try:
server = smtplib.SMTP_SSL(mailhost, 465)
# server.connect(mailhost, 465)
server.login(mailuser, mailpasswd)
server.sendmail(mailuser, mailto, msg.as_string())
server.quit()
print 'True'
except Exception, e:
print str(e)
网友评论