Search code examples
djangoemailgmail

Email sending from address is gmail with django


Minor but annoying issue, I have setup my django app to use my company's gmail account to send emails. The sending works fine, but the from in the emails is always the gmail account. Here are me settings/code - I have changed all the addresses etc, but to be sure everything works fine except for the from e-mail.

def send_discrepency_email(action, cart, atype):
    r = False
    user = cart.user
    subject = "%s - User Discrepency Response for order %s (%s)" % (action.upper(), cart.web_order_id, cart.syspro_order_id)
    message = "The user %s (%s) has opted to %s his/her order with the following discrepency type: %s." %(user.email, user.customer_id, action.upper(), atype)
    try:
        e = EmailMessage(subject, message, '[email protected]', ["[email protected]", "[email protected]"])
        e.send(fail_silently=False)
        r = True
    except Exception as e:
        print "Error sending discrepency email: %s" % e

    return r

Note: for the code I have also tried overriding with the headers kwarg to no avail.

DEFAULT_FROM_EMAIL = "[email protected]"
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'fakepword'
EMAIL_PORT = 587

Is this even doable with gmail, I swear I used this once before and it worked fine.

In short, no matter what changes I make, the FROM email is always [email protected]


Solution

  • This is a security setting with gmail. I ran into this issue with a rails app that I wrote. The from address must either be the account you sign in with aka '[email protected]' or an alias to that account. You can always just set the reply-to and get a similar effect to changing the from address. Or you will have to use a different SMTP server.