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