我無法找到此代碼中的錯誤。我試過隔離這個問題,但它工作當我將相關的代碼複製到一個單獨的文件。問題必須與周圍的代碼,但我不明白它甚至是相關的。這裏的一切:爲什麼Django發送錯誤的電子郵件模板?
問題是與「激活您的PROJECT帳戶」電子郵件。它向我發送一封電子郵件,它是這樣的:
--===============1413769924==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
text here
--===============1413769924==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
text here
其中「文本這裏」是電子郵件的兩個版本是相同的,但它是不是在實際文件和ISN」 t當我嘗試從不同的腳本發送電子郵件。下面的代碼:
def join_transporter(request):
form = TransporterJoinForm(request.POST)
if request.POST and form.is_valid():
user = User(
username = form.username.val,
first_name = form.first_name.val,
last_name = form.last_name.val,
email = form.email.val,
is_active = False,
)
user.set_password(form.password1.val)
user.save()
Profile.objects.create(
user = user,
phone = form.phone.val,
company_name = form.company_name.val,
details = Transporter.objects.create(),
address = Address.objects.create(
city = form.address.city.val,
province = form.address.province.val,
country = form.address.country.val,
street = form.address.street.val,
postal_code = form.address.postal.val
)
)
e = send_multipart_email('Activate Your PROJECT Account',
'emails/transporter_joined',
{'user':user, 'fee': settings.PROJECT_FEE * Decimal('100.00'),'settings':settings},
[user.email],
send_immediately=False
)
e.attach_file('/home/PROJECT/webapps/media/files/InsuranceLetter.pdf')
e.send() # <------- PROBLEM IS HERE
send_multipart_email('Transporter Joined',
'emails/staff_transporter_joined',
{'trans':user,'settings':settings},
['[email protected]','[email protected]']
)
messages.info(request, 'Thank you for registering. Please check your email for details on how to activate your account.')
return redirect('home')
def send_multipart_email(subject, template, data_dict, recipient_list, from_email=settings.DEFAULT_FROM_EMAIL, send_immediately=True):
if not isinstance(recipient_list, list): recipient_list = [recipient_list]
d = {'settings':settings} # default context vars
d.update(data_dict)
c = Context(d)
try:
tt = loader.get_template(template+'.txt')
except:
try:
ht = loader.get_template(template+'.html')
except:
raise Exception('Neither `%(tmpl)s.txt` or `%(tmpl)s.html` could be found.' % {'tmpl':template})
else:
e = EmailMultiAlternatives(subject, ht.render(c), from_email, recipient_list)
e.content_subtype = 'html'
else:
e = EmailMultiAlternatives(subject, tt.render(c), from_email, recipient_list)
try:
ht = loader.get_template(template+'.html')
except:
pass
else:
e.attach_alternative(ht.render(c), 'text/html')
if send_immediately:
e.send()
return e
[[email protected] emails]$ ls
account_activated.txt shipper_accepted_bid.txt transporter_awarded_shipment.txt
base.html shipper_joined.html transporter_bid_declined.html
base.txt shipper_joined.txt transporter_bid_declined.txt
forgot_password.html staff_transporter_joined.html transporter_joined.html
forgot_password.txt staff_transporter_joined.txt transporter_joined.txt
invoice_generated.html transporter_approved.html transporter_lost_auction.html
invoice_generated.txt transporter_auction_closed.html transporter_lost_auction.txt
shipper_accepted_bid.html transporter_auction_closed.txt
票:http://code.djangoproject.com/ticket/13364
哈哈,我得說,我也錯過了。沒有錯別字,它必須是別的東西。 – synic 2010-04-16 20:34:09
我能想到的唯一的其他事情是'ht.render(c)'....模板加載器Gizmo可能會緩存他'.txt'版本,而不是使用新模板更新它? – mpen 2010-04-16 20:38:52
這是什麼版本的Django?曾幾何時,我雖然Django模板加載器採用了'.html'的擴展名,但後來改變了。 – 2010-04-16 20:43:05