smtplib 发送邮件的简单实现

没事的时候写了一个python发邮件的toy小程序,仅供娱乐啊。。。
#!/usr/bin/env python
import smtplib,sys
server="smtp.126.com"
user="daidai141"
passwd="thisIsPassword"
send_addr="[email protected]"
recv_addr=""
subject=""
data=""
if len(sys.argv[1:]) < 1:
print """
Usage:%s received_address(request) subject
press enter,then input data,finally press "CTRL+D" to send
"""%(sys.argv[0])
sys.exit(1)
#msg format as follow:
#msg="""From:xxx
#to:xxx
#Subject:xxx
#
#data"""
try:
if len(sys.argv[1:]) ==1:
recv_addr=sys.argv[1]
subject="hello freiends from python!"
else:
recv_addr=sys.argv[1]
subject=sys.argv[2]
data=sys.stdin.read()
msg="""\
From:%s
to:%s
Subject:%s
%s"""%(send_addr,recv_addr,subject,data)

print msg,
smtp=smtplib.SMTP()
smtp.connect(server)
smtp.login(user,passwd)
smtp.sendmail(send_addr,recv_addr,msg)
smtp.quit()
print "\033[31msend successfully!\033[0m"
except :
print "input error!"


你可能感兴趣的:(smtplib)