SoFunction
Updated on 2024-11-10

Python Calling the API to Send Mail

The email function is basically used when operating or debugging various SDKs or APIs.

When testing, many people may use SMTP or their own email to send using SMTP, which is usually fine.

But you need to know that a website or a platform needs to send a large number of emails in a short period of time, and at the same time the emails may be blocked by firewalls and so on, resulting in non-delivery.

Therefore, the SMTP that comes with common mailbox is usually no way to meet the demand, so we may need some professional mailbox service, specific use of which service, we will not explain here to avoid advertising suspicion.

Generally speaking, professional mailbox servers will provide API and SMTP 2 ways, here we try to use Python to call API to send.

Python Calling the API

Python calls the API very simply, usually with a class called requests.

First you need to set up the KEYs used by the sending APIs on the major email sending platforms.

The mail server we are currently using, provides a Sandbox sending domain, usually to avoid sending a lot of spam, you also need to add the authorized email address to it.

The purpose of this is to only send emails to the specified email address, to avoid problems when debugging the API that may result from accidentally sending a large number of emails.

When the above 2 steps have been completed, the API can be selected.

Then select the Python code in the code above.

Code Analysis.

The code above is very simple Request code.

To execute the above code, you need the following configurations:

  • The API address, which varies depending on the server, will be given to you.
  • Key, different API authorization methods are different, you need to go to your mail service provider to find the KEY assigned to you, usually this KEY is used for authorized use.
  • Data, python request can accept data, this parameter is data, in data usually use JSON data format, the specific configuration you need to refer to the Python function parameter and the configuration provided by the mail service provider.

In the above parameters, we can configure and then test the mail delivery.

If all goes well, the API will return a specific result.

If you wish to do real email testing then you need to use a real domain configuration.

Above is Python call API send mail details, more information about Python send mail please pay attention to my other related articles!