Shubhanshu
Shubhanshu

Reputation: 1015

Invalid SMTPAPI Header: send_at must be a timestamp

I am scheduling the emails through SMTP API. This is what I have tried uptil now:

from smtpapi import SMTPAPIHeader
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Template, Context

def campaign_email(lg_user, template):

    user = lg_user.id
    email = user.email
    fname = lg_user.id.first_name
    lname = lg_user.id.last_name
    mobile = lg_user.contact_no
    purchase = lg_user.active_user_subjects.values_list('subject', flat=True)
    expiry = str(lg_user.active_user_subjects.values_list('at_expiry', flat=True))

    filename = '/tmp/campaign_mailer.html'
    opened_file = open(filename, "r").read()

    temp = Template(opened_file)

    c = Context({'fname': fname, 'lname': lname, 'subject': subject, 'email': email,
                 'mobile': mobile, 'purchase': purchase, 'expiry': expiry})

    header = SMTPAPIHeader()

    html_content = temp.render(c)

    send_at = {"send_at": 1472058300}
    header.set_send_at(send_at)

    msg = EmailMultiAlternatives(subject, html_content, sender, [email],
                                 headers={'X-SMTPAPI': header.json_string()})

    msg.attach_alternative(html_content, "text/html")
    msg.send(fail_silently=True)

In order to check, if my header (which on printing header.json_string() resolves to this:

{
    "send_at": {
        "send_at": 1472051700
    }
}

) was valid or not, I checked on https://sendgrid.com/docs/Utilities/smtpapi_validator.html and it came out to be completely valid.

But the failure mail that I got from sendgrid's support stated the reason of failure to be: send_at must be a timestamp. I believe, that in the documentation, it's clearly stated that the timestamp should be in UNIX format - which is what I have supplied as the value to my send_at key.

So, how do I resolve this error?

Upvotes: 0

Views: 1054

Answers (1)

solarissmoke
solarissmoke

Reputation: 31484

set_send_at() takes an integer argument, but you are passing it a dictionary ({"send_at": 1472058300}). This is invalid and causes the error.

Change it to:

header.set_send_at(1472058300)

Upvotes: 1

Related Questions