We are going to play around with sending e-mails again; this time we will send someone an e-mail whenever a file gets added to the file manager. The e-mail will contain a link to download the file.
The steps for sending an e-mail when a file has been uploaded, are as follows:
Set up the event listener in
/config/site_events.php
:Events::extend('on_file_add', 'FileEmailer', 'fileUploaded', 'libraries/file_emailer.php');
Create a new library in
/libraries/file_emailer.php
:Declare the new
FileEmailer
class:class FileEmailer { }
Add a method to the class called
fileUploaded
:public function fileUploaded($file, $fv) { }
Load the Mail helper:
$mail = Loader::helper('mail');
Set the subject of the message:
$mail->setSubject('A file has been added to the file manager');
Set the body of the message, including a link to download the file:
$mail->setBody('A new file has been uploaded. Download it here: '. $fv->getDownloadURL());
Add the recipient...