#信息
#发送人以及邮件服务器信息
SenderInfo = {
"host" : "smtp.qq.com",
"user" : "[email protected]",
"passwd" : "**********", #你的授权码
"from" : "Dcp<[email protected]>",
"postfix" : "qq.com"
}
#接收人
Receivers=["[email protected]"]
获取邮件的信息
def get_msg(msg_type):
msg=None
if msg_type == 1: #普通邮件
msg = MIMEText("Can I get your money?",_subtype='plain')
msg["Subject"] = "Your Money is Coming..."
name="Someone"
msg["From"]=formataddr(
(Header(name,"utf-8").encode(),"%s@%s"%(SenderInfo["user"],SenderInfo['postfix']))
)
msg['To']=";".join(Receivers)
elif msg_type == 2: #富文本邮件
content="""
Dcp's python email
Dcp's babys:
Happy New Year!
"""
msg = MIMEText(content,_subtype='html')
msg["Subject"] = "Your Red Bag is Coming.."
name="Money"
msg["From"]=formataddr(
(Header(name,"utf-8").encode(),"%s@%s"%(SenderInfo["user"],SenderInfo['postfix']))
)
msg['To'] = ";".join(Receivers)
elif msg_type == 3: #带附件的Email
msg = MIMEMultipart()
name="Money"
msg["Subject"] = "Your Money is Coming..."
msg["From"] = formataddr(
(Header(name, "utf-8").encode(), "%s@%s" % (SenderInfo["user"], SenderInfo['postfix']))
)
msg['To'] = ";".join(Receivers)
# msg.attach(MIMEText("Your Red Bag is Coming...","plain","utf-8"))
# att1 = MIMEBase('application','pdf',filename=('utf-8','','Red_Bag.pdf'))
# att1.add_header('Content-Disposition','attachment',filename=('gb2312','','Red_bag.pdf'))
# att1.add_header('Content-ID',<0>)
# att1.add_header('X-Attachment_Id','0')
# att1.set_payload(open('Red_Bag.pdf','rb').read())
# encode_base64(att1)
# msg.attach(att1)
# att2=MIMEText(open('Red_Bag.txt','rb').read(),'base64','utf-8')
# att2["Content-Type"]='application/octet-stream'
# att2["Content-Disposition"]="attachment";filename="Red_bage.txt"
# msg.attach(att2)
with open('Red_bag.png','rb') as f:
att3 = MIMEBase('image','png',filename='Red_Bag.png')
att3.add_header('Content-Disposition','attachment',filename='Red_Bag.png')
att3.add_header('Content-ID','<0>')
att3.add_header('X-Attachment-Id','0')
att3.set_payload(f.read())
encode_base64(att3)
msg.attach(att3)
return msg
发送邮件
def Send_Email(receivers,sender_info,messages):
try:
server = SMTP_SSL()
server.connect(sender_info["host"])
server.login(sender_info["user"],sender_info["passwd"])
server.sendmail(sender_info["from"],receivers,messages.as_string())
server.close()
return True
except Exception as e:
print (str(e))
return False
主函数:
if __name__=="__main__":
for i in range(1,4):
msg=get_msg(i)
ret=Send_Email(Receivers,SenderInfo,msg)
print ("Massage Send %s"%("Successfully" if ret else "Faild."))
完整代码:
from smtplib import SMTP_SSL
from email.encoders import encode_base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.header import Header
from email.utils import formataddr
SenderInfo = {
"host" : "smtp.qq.com",
"user" : "[email protected]",
"passwd" : "*********", #你的授权码
"from" : "Dcp<[email protected]>",
"postfix" : "qq.com"
}
# Receivers=["[email protected]","1829182801","[email protected]","[email protected]","[email protected]"]
Receivers=["[email protected]"]
def Send_Email(receivers,sender_info,messages):
try:
server = SMTP_SSL()
server.connect(sender_info["host"])
server.login(sender_info["user"],sender_info["passwd"])
server.sendmail(sender_info["from"],receivers,messages.as_string())
server.close()
return True
except Exception as e:
print (str(e))
return False
def get_msg(msg_type):
msg=None
if msg_type == 1:
msg = MIMEText("Can I get your money?",_subtype='plain')
msg["Subject"] = "Your Money is Coming..."
name="Someone"
msg["From"]=formataddr(
(Header(name,"utf-8").encode(),"%s@%s"%(SenderInfo["user"],SenderInfo['postfix']))
)
msg['To']=";".join(Receivers)
elif msg_type == 2:
content="""
Dcp's python email
Dcp's babys:
Happy New Year!
"""
msg = MIMEText(content,_subtype='html')
msg["Subject"] = "Your Red Bag is Coming.."
name="Money"
msg["From"]=formataddr(
(Header(name,"utf-8").encode(),"%s@%s"%(SenderInfo["user"],SenderInfo['postfix']))
)
msg['To'] = ";".join(Receivers)
elif msg_type == 3:
msg = MIMEMultipart()
name="Money"
msg["Subject"] = "Your Money is Coming..."
msg["From"] = formataddr(
(Header(name, "utf-8").encode(), "%s@%s" % (SenderInfo["user"], SenderInfo['postfix']))
)
msg['To'] = ";".join(Receivers)
# msg.attach(MIMEText("Your Red Bag is Coming...","plain","utf-8"))
# att1 = MIMEBase('application','pdf',filename=('utf-8','','Red_Bag.pdf'))
# att1.add_header('Content-Disposition','attachment',filename=('gb2312','','Red_bag.pdf'))
# att1.add_header('Content-ID',<0>)
# att1.add_header('X-Attachment_Id','0')
# att1.set_payload(open('Red_Bag.pdf','rb').read())
# encode_base64(att1)
# msg.attach(att1)
# att2=MIMEText(open('Red_Bag.txt','rb').read(),'base64','utf-8')
# att2["Content-Type"]='application/octet-stream'
# att2["Content-Disposition"]="attachment";filename="Red_bage.txt"
# msg.attach(att2)
with open('Red_bag.png','rb') as f:
att3 = MIMEBase('image','png',filename='Red_Bag.png')
att3.add_header('Content-Disposition','attachment',filename='Red_Bag.png')
att3.add_header('Content-ID','<0>')
att3.add_header('X-Attachment-Id','0')
att3.set_payload(f.read())
encode_base64(att3)
msg.attach(att3)
return msg
if __name__=="__main__":
for i in range(1,4):
msg=get_msg(i)
ret=Send_Email(Receivers,SenderInfo,msg)
print ("Massage Send %s"%("Successfully" if ret else "Faild."))