我在使用SparkPost发送电子邮件时遇到问题:
我的代码如下:
from configurations.global_configs import site_base_url
from media.settings import SPARKPOST_API_KEY
from sparkpost import SparkPost
import traceback
def welcome_email(user_profile):
try:
user_profile.confirmation_key = user_profile.generate_key()
user_profile.save()
email_address = user_profile.user.email
print email_address
name = user_profile.user.first_name or email_address
email_confirmation_url = site_base_url + "api/account/signup/?ck=" + user_profile.confirmation_key
print email_confirmation_url
sp = SparkPost(SPARKPOST_API_KEY)
result = sp.transmissions.send(
recipients=[email_address],
template='welcome',
subject='this is my subject',
track_opens=True,
track_clicks=True,
substitution_data={
'email_validation_url': email_confirmation_url
},
transactional= True
)
return result
except:
traceback.print_exc()
但是我的代码输出如下,并返回此错误:
[email protected]
http://example.com/api/account/signup/?ck=1144f138439dc42e
Traceback (most recent call last):
File "./users_system/services/email_confirm.py", line 28, in welcome_email
transactional= True
File "/usr/local/lib/python2.7/dist-packages/sparkpost/transmissions.py", line 142, in send
results = self.request('POST', self.uri, data=json.dumps(payload))
File "/usr/local/lib/python2.7/dist-packages/sparkpost/base.py", line 26, in request
raise SparkPostAPIException(response)
SparkPostAPIException: Call to https://api.sparkpost.com/api/v1/transmissions returned 400, errors:
At least one valid recipient is required:
如您所见,我有一个收件人,我知道这是有效的,因为我是通过sparkpost仪表板发送测试邮件的。但是为什么我收到此错误“至少需要一个有效的收件人”?我的问题在哪里
最佳答案
我找到了答案,首先,我要感谢Sparkpost支持团队,该问题出在spark模块的电子邮件解析器中,并且该问题是为解决它而在我的代码中创建的,我进行了更改以解决此问题:
收件人= [dict(address = dict(email = email_address))],