Allow file attachments in storages other than local files (eg S3). Fixes GH-249.

This commit is contained in:
Ross Poulton 2014-09-02 18:36:00 +10:00
parent 1aed98463a
commit 4c901880bc
3 changed files with 10 additions and 10 deletions

View File

@ -268,7 +268,7 @@ class TicketForm(CustomFieldMixin, forms.Form):
# Only files smaller than 512kb (or as defined in
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
try:
files.append(a.file.path)
files.append([a.filename, a.file])
except NotImplementedError:
pass
@ -441,7 +441,7 @@ class PublicTicketForm(CustomFieldMixin, forms.Form):
if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000):
# Only files smaller than 512kb (or as defined in
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
files.append(a.file.path)
files.append([a.filename, a.file])
context = safe_template_context(t)

View File

@ -45,8 +45,8 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b
fail_silently is passed to Django's mail routine. Set to 'True' to ignore
any errors at send time.
files can be a list of file paths to be attached, or it can be left blank.
eg ('/tmp/file1.txt', '/tmp/image.png')
files can be a list of tuple. Each tuple should be a filename to attach,
along with the File objects to be read. files can be blank.
"""
from django.conf import settings
@ -123,11 +123,11 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b
msg.attach_alternative(html_part, "text/html")
if files:
if type(files) != list:
files = [files,]
for file in files:
msg.attach_file(file)
for attachment in files:
file_to_attach = attachment[1]
file_to_attach.open()
msg.attach(filename=attachment[0], content=file_to_attach.read())
file_to_attach.close()
return msg.send(fail_silently)

View File

@ -410,7 +410,7 @@ def update_ticket(request, ticket_id, public=False):
if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000):
# Only files smaller than 512kb (or as defined in
# settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email.
files.append(a.file.path)
files.append([a.filename, a.file])
if title != ticket.title: