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
  • 23,288
  • 68
  • 183
  • 305
  • 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

2 Answers2

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.
  • 3,206
  • 22
  • 35
-2

You have to add DEFAULT_FROM_EMAIL, see the documentation.

schneck
  • 9,696
  • 11
  • 45
  • 70