更新时间: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()