我试图用Sendinblue.com的SMTP API发送电子邮件活动邮件,这是一个基于云的营销沟通软件套件。我正在运行Sendinblue提供的模板脚本的配置,如屏幕截图所示:
我的最初脚本如下:
# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-.................................' # full api key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "------@gmail.com"}, # personal email with which I was registered @ sendinblue
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
api_response = api_instance.create_email_campaign(email_campaigns)
pprint(api_response)
except ApiException as e:
print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
在我有疑问的情况下,我提到了Sendinblue文档。这个过程的大部分过程似乎都是不言自明的,只是configuration.api_key['api-key'] = 'YOUR API KEY'
的行文不明确,因为它没有很清楚地说明如何传递API -键,以及api_key的属性--假设api_key持有SMTP & API高级选项卡。中定义的api的名称--即使我假设它应该包含其他一些值,也不清楚它们应该是什么,我可以肯定的是,sib_api_v3_sdk.fig.api_key‘api-key’也会导致AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'
。
在最初获得错误AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'
之后,我研究了十几篇关于StackOverflow的文章,其中一篇是关于Kubernetes主题的,脚本似乎抛出了类似于我所得到的错误,所以我遵循了https://stackoverflow.com/questions/51617162/python-kubernetes-module-has-no-attribute-api-key中描述的建议。因此,我尝试重新分配类属性如下:
configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
现在我没有收到丢失的钥匙的错误,而是面对一条“未经授权”的消息,这一事实让我兴奋了一小段时间,直到我花了几个小时试图克服这个问题。现在抛出HTTP response body: {"message":"Key not found","code":"unauthorized"}
的脚本是这样的:
# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-c..............' # key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "mail....@gmail.com"}, # personal gmail with which I was initially registered @ sendinblue.com
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
api_response = api_instance.create_email_campaign(email_campaigns)
pprint(api_response)
except ApiException as e:
print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
我的第一个问题是:我的第一个剧本错了吗?第二:假设‘API’是我的API键的名称(如屏幕截图所示),configuration.api_key['api-sb'] = myapikey
是否是正确的语法?第三:在myapikey
变量中,当分配API键本身时,还应该有什么作为键的前缀吗?
发布于 2022-03-16 12:28:26
尝试此示例事务性电子邮件脚本。在蟒蛇帮我工作。
https://developers.sendinblue.com/reference/sendtransacemail
https://stackoverflow.com/questions/71139360
复制相似问题