可以先去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."