In many cases, there may not be a way to use the APIs provided by your mail server to send mail using Python, because not all mail service providers provide APIs for their clients to use.
Often mail delivery services using the Mail API require additional fees.
Therefore, when we test sending emails again, what we may need is SMTP mail delivery service, which is usually provided by all mail service providers.
To use the SMTP mail delivery service, you need to have the following information before you can complete and test it:
- SMTP mail server address, port, login user name and login user password
- Addresses for sending and receiving mail
- Subject and body of the e-mail
Doesn't this seem a bit complicated? Actually, Python provides a method for sendmail, which is in the smtplib library.
Simply follow the steps below and you'll be fine.
Building Message Objects
There is a library in Python for MIMEMultipart that we can use to build message objects.
What to do is also relatively simple, after defining the MIMEMultipart object, you need to set in this object who sends it, to whom it is sent and what the subject is.
Then insert the body in this object, you can insert HTML or plain text.
In order to make the email more beautiful, usually we will insert the HTML text, and very often in order to insert more data, we may also use the template.
mail
After we finish defining the message object, we are ready to send the email.
Before sending a message, we need to initialize the SMTP object, usually using the (smtpserver) method.
In this method, we will define the address and port of the sending mail server.
Subsequently we may call a starttls method that is expecting the sending mail server to be using TLS mode.
With the advancement of technology, and security concerns, many mail delivery servers require the use of TLS mode for delivery.
Then use the (email_user, email_passwd) method to log in to the server.
After the above steps are complete, we're done communicating between your Python code and the mail server, and your code will have gotten the server's object and initialized it at this point.
After this step you can call the sendmail function to send the e-mail.
Don't forget to close the connection to the mail server when the email is finished.
# Connect to the SMTP server server = (smtpserver) () (email_user, email_passwd) sending_response = (from_addr, to_addr, message.as_string()) print(sending_response) ()
For the sending service above, please refer to the pseudo-code provided above.
The full test code is available on GitHub:python-tutorials/ at master · cwiki-us-docs/python-tutorials · GitHub
pivot
Python's service for sending mail is still relatively simple.
Because mail delivery services need to communicate with third-party mail servers, the most important step in this communication process is to first obtain a link to the mail server.
Often this link may fail to get a connection because of firewalls, restrictions on sending usernames, password requirements, etc. and there is no way to get the object that connects to the server.
Just analyze the specific problem for the situation encountered.
The principle step to follow is:
- Constructing messages
- Getting a server connection
- Close server connection
One thing to keep in mind is not to forget to close the server connection.
Above is the details of Python based on SMTP send mail method, more information about Python SMTP send mail please pay attention to my other related articles!