Email using cron and including attachment to email from moodle

て烟熏妆下的殇ゞ 提交于 2019-12-02 10:41:47

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!