在Django中发送电子邮件附件(生产)

时间:2020-09-29 10:47:59

标签: django django-email

我想在电子邮件中添加附件。

我正在生产服务器上运行它(我可以在开发中执行此操作)。

我已经使用whitenoise成功地将我的静态文件存储在static_files中,并且它们正确显示在页面上。我正在尝试在此电子邮件中发送这些静态文件之一。

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

文件路径:

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

这将引发500错误。

谢谢。

0 个答案:

没有答案