Email using cron and including attachment to email from moodle

前端 未结 1 1691
余生分开走
余生分开走 2021-01-27 17:12

I would like to send emails only to users that have completed a specific course and add a pdf file (a certificate for completing the course) as attachment to the email, and do s

相关标签:
1条回答
  • 2021-01-27 17:42

    This is an overview.

    1. First create a local plugin. For example /local/yourplugin

    https://docs.moodle.org/dev/Local_plugins

    1. Then set up a message provider

    https://docs.moodle.org/dev/Message_API

    defined('MOODLE_INTERNAL') || die();

    in local/yourplugin/db/messages.php

    $messageproviders = array (
        'coursecompleted' => array (
        ),
    
    1. Then add an event observer - you will want to respond to the course_completed event

    https://docs.moodle.org/dev/Event_2

    in /local/yourpluginname/db/events.php

    have something like

    $observers = array(
    
        array(
            'eventname'   => '\core\event\course_completed',
            'callback'  => 'local_yourplugin_observer::course_completed',
        ),
    
    );
    
    1. Now add the message code

    Add something like this to '/local/message/classes/observer.php'

    defined('MOODLE_INTERNAL') || die();
    
    class local_yourplugin_observer {
    
        /**
         * Triggered when 'course_completed' event is triggered.
         *
         * @param \core\event\course_completed $event
         * @return bool
         */
        public static function course_completed(\core\event\course_completed $event) {
    
            // Your code here.
    
            $message = new \core\message\message();
            $message->component = 'local_yourplugin'; // Name of your local plugin.
            $message->name = 'coursecompleted'; // Name of message provider.
            $message->userfrom = $USER;
            $message->userto = $user;
            $message->subject = 'message subject 1';
            $message->fullmessage = 'message body';
            $message->fullmessageformat = FORMAT_MARKDOWN;
            $message->fullmessagehtml = '<p>message body</p>';
            $message->smallmessage = 'small message';
            $message->notification = '0';
            $message->contexturl = 'http://GalaxyFarFarAway.com';
            $message->contexturlname = 'Context name';
            $message->replyto = "random@example.com";
            $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for specific processor
            $message->set_additional_content('email', $content);
    
            // Create a file instance.
            $usercontext = context_user::instance($user->id);
            $file = new stdClass;
            $file->contextid = $usercontext->id;
            $file->component = 'user';
            $file->filearea  = 'private';
            $file->itemid    = 0;
            $file->filepath  = '/';
            $file->filename  = '1.txt';
            $file->source    = 'test';
    
            $fs = get_file_storage();
            $file = $fs->create_file_from_string($file, 'file1 content');
            $message->attachment = $file;
    
            $messageid = message_send($message);
        }
    }
    
    0 讨论(0)
提交回复
热议问题