在 mailjet 的 python 请求中添加变量
Add variables in python request for mailjet
我刚刚开始使用 Python,似乎无法使用 mailjet 将变量添加到我的 request
负载中。
这是示例 curl 脚本:
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3/send \
-H 'Content-Type: application/json' \
-d '{
"FromEmail":"pilot@mailjet.com",
"FromName":"Mailjet Pilot",
"Subject":"Your email flight plan!",
"Text-part":"Dear passenger, welcome to Mailjet! May the delivery force be with you!",
"Html-part":"<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!",
"Recipients":[
{
"Email": "passenger@mailjet.com"
}
]
}'
我已将其转换为 Python request
,如下所示:
import requests
emailFrom = "<my_email>"
fromName = "<my_name>"
message = "test"
emailTo = "<second_email>"
headers = {
'Content-Type': 'application/json',
}
payload = {'FromEmail':'<my_email>',
'FromName':'<my_name>', 'Subject':'Plan', 'Text-part':'Dearz', 'Html-part':'<h1>Done</h1>', 'Recipients':[{'Email':'<second_email>'}]}
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, data=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
我没有使用这些固定值,而是尝试使用 python 脚本开头的变量,但是当我使用这些变量时我无法让脚本发送电子邮件。
如何将变量添加到 Python 脚本中?
干杯:)
问题出在方法上。您正在尝试发送数据。
所以改为:
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, data=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
尝试将 data
替换为 json
:
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, json=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
希望这对您有所帮助
我刚刚开始使用 Python,似乎无法使用 mailjet 将变量添加到我的 request
负载中。
这是示例 curl 脚本:
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3/send \
-H 'Content-Type: application/json' \
-d '{
"FromEmail":"pilot@mailjet.com",
"FromName":"Mailjet Pilot",
"Subject":"Your email flight plan!",
"Text-part":"Dear passenger, welcome to Mailjet! May the delivery force be with you!",
"Html-part":"<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!",
"Recipients":[
{
"Email": "passenger@mailjet.com"
}
]
}'
我已将其转换为 Python request
,如下所示:
import requests
emailFrom = "<my_email>"
fromName = "<my_name>"
message = "test"
emailTo = "<second_email>"
headers = {
'Content-Type': 'application/json',
}
payload = {'FromEmail':'<my_email>',
'FromName':'<my_name>', 'Subject':'Plan', 'Text-part':'Dearz', 'Html-part':'<h1>Done</h1>', 'Recipients':[{'Email':'<second_email>'}]}
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, data=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
我没有使用这些固定值,而是尝试使用 python 脚本开头的变量,但是当我使用这些变量时我无法让脚本发送电子邮件。
如何将变量添加到 Python 脚本中?
干杯:)
问题出在方法上。您正在尝试发送数据。
所以改为:
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, data=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
尝试将 data
替换为 json
:
response = requests.post('https://api.mailjet.com/v3/send', headers=headers, json=payload, auth=('<PUBLIC_KEY>', '<PRIVATE_KEY>'))
希望这对您有所帮助