【问题标题】:SparkPost: At least one valid recipient is required python/django APISparkPost:至少需要一个有效的收件人 python/django API
【发布时间】:2016-06-16 17:36:29
【问题描述】:

我在使用 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 仪表板发送了测试邮件。但是为什么我收到此错误“至少需要一个有效的收件人”??!!!我的问题在哪里

【问题讨论】:

  • 您确定user_profile.user.email 是有效的电子邮件地址吗?
  • 我同意 cfs 似乎电子邮件地址有问题??那里的其他一切看起来都不错,并且在本地对我有用。顺便说一句,这个 Slack 小组也可以回答问题 slack.sparkpost.com
  • 我确定电子邮件是有效的 我什至使用了其他有效的电子邮件地址,但又出现了这个错误。

标签: python django email sparkpost


【解决方案1】:

我找到了答案,首先我要感谢 Sparkpost 支持团队,问题出在 spark 模块中的电子邮件解析器中,并且该问题是为了在我的代码中解决它而创建的,我进行了此更改以修复它:
收件人=[ dict(address=dict(email=email_address)) ],

【讨论】:

  • 感谢您提交此内容并帮助我们解决问题!对于其他用户 - 请注意,问题在于库没有正确解析带有 unicode 字符的收件人。我们今天早上刚刚向 python-sparkpost 推送了一个更新,解决了这个问题:github.com/SparkPost/python-sparkpost/releases/tag/v1.0.3
【解决方案2】:

既然您将它用作电子邮件的 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中提到的那样

【讨论】:

  • 谢谢你的回答,但我想用这个方法。
猜你喜欢
  • 1970-01-01
  • 2011-10-07
  • 2011-07-21
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多