I. how to send HTML right click
1. Prepare HTML code as content
2. Set the message subtype to html
3. send
4. For example: send an HTML file to yourself
from email.mime.text import MIMEText main_content = """ <!DOCTYPE html> <html lang = "en" <head> <meta charset = "UTF-8"> <title>Example</title> </head> <body> <h1>This is for testing html<h1> </body> </html> """ msg = MIMEText(main_content,"html","utf-8") #Build sender address and login information from_addr = "1215217867@qq.com" from_pwd = "" #Building message recipients' information to_addr = "1215217867@qq.com" smtp_srv = "smtp.qq.com" try: import smtplib srv = smtplib.SMTP_SSL(smtp_srv.encode(),465) srv.login(from_addr,from_pwd) srv.sendmail(from_addr,[to_addr],msg.as_string()) srv.quit() except Exception as a: print(a)
II. Send email with attachments
1. You can think of an email as a combination of a text email and an attachment
2. If a message involves multiple parts, it needs to be built in mimemiultipart format
3. Add a mime text body
4. Add a mime base or MEMEText as an attachment
5. For example:
from email.mime.text import MIMEText#Build attachment usage from email.mime.multipart import MIMEBase,MIMEMultipart#Building basic mail usage mail_mul = MIMEMultipart()#Building a mail object mail_text = MIMEText("Hello,I am liudana","plain","utf-8")#Build message body mail_mul.attach(mail_text)#Attach the built message body to the message #To build an attachment, you need to read the attachment locally #Open a local file #with rb Format open with open("00.TestCasePython.py","rb") as f: s = f.read() #Set attachment's MIME And file name m = MIMEText(s,"base64","utf-8")#Type is base64,This is the format of the body of the email. Just remember here m["Content-Type"] = "application/octet-stream" #Need attention #1.attachment English status of last semicolon #2.filename You need to wrap it in quotation marks. Please be careful to stagger it with the outer quotation marks m["Content-Disposition"] = "attachment;filename = '00.TestCasePython.py'" #Add to MIMEMultipart mail_mul.attach(m) #Build sender address and login information from_addr = "1215217867@qq.com" from_pwd = "ysqmojzwkgfciccd" #Building message recipients' information to_addr = "1215217867@qq.com" smtp_srv = "smtp.qq.com" try: import smtplib srv = smtplib.SMTP_SSL(smtp_srv.encode(),465) srv.login(from_addr,from_pwd) srv.sendmail(from_addr,[to_addr],mail_mul.as_string()) srv.quit() except Exception as a: print(a)
Three, source code
D55_2_HTMLMailSend.py
D55_3_SendAttachmentMail.py
https://github.com/ruigege66/Python_learning/blob/master/D55_2_HTMLMailSend.py
https://github.com/ruigege66/Python_learning/blob/master/D55_3_SendAttachmentMail.py
2.CSDN: https://blog.csdn.net/weixin_44630050
3. Blog Park: https://www.cnblogs.com/ruige0000/
4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials.