Blue Flower

So I had an issue the other day with a block I'm coding for a Moodle branch called Totara. The issue I was having was adding a created PDF document as an attachment for email. The email was to be sent to both a user/requester and  a group email so it could be uploaded to another system using a parser. The documentation for the Moodle email_to_user function is kinda sparse. 

email_to_user($emailTo, $emailFrom, $subject, html_to_text($message), $message, $fileLocation, $filename);  

is the function in Moodle responsible for emailing users. A helpful link for explaining the first four values can be found here, and in the interest of not duplicating work, I'll just link it http://articlebin.michaelmilette.com/sending-custom-emails-in-moodle-using-the-email_to_user-function/.

Now for the part the link doesn't really explain. The values $attachment and $attachmentName are interesting because although the default values are "" and "". If you decide to use the $attachment value, you also have to use $attachmentName. Although it may at first seem kinda counter-intuitive the $attachment value does not supply the name. When I first started using the $attachement value I thought, incorrectly, that the $attachmentName was only for renaming an attachment if you wanted the name to be different then what was listed. The moodlelib.php that email_to_user() function resides in has and if statement that checks to make sure both attachment values have been set. I didn't realize this was the case, and thought it was odd I didn't receive any errors even when I supplied the wrong file location and name.

That brings me to my next point, in order to use the attachment feature provided by email_to_user() you have to have a link relative to the $CFG->dataroot. So in other words you have to use mkdir() and create a folder in the dataroot for use with the block. Then you have to not only supply the link to create the document in the directory in dataroot but also record the name you used to make the document. In other words your code will look something like this.

$fileLocation = "temp/block_folder";

$fileName = "someFileName.pdf";

if(!file_exists($CFG->dataroot."/".$fileLocation) {

    mkdir($CFG->dataroot."/".$fileLocation);

}

//This is the function that creates the PDF at the location provided.

$pdf->Output('F',$fileLocation."/".$fileName);

Then for the email_to_user it would look like this, of course you can do the actual call however you feel like. 

email_to_user($emailTo, $emailFrom, $subject, html_to_text($message), $message, $fileLocation, $filename); 

The above function is assuming you are using html for the $message text, which if you are, the first $message location is the plainText version of the HTML message. html_to_text() converts the html message to a plain text version, saving you from having to program a string strip function. 

The most important lesson I believe, is that you have to make sure the file you created gets placed in the $CFG->dataroot location because the email_to_user() function will strip the file location and make it relative to the $CFG->dataroot location whether you like it or not. Giving it a full path IE /var/www/html/moodle/block/my_block/folder/my.pdf will not work and will in fact be made relative to the $CFG->dataroot location, so it will look like $CFG->dataroot ."/"var/www/html/...... 

I hope this helps someone in my predicament who is trying to create a document and send it to people in the Moodle/Totara user database. This works in both Moodle and Totara because the Totara core is based off of the Moodle core especially in this regard. The other part to remember is the emails are logged, so if any issues pop up you will have the logging to see what issues might be preventing your emails from being sent.  

thanks,

Lee