Sending An Email Attachment In Django (Production)

172 Views Asked by At

I am wanting to add an attachment to an email.

I am running this on a production server (I can do this in development).

I have successfully stored my static files in static_files using whitenoise and they display correctly on the page. I am trying to send one of these static files in this email.

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
if form.is_valid():

    # have also tried '\\attachment\\test.pdf'
    file_path = os.path.join(settings.STATIC_ROOT+'/attachment/test.pdf')

    try:
         msg = EmailMessage('subject', 'body', '#@#.#', '#@#.#')
                
         with open(file_path, 'rb') as attach_file:
             data = attach_file.read()
                    
         msg.add_attachment('test.pdf', data, 'application/pdf')

         msg.send()

Path to the file:

/home/project/project/static_files/attachment/test.pdf

This throws a 500 error.

Thank you.

0

There are 0 best solutions below