且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送带有附件django的电子邮件

更新时间:2022-11-16 23:00:06

假设您有一个模型,

Assuming you have a model as,

class MyModel(models.Model):
    # other fields
    presentation = models.FileField(upload_to='some/place/')



并在您的信号中,

import mimetypes


def mail_business_plan(sender, instance, created, **kwargs):
    if created:
        ctx = {"ctx": instance}

        from_email = 'info@some_email.in'

        subject = 'Business Plan by' + instance.company_name
        message = get_template('email/business_team.html').render(ctx)
        to = ['some_email@gmail.com']
        mail = EmailMessage(subject, message, to=to, from_email=from_email)

        content_type = mimetypes.guess_type(instance.presentation.name)[0] # change is here <<<
        mail.attach_file(instance.presentation, instance.presentation.read(), content_type) # <<< change is here also

        return mail.send()



参考:

mimetypes.guess_type()