How to use the Mailjet class library in Python for email sending
How to use the Mailjet class library in Python for email sending
Mailjet is a powerful cloud service for sending emails.It provides a simple and easy -to -use library that makes it easier to send emails in Python.This article will introduce how to send emails with Mailjet class libraries in Python and provide corresponding configuration and code examples.
Step 1: Install the Mailjet Library
First, we need to install the Mailjet class library.Open the terminal or command prompt, execute the following commands to install the Mailjet class library:
pip install mailjet
Step 2: Import Mailjet class library
At the beginning of the Python script, use the following code line to import the Mailjet class library:
python
from mailjet import Client
Step 3: Set the Mailjet API key and key password
In order to use the Mailjet service, we need to provide API key and key passwords.Log in to the Mailjet account, transfer to the "API key management" page, and then create or obtain the existing API key.Replace the API key and key passwords in the following code line:
python
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
Step 4: Create the Mailjet client
Next, we need to use the API key and key passwords to create the Mailjet client.Use the following code line to create the Mailjet client:
python
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
Step 5: Define mail content
Before sending emails, we need to define the contents of the mail.The following is the code of a sample mail content:
python
email = {
'From': {
'Email': 'sender@example.com',
'Name': 'Sender Name'
},
'To': [
{
'Email': 'recipient@example.com',
'Name': 'Recipient Name'
}
],
'Subject': 'Test Email',
'TextPart': 'This is a test email using Mailjet.',
'HTMLPart': '<h3>This is a test email using Mailjet.</h3>'
}
In the above code, we designate the email address and name of the sender, the email address and name of the recipient, and the theme of the email, the pure text content and the HTML content.
Step 6: Send an email
Once we define the contents of the mail, we can use the Mailjet client to send emails.Use the following code line to send emails:
python
result = mailjet.send.create(data=email)
Step 7: Check the status of email sending
After sending an email, you can check the status of the email.Use the following code lines to get the sending results:
python
print(result.status_code)
print(result.json())
In the above code, the `result.status_code` will return the http status code sent by the email,` result.json () `will return a JSON object containing the result of email sending.
Complete code example:
python
from mailjet import Client
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
email = {
'From': {
'Email': 'sender@example.com',
'Name': 'Sender Name'
},
'To': [
{
'Email': 'recipient@example.com',
'Name': 'Recipient Name'
}
],
'Subject': 'Test Email',
'TextPart': 'This is a test email using Mailjet.',
'HTMLPart': '<h3>This is a test email using Mailjet.</h3>'
}
result = mailjet.send.create(data=email)
print(result.status_code)
print(result.json())
The above is how to use the Mailjet class library in Python for the complete steps and code examples of email sending.Make sure to replace the API key and key passwords, and change the mail content as needed to successfully use MailJet to send emails.