0

A user has recently notified me that he cannot reply to my email messages because of white space in the address. He also mentioned the raw FROM field not being RFC 822 compliant - I don't know much about it and can't verify.

Here's the raw From field that he received:

From: SiteName someprefix@mg.somesite.io

This is the way I'm currently sending these emails:

msg_plain = render_to_string('email_template.txt', context)
                msg_html = render_to_string('email_template.html', context)

                EMAIL_FROM_FIELD = 'SiteName someprefix@mg.somesite.io'

                mail_was_sent = send_mail(
                    email_subject,
                    msg_plain,
                    EMAIL_FROM_FIELD,
                    [profile.user.email],
                    html_message=msg_html,
                )

What am I doing wrong?

zerohedge
  • 3,185
  • 4
  • 28
  • 63

2 Answers2

3

Unless I'm missing something, I think you need to change this:

EMAIL_FROM_FIELD = 'SiteName someprefix@mg.somesite.io'

To this:

EMAIL_FROM_FIELD = 'SiteName <someprefix@mg.somesite.io>'

The general rule is that wherever there may be linear-white-space (NOT simply LWSP-chars), a CRLF immediately followed by AT LEAST one LWSP-char may instead be inserted.

This is from: https://www.w3.org/Protocols/rfc822/

  • Thanks Micheal. The reason I have the SiteName in there is that it changes the "name" of the sender in the emails-list of the receiver. It looks better to see "SiteName" in your emails list than some long email address. – zerohedge Aug 28 '18 at 21:40
  • Hi, yes - I skipped over the RFC 822 compliance bit, I've updated my answer using the above reference (https://www.w3.org/Protocols/rfc822/). –  Aug 28 '18 at 21:42
1

You may define the form field as follow:

EMAIL_FROM_FIELD = 'SiteName <someprefix@mg.somesite.io>'
Antwane
  • 20,760
  • 7
  • 51
  • 84