This example uses the python 2.7 environment, and python 3 should operate similarly.
Two packages, smtplib and email, are required.
Send text type mail
Here's an example of sending text messages (using SMTP from NetEase 163):
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import formataddr # Third-party SMTP mail_host = "smtp.163.com" # SMTP server mail_user = "sender@163.com" # Sender Mailbox mail_pass = "******" # Client Authorization Number for Mailbox sender = "sender@163.com" # Sender of mail recivers = ["bob@qq.com", "someone@gmail.com"] # Mail recipient, you can specify more than one # Three parameters: the first is the text content, the second formats the text, and the third sets the character encoding message = MIMEText('Python Mail Sending Test', 'plain', 'utf-8') message['From'] = sender; # == message['From'] = formataddr(['sender', sender]) message['To'] = ", ".join(recivers) # == message['To] = formataddr(['ok', ', '.join(recivers)]) subject = 'Python Mail Test' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mali_host, 25) # Connect SMTP, port 25 smtpObj.set_debuglevel(1) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, recivers,message.as_string()) print "emails send successfully" except smtplib.SMTPException: print "Error:cannot send emails" smtpObj.quit() # Close Connection
It looks like NetEase has a hole in its SMTP. Messages ['From'] and messages ['To'] need to be consistent with sender and recivers. Titles and content should not have sensitive words as much as possible. Otherwise, they will be judged spam by the server. For the first time, I was blocked because Subject had "SMTP" (funny.gif).
If the message fails to send, you can check the status code returned by the set_debuglevel() function to determine why.
Send mail in HTML format
Unlike sending text, setting _subtype in MIMEText to HTML and sending HTML with pictures also creates an instance of MIMEMultipart().
# -*- coding: UTF-8 -*- import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage # Third-party SMTP mail_host = "smtp.163.com" # SMTP server mail_user = "sender@163.com" # Sender Mailbox mail_pass = "******" # Client Authorization Number for Mailbox sender = "sender@163.com" recivers = ["bob@qq.com", "alice@qq.com"] # Receive mail msg = MIMEMultipart('related') msg['From'] = sender msg['To'] = ", ".join(recivers) subject = 'HTML 1m4g3' msg['Subject'] = Header(subject, 'utf-8') mail_msg = """ <h1>HTML image test</h1> <p><a href="http://www.baidu.com">learn more</a></p> <p><img src="cid:image1"></p> """ msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) # Specify the picture of the current directory fp = open('test1.gif', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # Define picture ID s, referenced in HTML msgImage.add_header('Content-ID', '<image1>') msg.attach(msgImage) try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 SMTP Port smtpObj.set_debuglevel(1) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, recivers, msg.as_string()) print "emails send sucessfully" except smtplib.SMTPException: print "Error:cannot send emails" smtpObj.quit()
Send mail with attachments
# -*- coding: UTF-8 -*- import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage # Third-party SMTP mail_host = "smtp.163.com" # Set up the server mail_user = "sender@163.com" mail_pass = "******" sender = "sender@163.com" recivers = ["bob@qq.com", "alice@qq.com"] # Receive mail # Create attachment instance msg = MIMEMultipart() msg['From'] = sender msg['To'] = ", ".join(recivers) subject = 'Mail Attachments' msg['Subject'] = Header(subject, 'utf-8') # Mail body: MIMEText('The message body is as follows', 'html', 'utf-8') # Construct an attachment to transfer the txt file in the current directory: att1 = MIMEText(open('test1.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # The filename here can be named any way you like, as it will appear in the mail att1["Content-Disposition"] = 'attachment;filename="test_1.txt"' msg.attach(att1) try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 SMTP Port smtpObj.set_debuglevel(1) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, recivers, msg.as_string()) print "emails send sucessfully" except smtplib.SMTPException: print "Error:cannot send emails" smtpObj.quit()
Sending normal returns 250 status codes:
554 returned from send failure:
summary
One way to do this is to find out how the smart contract CTF flag s out of mailbox, the other is to supplement programming (you type code like Cai Xukun.gif).
All in all, there were many pits in it, and most of them were 554 returned (probably intercepted for spam), probably because some of the Subject's words didn't match (like "test" would be intercepted).Also, when sending in bulk, recivers is a list, so the message['To'] needs to be converted to a string with commas and join() functions.
over!