You can send custom emails from within Moodle using PHP using your own forms using the email_to_user() function. By the way, did you know that Moodle actually uses the open source PHPMailer behind the scenes?

A typical call to Moodle's email_to_user() function would look like:

email_to_user($toUser, $fromUser, $subject, $messageText, $messageHtml, '', '', true);

This is a very simple version of a very powerful function. For more information about the email_to_user() function, take a look at the source code for function email_to_user in Moodle's /lib/moodlelib.php.

If you wanted to also include an attachment, you would need to also specify the path to the file to attach as well as give it a file name in the 6th and 7th parameter. The call to email_to_user() should look like this:

email_to_user($toUser, $fromUser, $subject, $messageText, $messageHtml, $completeFilePath, $nameOfFile, true);

Tips for Using email_to_user()

  1. $toUser and $fromUser must be Moodle user objects, not email addresses. The stdClass object must contain a minimum of the following:
    • $emailuser->email: Email address
    • $emailuser->firstname: You can put both first and last name in this field.
    • $emailuser->lastname
    • $emailuser->maildisplay = true;
    • $emailuser->mailformat = 1; // 0 (zero) text-only emails, 1 (one) for HTML/Text emails.
    • $emailuser->id: Moodle User ID. If it is for someone who is not a Moodle user, use an invalid ID like -99.
    • $emailuser->firstnamephonetic
    • $emailuser->lastnamephonetic
    • $emailuser->middlename
    • $emailuser->alternatename
    • Except for those fields above that have a description beside them, the rest of the fields can be set to an empty string if you don't have the information available.
  2. You can convert your HTML message into plain text using:
    $messagetext = html_to_text($messagehtml);
  3. You can get the configured support user information using the following two variables:
    • $CFG->supportemail
    • $CFG->supportname
  4. If you want the email to come from noreply@yourwebsite.com, set the true parameter to  false .

Be sure to always use Moodle filters to clean any user entered information before submitting it in an email including email addresses, subject, message or you will be just asking for trouble.

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. Click Site Administration > Advanced Features > Development > Debugging
  2. Set Debug messages to DEVELOPER: extra Moodle debug messages for developers
  3. Check the Display debug messages box.
  4. Check the Debug email sending box.
  5. Click Save changes at the bottom of the page.

Don't forget to go back and disable these settings when you are done debugging.