0

From the documentation, I see that the host, port, username, and password can be defined in a backend file, but I want to define all of them in my code itself. Can this be done? If so, how?

from django.core.mail import EmailMessage

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
)

message.attach_file('/images/weather_map.pdf')

Thanks in advance!

Update:

I want to avoid storing credentials in any file. Ultimately, I want the code to prompt for the username and password as input variables. Update:

I tried this:

import pandas as pd
from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend
attachment_path=r'C:\path'+'\\'

connection = EmailBackend(
    host='host',
    port=587,
    username='login',
    password='password'
)

email = EmailMessage(
    'Hello',
    'Body goes here',
    'example@example.com',
    ['example@example.com'],
    ['example@example.com'],
    reply_to=['example@example.com'],
    headers={'Message-ID': 'foo'},
    connection=connection
)
email.attach_file(attachment_path+'attachment.pdf')
email.send()
Dance Party2
  • 7,214
  • 17
  • 59
  • 106
  • Trying to avoid storing credentials in your code is a good aim. However, you should consider loading the credentials from environment variables in your settings, rather than passing the credentials every time you send an email. – Alasdair Aug 15 '17 at 14:46

2 Answers2

5

You can use get_connection to instantiate an email backend:

from django.core.mail import get_connection

connection = get_connection(
    host='...',
    port='...',
    username='...',
    ...
)

Then pass your connection when instantiating the EmailMessage.

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
    connection=connection,
)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I got an error that EmailBackend is not defined. Do I need to do from django.core.mail.backends.smtp import EmailBackend first? – Dance Party2 Aug 15 '17 at 15:01
  • Oops, that was a typo. It should be `get_connection()`. An alternative would be to do `from django.core.mail.backends.smtp import EmailBackend ` then instantiate `EmailBackend`. – Alasdair Aug 15 '17 at 15:06
  • So I tried that and got this error: ImproperlyConfigured: Requested setting EMAIL_USE_TLS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. – Dance Party2 Aug 15 '17 at 15:59
  • So I think I just need to know how to call settings.configure() – Dance Party2 Aug 15 '17 at 16:07
  • Configuring settings in a standalone script is a completely different issue. See [the docs](https://docs.djangoproject.com/en/1.11/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage) or [this question](https://stackoverflow.com/questions/39723310/django-standalone-script). – Alasdair Aug 15 '17 at 16:11
  • Sorry, I assumed that it would somehow work from my main code. It looks like I can't avoid having to set environment variables or store credentials in another file with Django. – Dance Party2 Aug 15 '17 at 16:14
  • You don't need to store any credentials in another file. The above method should work, you just have to configure the settings in your standalone script. – Alasdair Aug 15 '17 at 16:18
  • You might be able to avoid having the settings referenced by passing [all the arguments](https://docs.djangoproject.com/en/1.11/topics/email/#smtp-backend) when instantiating the backend. – Alasdair Aug 15 '17 at 16:22
0

Django is just a Python package. You could do this literally in a million different ways.

You can import the class anywhere (eg views.py etc):

from django.core.mail import EmailMessage

And then call it as in the docs..

If you don't know what views.py is then i strongly recommend following the tutorial

joeskru
  • 242
  • 3
  • 16
  • I think I'm following, but due to restrictions at work, I need to avoid storing credentials anywhere (I am ultimately going to have the code ask for credentials as as input). – Dance Party2 Aug 15 '17 at 14:43