New modules required for the mail automation chapter:
smtplib Mail Protocol and Delivery Module
email Content Definition Module
schedule Timing Module
The smtplib and email modules can help us to send emails normally; the schedule module can help us to send emails at regular intervals, such as payroll emails and other emails that need to be sent at a fixed time.
Sending regular mail with python
Recognizing the Send Mail Process
Here's a brief description of the process of sending an email (to be honest, it's a step I kind of don't want to write about)
Step 1: Log in to your mailbox
Step 2: Write the email address of the email recipient
Step 3: Write the title and content of the email (sometimes you need to add attachments)
Step 4: Send an email
Understanding Mail Protocols
- smtp: protocol for mail delivery
- pop3: protocol for mail reception
What's the deal?
A protocol is a rule, which has been encapsulated by the underlying network. There is no need to care what its specific rules are, just use the upper layer tools directly.
For example, in the email scenario, when we send an email using the smtp protocol, the network knows that it is an outgoing email when it finds out that it is the pop3 protocol. When the network realizes that it is smtp, it knows that it is an outgoing message; the same is true for pop3, when the network realizes that it is pop3, it knows that it is an incoming message.
smtplib module
The smtplib module is one of the most important modules in python for sending mail.
Creates a protocol object:
smptObj = () Generate smpt protocol object by ()
Create link:
(smpt server address, 25) via connect(server IP address, port number), the default port number of smpt service is 25
Login Authentication:
(mail_user, mail_password) pass login(mail_user, mail_password) into mailbox name and login password to finish the verification. (Note: some mailbox servers pass in password instead of login password [also called authorization code], check it in mailbox settings)
Send an email:
(sender, receivers, message) Pass sendmail(sender, receivers, message) into the sender's email address, the receiver's email address, and the content of the message to complete the action of sending an email.
PS: Here you can see that "recipients' email addresses are 'receivers' plural", so "recipients' email addresses" is an array, a list, which can be sent to more than one person; the "message" of the message content is the encrypted string of the message object.
email package
The email package has two functions that are more commonly used:
function name | parameters | clarification |
---|---|---|
MIMEText | Mail content, mail type, coding format | Objects that define the content of the email sent |
Header | Various types of information, coding formats | Define various types of information as objects, such as headings, etc. |
PS: The ""message" is the encrypted string of the message object" mentioned above is generated by encrypting "MIMEText".
Sending mail mini-case
The code example is as follows:
# coding:utf-8 import smtplib from import MIMEText from import Header ''' Third-party smpt [the email addresses involved in the script are fake, please test with your own real email address] ''' mail_host = "" # Setting up mailbox servers mail_user = "conan868242" # Set up your own mailbox mail_pass = "cb997b01a87232b2" # Here is the passphrase for the mailbox, the authorization code; not the password. sender = "conan868242@" # Define the sender mailbox (which is actually yourself) receivers = ["3241716373@"] # Define recipient mailboxes (can be multiple recipients) message = MIMEText("This is a test email O(∩_∩)O(onom.) laughing out loud~", "plain", "utf-8") # Define the content of the message; "plain" is the format, which means it's a normal file. message["From"] = Header(sender) # Define send message within message --> sender message["Subject"] = Header("Python Test Emailing", "utf-8") # Define the title of the message to be sent within message print("Encrypted sent content \n", message.as_string()) # Printout of encrypted transmissions try: # Catch exceptions for sending mail smtpObj = () # Instantiate the SMTP protocol object (mail_host, 25) # Linking SMTP servers (mail_user, mail_pass) # Login Email Verification (sender, receivers, message.as_string()) # Send email; "message" encrypts the sent content string with "as_string()" except as error: print("error:{}".format(error))
PS: Don't try to run it with this source code anymore, I've changed the license code.
The results of the run are as follows:
Pitfall avoidance summary for sending emails
Enable smtp and pop3 access to the sender's mailbox
Different mailboxes require different types of authentication (some are login passwords, some are authorization codes)
Some mailboxes open authorized access permission and authorization code may charge, here need to pay attention to it (such as QQ mailbox [give a bad comment])
to this article on the Python mail sending function of the method detailed article is introduced to this, more related Python mail sending content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!