Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues When attaching file with email in sendgrid v3 APIs #60

Closed
veris-neerajdhiman opened this issue Aug 5, 2017 · 7 comments
Closed

Comments

@veris-neerajdhiman
Copy link

  • I was using this package for sending email with sendgrid. Everything was working fine but some days ago one of my email where I attach an excel sheet started raising errors.

  • On debugging I find out that it was because of new Behaviour of sendgrid V3 APIs.

  • The issue I was getting is bad Request error. Complete error is as follows :

{'errors': [{'message': 'The content value must be a string at least one character in length.', 'help': 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.value', 'field': 'content.0.value'}]}

  • Seems kile sendgrid don't accept content=None anymore.

  • In sendgrid package file named mail.py class Mail() they accept content kwargs but this package doesn't seems to send content when using Mail() in sgbackend/mail.py _build_sg_mail().

  • You can also find out the same issue raised here

@jayhale
Copy link

jayhale commented Sep 26, 2017

Can you post code to replicate the issue? At least I can verify that this will be resolved by either #69 or #55 which are upgrades to the sendgrid dependency.

@inforian
Copy link

inforian commented Oct 6, 2017

@jayhale Below is the code which I am using to send an email :

from django.core.mail import EmailMultiAlternatives

class EmailNotification(BaseNotification):
    """
    Notification class for Email notification
    """

    def __init__(self, destination, message, case, content_object, subject, message_html, attachment_name=None, attachment_type=None, attachment=None):
        super().__init__(destination, message, case, content_object)
        self.attachment = attachment
        self.attachment_name = attachment_name
        self.attachment_type = attachment_type
        self.subject = subject
        self.message_html = message_html

        self.email_message = EmailMultiAlternatives(self.subject, self.message, 'DJ<[email protected]>',
                                                    [self.destination])
        if self.attachment:
            self.email_message.attach_file(self.attachment)

        if self.message_html:
            self.email_message.attach_alternative(x
                self.message_html, 'text/html')

    def send(self):
        """Send email to destination
        """
        return self.email_message.send()

@inforian
Copy link

inforian commented Oct 6, 2017

@jayhale Today Again I got the same error (this email do not have any attachment with it).
Below are my package info

sendgrid==3.6.5
sendgrid-django==4.0.4

Error tracback :

{'errors': [{'field': 'content.0.value', 'help': 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.value', 'message': 'The content value must be a string at least one character in length.'}]}

@jayhale
Copy link

jayhale commented Oct 6, 2017

Yes I see - I'm thinking the fix is for the package to raise an error when the content is None or '' before sending.

EDIT: The issue isn't with the package, it's handling the zero-length content as expected and the API is rejecting it.

@inforian
Copy link

inforian commented Oct 6, 2017

@jayhale But Howcome earlier the same code was working and even if package raise an error then what content one must send and where (in which variable)?

@jayhale
Copy link

jayhale commented Oct 6, 2017

notification = EmailNotification(
    message='Mail contents', # Any non-zero-length string
    **the_other_args)

Your notification object is being instantiated with a zero-length-string or None in message. That is then being passed to SendGridBackend() which instantiates a Mail() object from the sendgrid-python package which sends it on to the API which replies with an error telling you that the content of the message must be longer than 0 in length.

I don't know if anything changed in the API. This package has used the v3 API for the past year:
9cb2668

Check that the length of your message is greater than zero - I think the issue is there.

@inforian
Copy link

inforian commented Oct 9, 2017

Yes, issue id fixed.
Actually I was using two params , message_txt and message_html. So in some emails message_txt length was 0 and this is where sendgrid was raising errors and I was thinking I am sending the message content with email (message_html).

Anyway Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants