Use Python to finish sending messages
1, the basic information required by the mail
2. Python sends mail
1, the basic information required by the mail
Sender's server, sender, recipient (whether multiple), subject, content, whether there are attachments
2. Python sends mail
Import Smtplib
Import Mimetext #此模块可以用于发送正文
Import Mimemultipart #此模块用于发送带附件的邮件
#邮件基础信息配置
"Smtp.163.com" #发件服务器, QQ mailbox for smtp.qq.com
0 #端口, QQ mailbox is 465
"[Email protected]" #发件箱
"XXXX" #qq邮箱为授权码
"[Email protected]" #收件箱, multiple mailboxes for ["[Email protected]","[email protected]"]
Msg=mimemultipart ()
msg["from"]=sender
msg["to"]=receiver #发给单个邮箱
#msg ["to"]=";". Join (receiver) #发给多个邮箱
msg["subject"]="test"
Body=mimetext (Mail_body,"html","Utf-8")
Msg.attach (body) #读取附件中的正文作为邮件正文
#附件信息的配置
File_path=r"D:\result.html" #读取本地的一个一个测试报告
With open (File_path, asfp:
Mail_body=fp.read ()
Att=mimetext (Mail_body,"Basse64","Utf-8")
att["content-type"]="Application/octet-stream "
att["content-disposition"]=' attachment; filename= "test_report.html" ' #将系统中的result. HTML rename as Attachment send
Msg.attach (ATT)
#兼容一般邮箱和qq邮箱
Try
SMTP = Smtplib. SMTP ()
Smtp.connect (smtpserver) #连接服务器, general mailbox, such as 163
Smtp.login (SENDER,PSW) #登录
Except
Smtp=smtplib. Smtp_ssl (Smtpserver,port) #连接服务器, QQ mailbox
Smtp.login (SENDER,PSW) #登录
Smtp.sendmail (Sender,receiver,msg.as_string ()) #发送
Smtp.quit () #关闭
Python Send mail