发布时间:2019-09-12 07:55:51编辑:auto阅读(1576)
可以先去Mailgun注册一个免费的programmable mail servers,免费的有每天200封邮件的限制。
Mailgun is a set of powerful APIs that allow you to send, receive, track and store email effortlessly.
Mailgun提供一系列强大的API,使得你可以毫无费力的实现邮件的发送,接收,跟踪和存储。
也可以不注册,直接使用自己的邮箱服务提供商的SMTP进行测试。
下面是mail.py的全部代码
- #!/usr/bin/env python
- #coding:utf-8
- import sys
- import smtplib
- from email.MIMEMultipart import MIMEMultipart
- from email.MIMEText import MIMEText
- MAIL_HOST='smtp.mailgun.org'
- MAIL_ADMIN='user1@z42.mailgun.org'
- MAIL_PORT=587
- MAIL_USER='postmaster@z42.mailgun.org'
- MAIL_PASSWORD='*******'
- def main():
- if len(sys.argv)<4:
- raise Exception('argument number must be 3')
- to=sys.argv[1]
- title=sys.argv[2]
- content=sys.argv[3]
- smtp=smtplib.SMTP(MAIL_HOST,MAIL_PORT)
- smtp.login(MAIL_USER,MAIL_PASSWORD)
- msg=MIMEMultipart()
- msg['From']=MAIL_ADMIN
- msg['To']=to
- msg['Subject']=title
- msg.attach(MIMEText(content))
- print(msg)
- smtp.sendmail(MAIL_ADMIN,to,msg.as_string())
- smtp.quit()
- if __name__=='__main__':
- main()
使用下面的命令行发送邮件
- >python mail.py jorden008@163.com "hi, it's me" "hello, andy. This is mail content."
关于smtplib的使用,大家可以参考
上一篇: hbase+python安装部署及操作
下一篇:
python命令行
47848
46402
37290
34738
29321
25977
24922
19955
19549
18032
5796°
6420°
5936°
5965°
7071°
5919°
5950°
6444°
6407°
7786°