1

I have a problem sending email with SparkPost: My code is as follow:

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

but my code output is as follows and returns this error:

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:

As you can see I have one recipient and I know that is valid because I sent test mail via sparkpost dashboard. But why I get this error "At least one valid recipient is required" ??!!! where is my problem

golden_boy615
  • 394
  • 3
  • 17
  • Are you sure that `user_profile.user.email` is a valid email address? – cfs Mar 03 '16 at 13:11
  • I agree with cfs it seems there is a problem with the email address?? Everything else looks good there and works for me locally. BTW, this Slack group can aswer questions too http://slack.sparkpost.com – Yepher Mar 03 '16 at 13:44
  • I am sure that email is valid I even used other valid email addresses but again this error. – golden_boy615 Mar 04 '16 at 07:08

2 Answers2

4

I found the answer, First I want to thank Sparkpost support team the issue was in email parser in spark module and the issue is created for solving it in my code I made this change to fix it:
recipients=[ dict(address=dict(email=email_address)) ],

golden_boy615
  • 394
  • 3
  • 17
  • 1
    Thanks for submitting this and helping us troubleshoot! For other users - note that the issue was that the library wasn't properly parsing recipients with unicode chars. We just pushed an update to python-sparkpost this morning that resolves this: https://github.com/SparkPost/python-sparkpost/releases/tag/v1.0.3 – richleland Mar 04 '16 at 15:39
0

Since you are using it as a django backend for your email, why don't you do this directly with the

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>',
)

as mentioned in their official docs