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()