我正在尝试使用Django发送电子邮件,这是我的views.py
def contact(request):
if request.method == 'GET':
return TemplateResponse(request, "coming_soon/contact.html", {"contact": "true"})
elif request.method == 'POST':
form = ContactForm(request.POST, request.FILES)
is_valid = form.is_valid()
errors = []
if is_valid:
form.save()
name = form.cleaned_data['name']
surname = form.cleaned_data['surname']
telephone = form.cleaned_data['telephone']
from_email = form.cleaned_data['email']
if form.cleaned_data['attachment1']:
attachment1 = request.FILES['attachment1']
if form.cleaned_data['attachment2']:
attachment2 = request.FILES['attachment2']
if form.cleaned_data['attachment3']:
attachment3 = request.FILES['attachment3']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
print "data saved"
#sending email
to = EMAIL_TO
sender = EMAIL_HOST_USER
body = 'name:' + name + ' ' + 'surname:' + surname + ' ' + 'telephone:' + telephone + '\n' + 'message:' + message + '\n' + 'from:' + from_email
email = EmailMessage(subject, body, sender, [to])
email.attach_file(attachment1)
email.attach_file(attachment2)
email.attach_file(attachment3)
email.send()
else:
for e in form.errors:
errors.append(e)
json_errors = json.dumps(errors)
return HttpResponse("<script language='javascript' type='text/javascript'>window.top.window.upload_finished("+json_errors+");</script>",content_type="text/html")
和我的models.py:
class Contact(models.Model):
name = models.CharField(max_length=255)
surname = models.CharField(max_length=255)
telephone = models.CharField(max_length=255)
email = models.EmailField(max_length=100)
subject = models.CharField(max_length=255)
attachment1 = ContentTypeRestrictedFileField(upload_to='attachments', blank=True,max_upload_size=settings.FILE_MAX_UPLOAD_SIZE)
attachment2 = ContentTypeRestrictedFileField(upload_to='attachments', blank=True,max_upload_size=settings.FILE_MAX_UPLOAD_SIZE)
attachment3 = ContentTypeRestrictedFileField(upload_to='attachments', blank=True,max_upload_size=settings.FILE_MAX_UPLOAD_SIZE)
message = models.CharField(max_length=4000)
def __unicode__(self):
return self.name+" "+self.surname+" "+self.subject
并在forms.py中我这样做:
class ContactForm(ModelForm):
class Meta:
model = Contact
当我尝试发送带附件的电子邮件时,它说:
ERROR Internal Server Error: /en/contact/
Traceback (most recent call last):
File "D:\workspace\principal_env\lib\site-packages\django\core\handlers\base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\workspace\principal\apps\coming_soon\views.py", line 87, in contact
email.attach_file(attachment1)
File "D:\workspace\principal_env\lib\site-packages\django\core\mail\message.py", line 293, in attach_file
filename = os.path.basename(path)
File "D:\workspace\principal_env\lib\ntpath.py", line 198, in basename
return split(p)[1]
File "D:\workspace\principal_env\lib\ntpath.py", line 170, in split
d, p = splitdrive(p)
File "D:\workspace\principal_env\lib\ntpath.py", line 125, in splitdrive
if p[1:2] == ':':
TypeError: 'InMemoryUploadedFile' object is not subscriptable
请帮我解决这个问题
答案 0 :(得分:0)
嗯,它需要email.attach_file()
中的路径,我发现form.instance.attachment1
所以我的email.attach_file()看起来像这样
if form.instance.attachment1:
email.attach_file(MEDIA_ROOT + '/' + str(form.instance.attachment1))
if form.instance.attachment2:
email.attach_file(MEDIA_ROOT + '/' + str(form.instance.attachment2))
if form.instance.attachment3:
email.attach_file(MEDIA_ROOT + '/' + str(form.instance.attachment3))
希望它会帮助别人