How to send an uploaded image as email attachment

82 views
Skip to first unread message

Arthur Clemens

unread,
May 20, 2013, 2:07:39 PM5/20/13
to zotoni...@googlegroups.com
I've followed http://zotonic.com/documentation/807/implementing-a-simple-contact-form
Now I've added an upload field to the form. I see it creates an upload object that looks like this:

{upload,"2013-05-17 11.22.24.png", "/tmp/ztmp-zo...@arthur-2-1369.71252.84600",undefined,undefined}

The third attribute is the tmp file location. How do I access that image and attach it to the email?

I guess one of the operations is to rename it to the original filename so that it can be recognized as image.

Arthur

Arjan Scherpenisse

unread,
May 29, 2013, 3:40:02 PM5/29/13
to zotoni...@googlegroups.com
Only seeing this mail just now...

The #email{} record lets you specify more options when sending email.
It has an attachments list which lets you specify files or #upload{}
records which will be attached.

For instance:

z_email:send(#email{
to=admin_email(Context),
html_tpl="some_email.tpl",
attachments=[#upload{tmpfile="/tmp/foo.png",
mime="image/png"}]
},
Context),

In your case you can pass the upload record straight through from the
form; I guess something like this:

Upload = z_context:get_q("yourfile", Context),
z_email:send(#email{
to=admin_email(Context),
html_tpl="some_email.tpl",
attachments=[Upload]
},
Context),


cheers, Arjan
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Zotonic users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to zotonic-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Marc Worrell

unread,
Jun 25, 2013, 3:16:48 AM6/25/13
to zotoni...@googlegroups.com

On 29 mei 2013, at 21:40, Arjan Scherpenisse wrote:

> The #email{} record lets you specify more options when sending email.
> It has an attachments list which lets you specify files or #upload{} records which will be attached.
>
> In your case you can pass the upload record straight through from the form; I guess something like this:
>
> Upload = z_context:get_q("yourfile", Context),
> z_email:send(#email{
> to=admin_email(Context),
> html_tpl="some_email.tpl",
> attachments=[Upload]
> },
> Context),

Be careful that the file mentioned in a query upload arg is a temporary file.
It is deleted when the request process is killed (or stops).

E-mail is sent asynchronously, and might be deferred to a later time if there are errors.
(Quite common situation is gray listing where your first e-mail is refused with a temporary error, but a later attempt will pass without any problems)

So, you need to store your uploaded file in a more definitive place or read it from disk and add it to the #upload.data field:

{ok, Data} = file:read_file(Upload#upload.tmpfile),
{ok, IdnProps} = z_media_identify:identify(Upload),
Upload1 = #upload{data=Data, filename=#upload.filename, mime=proplists:get_value(mime, IdnProps)}.

This will ensure that your data doesn't disappear whilst sending the e-mail.
It also does a best effort to find the correct mime type; browsers often send invalid or wrong mime types.

Of course, you might want to check that the user didn't upload a 400MB movie before reading it in to memory and sending it off to your inbox...

- Marc

Kirill Sysoev

unread,
Sep 15, 2013, 9:35:03 AM9/15/13
to zotoni...@googlegroups.com
Hi Marc, Arjan, Arthur  and other zotonic fans!

Everything seem to work fine in this example, but is there a way to check uploaded file size before or in process of uploading to prevent big files to occupy hard disk totally.

In the other words, how to limit maximum size of uploaded files?

Thanks in advance,
Kirill

> Of course, you might want to check that the user didn't upload  a 400MB movie before reading it in to memory and sending it off to your inbox... 



вторник, 25 июня 2013 г., 11:16:48 UTC+4 пользователь Marc Worrell написал:

Andreas Stenius

unread,
Sep 15, 2013, 3:13:52 PM9/15/13
to Zotonic users
If you use mod_acl_simple_roles, there's a file size limit option for the role you can set.
It seems that the whole file is received before the check though..

See:

There might be other (better?) options, but I've no experience with file uploads..



2013/9/15 Kirill Sysoev <kirill...@gmail.com>

Kirill Sysoev

unread,
Sep 16, 2013, 4:09:42 AM9/16/13
to zotoni...@googlegroups.com
Thank you for your answer Andreas.

I don't use mod_acl_simple_roles. 
I just check size of uploaded to /tmp file with filelib:file_size(FileName)
Everything works fine but seems to be not very secure.
I'd feel myself more confident if I could control size of a file during upload process.

Best,
Kirill

 


2013/9/15 Andreas Stenius <andreas...@astekk.se>

Andreas Stenius

unread,
Sep 16, 2013, 4:30:51 AM9/16/13
to Zotonic users
This would require a minor tweak here:
In order to provide some custom file size limitations..

Unless you use your own controller (webmachine resource) as described here: https://github.com/basho/webmachine/wiki/Streamed-Body



2013/9/16 Kirill Sysoev <kirill...@gmail.com>

Kirill Sysoev

unread,
Sep 16, 2013, 5:04:04 AM9/16/13
to zotoni...@googlegroups.com
Thank you for hint, Andreas!

Every time I dig deeper into zotonic it starts shining even more :)

Best,
Kirill

понедельник, 16 сентября 2013 г., 12:30:51 UTC+4 пользователь Andreas Stenius написал:

Andreas Stenius

unread,
Sep 16, 2013, 7:54:46 AM9/16/13
to Zotonic users
2013/9/16 Kirill Sysoev <kirill...@gmail.com>
Thank you for hint, Andreas!

You are welcome :)
 

Every time I dig deeper into zotonic it starts shining even more :)

I agree, it's a rough diamond that with some polishing is truly spectacular :)

Reply all
Reply to author
Forward
0 new messages