Skip to content

Allow multiple emails in the from_email argument #60

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

Closed
avelis opened this issue Apr 17, 2017 · 9 comments
Closed

Allow multiple emails in the from_email argument #60

avelis opened this issue Apr 17, 2017 · 9 comments

Comments

@avelis
Copy link

avelis commented Apr 17, 2017

According to RFC-5322 Section 3.6.2, the From can be a mailbox list and the Sender can be a mailbox. I tried to set the from_email as a list of emails but caught an AnymailInvalidAddress when calling the send method.

>>> m = EmailMessage(to=['[email protected]'], from_email=['[email protected]', '[email protected]'], body='test', subject='test')
>>> m.send()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/.../lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/.../lib/python2.7/site-packages/anymail/backends/base.py", line 88, in send_messages
    sent = self._send(message)
  File "/.../lib/python2.7/site-packages/anymail/backends/base_requests.py", line 56, in _send
    return super(AnymailRequestsBackend, self)._send(message)
  File "/.../lib/python2.7/site-packages/anymail/backends/base.py", line 117, in _send
    payload = self.build_message_payload(message, self.send_defaults)
  File "/Users/andrewvelis/Documents/promoterapp/apps/emailrelay/backends.py", line 8, in build_message_payload
    return SendGridSMTPAPIMethodsPayload(message, defaults, self)
  File "/.../lib/python2.7/site-packages/anymail/backends/sendgrid_v2.py", line 93, in __init__
    *args, **kwargs)
  File "/.../lib/python2.7/site-packages/anymail/backends/base_requests.py", line 116, in __init__
    super(RequestsPayload, self).__init__(message, defaults, backend)
  File "/.../lib/python2.7/site-packages/anymail/backends/base.py", line 265, in __init__
    value = converter(value)
  File "/.../lib/python2.7/site-packages/anymail/backends/base.py", line 304, in parsed_email
    return ParsedEmail(address, self.message.encoding)  # (handles lazy address)
  File "/.../lib/python2.7/site-packages/anymail/utils.py", line 144, in __init__
    % (address, str(err)))
AnymailInvalidAddress: Invalid email address format ['[email protected]', '[email protected]']: Multiple email addresses (parses as [('', ''), ('', u"'[email protected]'"), ('', u"'[email protected]'"), ('', '')])

It feels like a bug with django-anymail but I am open for discussion/education on how to achieve this.

@avelis avelis changed the title Allow multiple emails in the address from_email Allow multiple emails in the from_email argument Apr 17, 2017
@medmunds
Copy link
Contributor

Oh, wow, I didn't know the RFC allowed multiple addresses in the From field. Learned something new today.

Anymail extends Django's EmailMessage class, which is specced to take a single string as its from_email. So that's not something Anymail could change unilaterally. You could submit a proposed change to Django, and if accepted Anymail would follow the change.

But FWIW, most (maybe all?) of the ESPs Anymail supports only allow a single from address in their APIs. So even if Django's EmailMessage allowed multiple from emails, they'd just turn into AnymailUnsupportedFeature errors.

Can I ask how you're trying to use multiple from emails? There might be some other way to achieve a similar outcome. (E.g., the EmailMessage reply_to allows a list of emails, and this works with all Anymail ESPs except SendGrid.)

@avelis
Copy link
Author

avelis commented Apr 17, 2017

@medmunds The scenario I am running into is with a pseudo mail forwarding feature I have developed.

That features generates a from_email as follows: From:'"[email protected]" <[email protected]> . The reasons for this scenario is rather long and specific. However, reading the RFC spec led me to explore a mailbox-list from_email option.

Exploring Django's EmailMessage source, I see that the message object from can be assigned using the extra_headers dict. I can see if I can explore an option around that.

If the issue is with Django I can consider opening a ticket there. Although Django's core message functionality seems to allow for it. I could be wrong.

Thanks again for making the project regardless. This is a wonderful library to use and I am grateful it exists.

@medmunds
Copy link
Contributor

Hmm, couple of things:

  • from_email='"[email protected]" <[email protected]>' should work just fine. That is, from_email is a single string, with a single email address (mailbox) whose display-name is "[email protected]" and whose addr-spec is "[email protected]". (And that's a good approach if you're trying to change how the "From" is displayed in the recipient's email client.)

  • Looking more closely, I see that Anymail is imposing an additional requirement on from_email that Django core email doesn't: Django would allow from_email='[email protected], [email protected]'—still a single string (not a Python list), but with multiple, comma-separated emails in it. And it would pass that string, more-or-less intact, on to the underlying email backend as the From header.

I can look at making that work in Anymail. But the problem is still going to be that most ESP send APIs don't allow multiple from addresses. And they tend to be pretty picky about allowing header overrides of standard fields like From. So it'll probably just be a different error getting raised.

Which ESP(s) are you using?

@avelis
Copy link
Author

avelis commented Apr 17, 2017

@medmunds We are currently using SendGrid. I just got off chat support with them. They appear to only support one email address-spec in the From field. Ugh. Not sure if other ESP's provide it.

If you feel like closing the ticket that's fine. I will initiate a feature request with SendGrid to see if they can allow for that. Likely wouldn't be addressed anytime soon though.

@medmunds
Copy link
Contributor

Yeah, not entirely surprising. (SendGrid's v3 API actively prevents multiple reply-to addresses, so it doesn't seem likely they'd handle multiple from emails.)

You got me curious, though, and I ran some quick tests to see which ESPs would even support the from_email='[email protected], [email protected]' form:

  • Mailgun allows multiple from fields
  • Mandrill's API doesn't have any way to express more than one from
  • Postmark accepts multiple addresses in its From field, but silently truncates to just the first one before sending
  • SendGrid's v3 API doesn't have any way to express more than one from
  • SparkPost allows a comma-separated from string

But: even if you switched to an ESP that supports them, you'd probably run into deliverability issues with multiple from addresses. Gmail flat-out rejects the message ("5.7.1 Messages with multiple addresses in From: header are not accepted. - gsmtp"). Outlook.com accepts the message, but seems to consider multiple from addresses a spam signal (my tests ended up in the junk folder).

For completeness, I think I'm going to change Anymail to support a multi-address from_email string for Mailgun and SparkPost, and to issue a clearer "unsupported" error for the other ESPs. But in practice, multiple from addresses probably aren't all that usable.

For your project, one approach I've seen with forwarding is encoding one email address in another. E.g.: From: support+customer%[email protected]. (Assuming company.com's mail supports +-aliases, that delivers to [email protected], but you can still extract the "other from" from the address.)

@avelis
Copy link
Author

avelis commented Apr 17, 2017

@medmunds Thanks again for the help with creative solutions on my specific scenario. And for the responsiveness to the issue I created on the repo.

@medmunds
Copy link
Contributor

@avelis no problem - thanks

@ajeetgupt1982
Copy link

Exception Value: | Invalid address; only [email protected] could be parsed from [email protected],[email protected]"

I am getting the above error while using in to or cc .

@medmunds
Copy link
Contributor

@ajeetgupt1982 I appreciate your searching for related issues before opening a new one, but it sounds like you are seeing something different. This issue is specifically about multiple from_email addresses, not to or cc.

If you're having trouble with multiple recipients, please open a new issue (or ask a question on Stack Overflow), and be sure to include all of the information requested. In particular, which ESP, what version of Anymail, your code that tries to send the email, and the exact and complete error message. (The exception value you posted above doesn't appear in Anymail's code. There is a similar error, though, and if that's the one you're seeing, then you've added text and changed some important details as you pasted it here.)

You may also want to take a look at the help page in Anymail's docs, if you haven't already.

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