SparkPost:至少需要一个有效的收件人python / django API

时间:2016-03-03 11:49:43

标签: python django email sparkpost

使用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()

但我的代码输出如下并返回此错误:

exampleemail@gmail.com
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仪表板发送了测试邮件。但是为什么我会收到这个错误"至少需要一个有效的收件人" ?? !!!我的问题在哪里

2 个答案:

答案 0 :(得分:3)

我找到了答案,首先,我要感谢Sparkpost支持团队,问题出在Spark模块中的电子邮件解析器中,问题就是在我的代码中解决了这个问题我做了这个修改来修复它:
          recipients = [dict(address = dict(email = email_address))],

答案 1 :(得分:0)

由于您将其用作电子邮件的django后端,为什么不直接使用

进行此操作
from django.core.mail import send_mail

send_mail(
 subject='hello from sparkpost',
 message='Hello Rock stars!'
 from_email='from@yourdomain.com',
 recipient_list=['to@friendsdomain.com'],
 html_message='<p>Hello Rock stars!</p>',
)

official docs

中所述