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

Can't set reply-to neither by header nor explicitly #50

Open
gomezjdaniel opened this issue Jan 26, 2017 · 3 comments
Open

Can't set reply-to neither by header nor explicitly #50

gomezjdaniel opened this issue Jan 26, 2017 · 3 comments

Comments

@gomezjdaniel
Copy link

gomezjdaniel commented Jan 26, 2017

By running README example:

mail = EmailMultiAlternatives(
  subject="Your Subject",
  body="This is a simple text email body.",
  from_email="noreply@mydomain.com",
  to=["my@email.com"],
  headers={"Reply-To": "support@sendgrid.com"}
)
mail.send()

I get a 400 HTTP error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/django/core/mail/message.py", line 342, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python3.5/site-packages/sgbackend/mail.py", line 68, in send_messages
    self.sg.client.mail.send.post(request_body=mail)
  File "/usr/local/lib/python3.5/site-packages/python_http_client/client.py", line 204, in http_request
    return Response(self._make_request(opener, request))
  File "/usr/local/lib/python3.5/site-packages/python_http_client/client.py", line 138, in _make_request
    return opener.open(request)
  File "/usr/local/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/local/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/local/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

The thing is sendgrid-django has a set_reply_to method in the class Mail (https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py#L154) while in sendgrid-django _build_sg_mail method (https://github.com/elbuo8/sendgrid-django/blob/master/sgbackend/mail.py#L69) reply_to is not added by the proper method, instead it expects to be added from setting a custom header like the example does:

for k, v in email.extra_headers.items():
    mail.add_header({k: v})

Next problem is django EmailMessage class only accepts reply_to param a list/tuple (https://github.com/django/django/blob/1c466994d9729a8902d34e277a3a685bef2a08e1/django/core/mail/message.py#L232) while sengrid-python expects only as I see one single email as reply-to (https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py#L85 and https://github.com/sendgrid/sendgrid-python/blob/ca96c8dcd66224e13b38ab8fd2d2b429dd07dd02/examples/mail/mail.py#L132)

@kevin-brown
Copy link

This looks like it's probably the same bug as #34.

@gomezjdaniel
Copy link
Author

gomezjdaniel commented Jan 27, 2017

It is. I already made a workaround fix -which miss the possibility that reply-to email includes a name- in a forked repository of this one that includes inside _build_sg_mail

        if email.reply_to:
            if isinstance(email.reply_to, str):
                mail.set_reply_to(Email(email.reply_to))
            else:
                mail.set_reply_to(Email(email.reply_to[0]))

Thing is Django asks as reply_to for a tuple/list while sendgrid only accepts one email as reply_to. Do you know if the email specification allows multiple emails in Reply-to header?

@abendig
Copy link

abendig commented Feb 3, 2017

Our fix currently looks like this:

        for k, v in email.extra_headers.items():
            if k.lower() == "reply-to":
                mail.set_reply_to(Email(v))
            else:
                mail.add_header({k: v})
        if not mail.reply_to and hasattr(email, 'reply_to') and email.reply_to:
            mail.set_reply_to(Email(email.reply_to[0]))

Based on the discussion in #34 I can't tell how close an actual fix here is. Happy to help/contribute a PR for this though.

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