1

I am trying to use django's builtin Email class EmailMessage to send an email with attachment like below

def send_email(path, from_email, to_list, file_name):
    subject = 'Output for csv file %s'%(file_name,)
    body = 'Please find the attached output file your csv input file %s'%(file_name,)
    message = EmailMessage(subject, body, from_email, to_list)
    message.attach_file(path)
    message.send()

send_email('/home/user/hello.csv', 'myself@gmail.com', ['client@gmail.com'], 'functional_test')

And my Email settings in settings.py are

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'actual@myhost.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

So from the above settings the EMAIL_HOST_USER was actual@myhost.com and when i am calling my send_email function i am specifying from_email as myself@gmail.com.

So when i received the email i am receiving it from actual@myhost.com instead of myself@gmail.com even though we specified different from_email.

Is there anything that i can do to receive the mails with from address as myself@gmail.com ?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • 2
    Possible duplicate of [The email\_from in Django send\_mail function not working](https://stackoverflow.com/questions/6803009/the-email-from-in-django-send-mail-function-not-working) – Hayden Crocker Aug 16 '17 at 13:56

3 Answers3

1

Got the same issue, @HaydenCrocker is right.

It’s Gmail’s restrictionists, need to change another email server if really want to customers from_email.

C.K.
  • 4,348
  • 29
  • 43
0

if you are using Gmail, you need to add the email you want use as alias

Follow instructions below

https://support.google.com/a/answer/33327?product_name=UnuFlow&hl=en&visit_id=638041629555716720-783343126&rd=1&src=supportwidget0&hl=en

andylee
  • 255
  • 2
  • 6
-2

You have to add DEFAULT_FROM_EMAIL, see the documentation.

schneck
  • 10,556
  • 11
  • 49
  • 74