Using Python to manipulate mailboxes

Keywords: Excel encoding

1, Mailbox adds interfaces for software intervention, such as our mailbox verification and mailbox login, but only if we need to configure them,

Take QQ email and Netease email as examples:

QQ email: first click the setting button

QQ email: select Account tab

QQ email: find this service

QQ email: then send SMS to the designated number according to the prompt to obtain the authorization code

2, The concrete realization of code sending mail

#SMTP = > Simple Mail Transfer Protocol
import smtplib   
import email   #The file name cannot have the same name as the imported Library
from email.mime.image import MIMEImage   #Picture type mail
from email.mime.text import MIMEText # MIME is mostly used in mail extension protocol
from email.mime.multipart import MIMEMultipart #Create attachment type

HOST = 'smtp.qq.com'  #Call mailbox borrow excuse
SUBJECT = 'Today is August 10, 2018, who is the 18th birthday'#Set message title
FROM = '1468630684@qq.com'#The sender's mailbox needs to be set to enable the smtp protocol first
TO = 'hsiangshuai@163.com,1468630684@qq.com'#Set the recipient's mailbox (can be sent to more than one person at a time)
message=MIMEMultipart('related')#Message information, content is empty  #Equivalent to envelope##related means to send mail to the other party in the form of embedded resources
#Send the email subject to the mailbox of the other party,
#Parameter 1. The content must be a string
        # 2. Content form, text type is plain by default
        # 3. Use utf-8 for content coding
# message_html=MIMEText('shuai123 kill no line ',' plain','utf-8 ')
#Load message content into message information
message_html=MIMEText('<h1 style="color:red;font-size:100px">study hard and make progress every day</h1><img src="cid:small">','html','utf-8')
message.attach(message_html)

# ===========Send picture-=============
image_data=open('1.gif','rb')
message_image = MIMEImage(image_data.read())
#Close the file you just opened
image_data.close()
# (222)
message_image.add_header('Content-ID','small')
#Add picture file to email message
message.attach(message_image)
#(333)
message_image = MIMEText(open('1.gif','rb').read(),'base64','utf-8')
message_image['Content-disposition'] = 'attachment;filename="happy.gif"'
message.attach(message_image)
#===========The xlsx file is sent as content to the mailbox of the other party to read excel and rb,
# ==For MIMEText(), the default encoding form is base64. For binary files, base64 is not set, and garbled code will appear==========
message_xlsx = MIMEText(open('table.xlsx','rb').read(),'base64','utf-8')
#Set the name of the file in the attachment
message_xlsx['Content-Disposition'] = 'attachment;filename="test1111.xlsx"'
message.attach(message_xlsx)

#Set message sender
message['From']=FROM
#Set mail recipient
message['TO']=TO
#Set message title
message['Subject']=SUBJECT
#Get the certificate of transmission protocol of jiangjianyou Award
email_client = smtplib.SMTP_SSL()
email_client.connect(HOST,'465')
#Set send domain name, port 465
result=email_client.login(FROM,'rehotdvftldxgfdf')#qq
# result=email_client.login(FROM,'xs147258')#NetEase 163


print('Login results',result)

email_client.sendmail(from_addr=FROM,to_addrs=TO.split(','),msg=message.as_string())
#Close mail sending client
email_client.close()

 

Posted by Drachlen on Sat, 04 Jan 2020 20:13:45 -0800