In many cases, sending mail in Python may not be possible using the API provided by the mail server, because not all mail providers provide apis for customers to use.
Mail delivery services that use the mail API usually require an additional charge.
Therefore, when we send the mail test, we may need the SMTP mail sending service, which is usually provided by all mail service providers.
To use the SMTP mail sending service, you need the following information to complete and test:
- IP address, port number, user name, and password of the SMTP mail server
- Address for sending and receiving mail
- The subject and body of the message
If it seems a bit complicated, Python actually provides a method for sendmail, which is in the smtplib library.
Simply follow the steps below to do it.
Building a message Object
There is a library in Python called MIMEMultipart that we can use to build message objects.
The thing to do is relatively simple. After defining the MIMEMultipart object, you need to set in the object who sent it, to whom, and what the subject is.
And then you insert text into that object, you can insert HTML or you can insert plain text.
For the sake of aesthetics, we usually insert HTML text, and many times we may use templates to insert more data.
Send E-mail
After defining the message object, we can send the mail.
Before sending mail, we need to initialize the SMTP object, usually using the smtplib.smtp (smtpServer) method.
In this method, we define the address and port of the sending mail server.
We might then call a starTTls method that expects the sending mail server to use TLS mode.
With the development of technology and security considerations, many mail sending servers require TLS mode for sending.
Then use the server.login(email_user, email_passwd) method to login to the server.
After the above steps are complete, your Python code will communicate with the mail server, your code will get the server object, and the object will be initialized.
Once this is done, you can call the sendmail function to send the mail.
When the email is sent, do not forget to close the connection to the mail server.
# Connect to the SMTP server
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(email_user, email_passwd)
sending_response = server.sendmail(from_addr, to_addr, message.as_string())
print(sending_response)
server.quit()
Copy the code
For the above send service, please refer to the pseudocode provided above.
Complete test code refer to making the above: python – tutorials/SendMailSMTP. Py at master · cwiki – us – docs/python – tutorials, dead simple
The main points of
The Python mail service is relatively simple.
Because the mail sending service needs to communicate with the third-party mail server, the most important step in this communication process is to obtain the link of the mail server first.
Usually this connection will fail because of firewalls, sending user name restrictions, password requirements, etc., and there will be no way to get the object connecting to the server.
It is ok to analyze specific problems in the light of the situation encountered.
The principle steps to follow are:
- Build the message
- Get a server connection
- Send E-mail
- Closing server Connections
Be careful not to forget to close the server connection.
www.ossez.com/t/python-sm…