我有一个用户可以要求提供文件的表格。他们需要输入姓名和电子邮件地址,文档将被发送到该电子邮件地址。
怎么做?
我的代码如下:
if download_form.is_valid():
ln_go = get_lang_code(request, request.GET.get('lang'))
file_to_be_sent = "/assets/documents/case_study" + ln_go + ".pdf"
name = request.POST.get('name')
email = request.POST.get('email')
msg = EmailMultiAlternatives('Het aangevraagde document',
'Geachte {0}, <br/><br />In bijlage sturen wij u het aangevraagde document door. <br /><br />Met vriendelijke groeten, <br /><br /> Het Autralis team'.format(name),
'no-reply@autralis.com',
[email, ], # TO
['some-email-address@gmail.com', ]) # cc
msg.content_subtype = "html" # Main content is now text/html
msg.attach("case_study.pdf", file_to_be_sent, "application/pdf")
msg.send()
这些文件所在的项目树如下:
我有两个问题:
答案 0 :(得分:1)
你应该将content
放在attach方法中:
import os
from django.conf import settings
file_to_be_sent = "assets/documents/case_study{}.pdf".format(ln_go)
file_to_be_sent = os.path.join(settings.BASE_DIR, file_to_be_sent)
with open(file_to_be_sent) as f:
msg.attach("case_study.pdf", f.read(), "application/pdf")
或使用attach_file
方法
msg.attach_file(file_to_be_sent)
中方法的详细信息