Automatic quick start -- python(17) -- mail sending

Posted by cybtec on Thu, 13 Jan 2022 02:06:50 +0100

It's safe here. After we ran the automated script, did we want to send the email together at one go? Complete a gorgeous real automation.

catalogue

zmail operation

Plain text sending:

Send in html format:

Send in attachment format:

encapsulation

zmail operation

   1. import zmail

   2. Mail content, including subject, content_text and attachments

   3. Sender information, including: sender account number, password (authorization code)

   4. Send mail, including: recipient address, mail content

   5. For mass mailing, the recipient address is written in a list, and multiple recipients are separated by commas

Plain text sending:

import zmail

#Mail content, subject content_text, cannot be defined arbitrarily
msg = {
    'subject':'Mail subject:Little sister's information',
    'content_text':'Height 172, long legs, high appearance...'
}
#Sender, authorization code
sender = ('123456798@qq.com','dfddfdfdfggrgrgc')
#addressee
reciver = '123456789@163.com'
#In the form of mass sending, the list here does not support changing to tuples
#reciver = ['123465789@163.com','12345678@qq.com']
# Login, index value, account and password
sender = zmail.server(sender[0],sender[1])#(* sender) is also called unpacking, which is automatically filled
# Send mail, which mail to launch, and what to send
sender.send_mail(reciver,msg)

It should be noted that the operation here is very simple. msg is a dictionary variable, which we set ourselves, but the key in it cannot be changed. It is specified in the library. Other variable names can be taken at will. The sender here is your account and authorization code.

Mass sending can also be carried out, but it needs to be in the form of list.

Here's how to get the authorization code. In your email, find the setting button, click to enter the setting interface, then find your account button, click to enter, and keep pulling down:

Open all these services:

Click generate authorization code and send SMS according to the operation. Click I have sent to get a string of authorization codes. Remember to put them away.    

Send in html format:

We can also send documents in HTML form, so this wave of operation is also a key. In the subsequent automatic mail sending, we need to compress the report, and how to send it in zip or rar or other forms.

# Method 1
comtent = '''
html content
'''
# Method 2
with open('email_msg.html','r',encoding='utf-8') as e:
    comtent =  e.read()
#If the text and html are written here, it will be overwritten by the html format and content
msg = {
    'subject':'Mail subject:Automated test report',
    'content_html':comtent,
}
#Sender
sender = ('123456798@qq.com','dfddfdfdfggrgrgc')
#addressee
reciver = '123456789@163.com'
#In the form of mass sending, the list here does not support changing to tuples
#reciver = ['123465789@163.com','12345678@qq.com']
# Login, index value, account and password
sender = zmail.server(sender[0],sender[1])#(* sender) is also called unpacking, which is automatically filled
# Send mail, which mail to launch, and what to send
sender.send_mail(reciver,msg)

Here, Qing'an gives two methods. The first one is lw, and the second one is better. html is not very suitable for sending great pictures, but there is another one you absolutely want to know, that is, sending in the form of attachments, which is simple and fast. After sending, you can clean up the disk, ha ha!!!

Send in attachment format:

Direct code:

#Send attachments. Multiple attachments are in the form of a list
msg = {
    'subject':'Mail subject:Hello',
    'content_text':'enclosure',
    'attachments':'1.png'
}
#'attachments':['1.png','test.make.txt']
#Sender
sender = ('123456798@qq.com','dfddfdfdfggrgrgc')
#addressee
reciver = '123456789@163.com'
#In the form of mass sending, the list here does not support changing to tuples
#reciver = ['123465789@163.com','12345678@qq.com']
# Login, index value, account and password
sender = zmail.server(sende[0],sende[1])#(* sender) is also called unpacking, which is automatically filled
# Send mail, which mail to launch, and what to send
sender.send_mail(reciver,msg)

 

encapsulation

We don't want to rewrite the code every time, so let's encapsulate it and directly import the class call when necessary.

class Email_file():
    # Subject, body, attachment, and initialize the corresponding attributes. If you send multiple attachments, you must * attachments
    def __init__(self,subject,content_text,attachments):
        self.subject = subject
        self.content_text = content_text
        self.attachments = attachments
        #Transfer multiple attachments into list form
        #self.attachments = list(attachments)
        self.msg= {
            'subject':self.subject,
            'content_text':self.content_text,
            'attachments':self.attachments,
        }
    def send_email(self,*sender_more,**romve_more):
        #Single sender, multiple senders
        #self.sender = '1104282189@qq.com'
        self.sender = sender_more
        #addressee
        # self.remove = '1104282189@qq.com'
        #Multiple Recipients 
        self.list_remove = []
        #Store dictionary values in the list
        for i in romve_more.values():
            self.list_remove.append(i)

        #Login operation
        self.get_email = zmail.server(*self.sender)
        #send out
        self.get_email.send_mail(self.sender,self.msg)
m = Email_file('Mail subject:Hello','Message body content:Hello!','email_msg.html')
#Value transfer, multiple senders, multiple recipients
m.send_email('123456789@qq.com','qokdghjdgjdkejye',user1='123456789@163.com',user2='123456789@qq.com')

Finally, the email sending needs to be changed according to the actual situation of its own framework. Therefore, here is just an introduction to tell you that you can do so. Need to be flexible and good at Baidu!

Topics: Python