What is the correct way of sending a mail with an attachment to user in Moodle 2.0 ?

What is the correct way of sending a mail with an attachment to user in Moodle 2.0 ?

by Jean-Michel Védrine -
Number of replies: 3

Hello,

I need to write code to send an email with an attachment to an user.

In Moodle 1.9 my code was using email_to_user.

Now with the new filestorage API, I need to upgrade my code.

email_to_user is expecting a full path from dataroot dir. I see nothing in the filestorage API to get this full path.

Maybe I am wrong but something seems missing to me in Moodle APIs either in moodlelib/email_to_user or in filestorage.

I searched moodle code but the only example I have is what Chardelle Busch has done in the certificate module replicating a private function of the filestorage lib (function certificate_path_from_hash). I am sure this is dangerous and the sky will fall on my head someday if i do this, and filestorage lib changes.

There must be some better way. I must be missing something in filestorageAPI blush

I looked at the new messaging library but the email output seems no to support attachments.

Unfortunately I can't use a link, I need to send a real attachment

Can somebody help me ?

Thanks a lot.

Average of ratings: -
In reply to Jean-Michel Védrine

Re: What is the correct way of sending a mail with an attachment to user in Moodle 2.0 ?

by Jean-Michel Védrine -

Well I was blind !

In fact i think it was just under my nose but I was not reading the file API Doc correctly. Nothing is missing in the API and all that I need seems to be in the docs List files area

I will test and come back if ever it doesn't work.

In reply to Jean-Michel Védrine

Re: What is the correct way of sending a mail with an attachment to user in Moodle 2.0 ?

by Jean-Michel Védrine -

Too bad, I was happy too quickly !
Because as I say in my first message email_to_user is expecting a full path from $CFG->dataroot and filestorage system absolutely don't want wide eyes to give it to me !

stored_file.php lib clearly says in the comment before the get_content_file_location protected function :
Protected - developers must not gain direct access to this function.
I think this kind of "rigidity" (sorry, no offence to the authors) is in fact counter-productive because in my search I have found at least 3 or 4 contributed codes that implement their own "ugly hack" to get the file location.
But I will not do so.
I will using the file API to copy my file to a temp dir and use that location in calling email_to_user. I am fearing some performance issues as I need to do that for quite a lot of users (each user must be mailed it's own file) but i will see.
I will also post a tracker issue because in my opinion email_to_user hasn't been properly upgraded to file API and it should be possible to use something other than a full path from $CFG->dataroot for the attachment.

In reply to Jean-Michel Védrine

Re: What is the correct way of sending a mail with an attachment to user in Moodle 2.0 ?

by Jonas Asa. -

Hi. How did you solve the problem? Can you show some code? Did you open a tracker issue?

I can get the filename

$requestform->get_new_filename('userfile')

but not the path to include as an argument for email_to_user.

Thank you.