import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
import string
length = len(sys.argv)
if length !=2:
print '参数不正确'
else:
argument = sys.argv[1]
mailto_list = string.split(argument,',')
mail_host="smtp.exmail.qq.com"
mail_user="123@qq.com"
mail_pass="123456"
text = "hello world"
html="""\
<html>
<head></head>
<body>
<p>
Hello!<br>
This is the link:<br>
<a href="http://www.baidu.com">link</a>
</p>
</body>
</html>
"""
part1=MIMEText(text,'plain')
part2=MIMEText(html,'html')
subject='Email-Html From Python'
def send_mail(mail_list,sub,text,html):
msg=MIMEMultipart('alternative')
msg.attach(text)
msg.attach(html)
msg['Subject'] = subject
msg['From'] = mail_user
msg['To'] = ";".join(mail_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(mail_user, mail_list, msg.as_string())
s.quit()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto_list,subject,part1,part2):
print "send succeed"
else:
print "send failed"
python学习笔记------发送邮件(文本和html)
最新推荐文章于 2023-10-18 09:58:38 发布