I can't find the bug in this code. I've tried isolating the problem, but it works when I copy the relevant code into a separate file. The problem must be with the surrounding code, but I don't see how it's even relevant. Here's everything:
The problem is with the "Activate Your PROJECT Account" email. It sends me an email with something like this in it:
--===============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
Where "text here" is the same for both versions of the email, but it isn't in the actual files, and isn't when I try sending the emails from a different script. Here's the code:
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
[PROJECT@SERVER 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