Send e-mail with large files from form attached

482 views
Skip to first unread message

tezro

unread,
Mar 17, 2010, 4:57:06 AM3/17/10
to Django users
Hello everyone. I need some help or advice.

I've got a form with 20 ImageFields - such a form for sending photos
to the site admin as a request for a new user. Well, Django certainly
handles and uploads the, that's OK. But when it comes to sending all
the files as an attachment - I got stuck.

Here's a simple example of how I tried to do that:
------------------------------------------------
from django.core.mail import EmailMessage

email = EmailMessage()
for (k, v) in request.FILES.items():
email.attach(v.name, v.read())
------------------------------------------------

Small files are read nicely. But when someone "clever" fills out all
the form files (all the twenty) with images each one at least 10 Mb -
Django consumes so much memory... so I'm not in knowledge to handle
that.

Please, guide me how to handle big files from a form and send them
without memory leaks. Thanks ahead.

Paulo Almeida

unread,
Mar 18, 2010, 12:59:05 PM3/18/10
to django...@googlegroups.com
I can't help you with the technical part, but a couple of suggestions:

* Sum the size of the images and send two (or more) e-mails if it exceeds a threshold
* Forget attachments and just zip the images and make them accessible in an URL that is e-mailed to the site admin

- Paulo


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Jirka Vejrazka

unread,
Mar 18, 2010, 1:19:18 PM3/18/10
to django...@googlegroups.com
> I've got a form with 20 ImageFields - such a form for sending photos
> to the site admin as a request for a new user. Well, Django certainly
> handles and uploads the, that's OK. But when it comes to sending all
> the files as an attachment - I got stuck.

> Small files are read nicely. But when someone "clever" fills out all


> the form files (all the twenty) with images each one at least 10 Mb -
> Django consumes so much memory... so I'm not in knowledge to handle
> that.

Hi,

email is not really suitable for sending large amounts of data. The
fact that people use it that way is a bit sad, but you should not
really follow their example.

So, you're trying to send up to 200 MB via email. That's in fact
some 270 MB, because binary data is base64 encoded in emails,
typically (not always, but often). That is the reason for the memory
problem.

As Paulo pointed out, created a unique, temporary URL for
downloading those images and send just the link to that URL.

HTH

Jirka

tezro

unread,
Mar 19, 2010, 9:41:27 AM3/19/10
to Django users
Hi. All that you say is totally clear to me, but I tried to suit the
customer's needs. And, yes, I finished my deals on just saving the
images serverside and sending links to files over e-mail. Thanks for
that.

Another question that bothers me is the following.

-------------------------------------------------------------


for (k, v) in request.FILES.items():

file_data = open('%s/%s' % (dir, filename), 'wb')
file_data.write(v.read())
file_data.close()
-------------------------------------------------------------

That's not all the loop but that's enough to understand that I write
uploaded files to another location to give a link in e-mail.

I'm not really sure if it's correct - the files after upload are saved
to temp directory or RAM, but I actually read them again "v.read()" to
save to another loaction. Is that ok? Is there other way to save
uploaded files without reading them again?

Thanks.

On Mar 18, 8:19 pm, Jirka Vejrazka <jirka.vejra...@gmail.com> wrote:
> > I've got a form with 20 ImageFields - such a form for sending photos
> > to the site admin as a request for a new user. Well, Django certainly
> > handles and uploads the, that's OK. But when it comes to sending all

> > thefilesas an attachment - I got stuck.
> > Smallfilesare read nicely. But when someone "clever" fills out all
> > the formfiles(all the twenty) with images each one at least 10 Mb -


> > Django consumes so much memory... so I'm not in knowledge to handle
> > that.
>
> Hi,
>

>  emailis not really suitable for sendinglargeamounts of data. The


> fact that people use it that way is a bit sad, but you should not
> really follow their example.
>

>   So, you're trying to send up to 200 MB viaemail. That's in fact

tezro

unread,
Mar 19, 2010, 9:43:40 AM3/19/10
to Django users
Thanks for reply. I tried both variants before the post :) Nothing
suited me and the customer.

When a-mailng files are uploaded to RAM anyway, zipping files just
don't really help on binary data. I chosed to save them to a web-
visible location.

On Mar 18, 7:59 pm, Paulo Almeida <igcbioinformat...@gmail.com> wrote:
> I can't help you with the technical part, but a couple of suggestions:
>
> * Sum the size of the images and send two (or more) e-mails if it exceeds a
> threshold
> * Forget attachments and just zip the images and make them accessible in an
> URL that is e-mailed to the site admin
>
> - Paulo
>

> On Wed, Mar 17, 2010 at 8:57 AM, tezro <tezro...@gmail.com> wrote:
> > Hello everyone. I need some help or advice.
>
> > I've got a form with 20 ImageFields - such a form for sending photos
> > to the site admin as a request for a new user. Well, Django certainly
> > handles and uploads the, that's OK. But when it comes to sending all
> > the files as an attachment - I got stuck.
>
> > Here's a simple example of how I tried to do that:
> > ------------------------------------------------
> > from django.core.mail import EmailMessage
>
> > email = EmailMessage()
> > for (k, v) in request.FILES.items():
> >    email.attach(v.name, v.read())
> > ------------------------------------------------
>
> > Small files are read nicely. But when someone "clever" fills out all
> > the form files (all the twenty) with images each one at least 10 Mb -
> > Django consumes so much memory... so I'm not in knowledge to handle
> > that.
>
> > Please, guide me how to handle big files from a form and send them
> > without memory leaks. Thanks ahead.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>

Paulo Almeida

unread,
Mar 19, 2010, 10:20:44 AM3/19/10
to django...@googlegroups.com
Hi,

My suggestion to zip the files was to provide a single download link with all the files.

- Paulo

To unsubscribe from this group, send email to django-users...@googlegroups.com.

tezro

unread,
Mar 19, 2010, 10:32:54 AM3/19/10
to Django users
Oh. I get it, sorry. Any suggestions on my next question about
file.read() would be thankful.

On Mar 19, 5:20 pm, Paulo Almeida <igcbioinformat...@gmail.com> wrote:
> Hi,
>
> My suggestion to zip the files was to provide a single download link with
> all the files.
>
> - Paulo
>

> > <django-users%2Bunsu...@googlegroups.com<django-users%252Buns...@googlegroups.com>

Paulo Almeida

unread,
Mar 19, 2010, 11:35:58 AM3/19/10
to django...@googlegroups.com
Oh, I missed that part. I'm not an expert, so you may want to investigate alternatives, but with the shutil module you can copy files or entire directories:

http://docs.python.org/library/shutil.html

Look for copy and copytree.

Best,
Paulo

To unsubscribe from this group, send email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages