Moodle 电子邮件功能不工作

Moodle Email Function Not Working

您好,我想在 moodle 数据库中插入记录后立即发送电子邮件 table,以下代码无效

if ($recs = $ti_form->get_data()) {
    // Do something with the data, then redirect to a new page
    $lastinsertid = $DB->insert_record('suggestions', $recs);
    $toUser = 'omerzia@live.com';
    $fromUser = 'ICAN';
    $subject = 'New Suggestion Added';
    $messageText = 'New Suggestion Added';
    $sent = email_to_user($toUser, $fromUser, $subject, $messageText);
    //mail($to, $subject, $message, $headers);

    if($sent) {
      print "Email successfully sent";
    }else{
      print "There was an error sending the mail";
     }

       redirect('suggestions.php');
}

以上代码提供了错误信息。可能是什么原因?非常感谢任何帮助。

谢谢

如果您查看 email_to_user() 函数的 phpdoc header,前两个参数应该是用户 objects 而不是字符串。

* @param stdClass $user  A {@link $USER} object
* @param stdClass $from A {@link $USER} object

所以对于用户

$touser = $DB->get_record('user', array('email' => 'omerzia@live.com');

对于发件人用户,您可以使用支持用户

$fromuser = core_user::get_support_user();

Enabling Email Debugging in Moodle

When developing email support in your Moodle plugin, you can make your life a whole lot easier by enabling email debugging however this should only be done in a development environment. The settings are all in the same place you would normally go to put Moodle debugging in DEVELOPER mode. Simply complete the following steps:

  1. 单击站点管理 > 高级功能 > 开发 > 调试
  2. 将调试消息设置为开发人员:针对开发人员的额外 Moodle 调试消息
  3. 选中“显示调试消息”框。
  4. 勾选调试邮件发送框。
  5. 点击页面底部的保存更改。

完成调试后不要忘记返回并禁用这些设置。

调试您的工作,这将向您显示一些可能对您的问题有所帮助的输出。