0

For reasons too complex to disclose here, a client of mine has requested that we develop a custom notifications script (installed under the Moodle root as a plain .php script) that runs on a cron and scans certain activities in Moodle (3.11) in order to send notifications to students (but only in one specific course) when other students are posting/participating in those other activities.

My complication comes in when trying to send emails in this notifications script using Moodle's internal mailing system, which I understand is a custom implementation of PHPMailer. The client wants the emails sent by this script to come through a different email address than the official one used by the rest of the Moodle site. So, while all regular Moodle emails (password resets, etc.) should come from "original@example.com", they want the custom emails in this script to come from "new@example.com" instead.

Example:

// prepare variables
$subject = "subject line";
$body = "some body text";
$toUser = $DB->get_record('user', array('email' => 'somestudent@example.com'));
$fromUser = $DB->get_record('user', array('email' => 'new@example.com'));

// TO DO: swap out Moodle SMTP settings - if possible?

// send email
$success = email_to_user($toUser, $fromUser, $subject, '', $body);

I am currently using the email_to_user() method in Moodle but, by changing the "from user" value (as shown above), can only change the "name" of the sender, not the email address it actually sends from. So, the result is an email with sender/header info that looks like "Custom User (via Moodle Site) original@example.com" rather than the default which is essentially "Moodle Site original@example.com".

What I actually want to appear is "Custom User new@example.com". To do this, I understand that I would need to swap out the SMTP settings prior to sending.

Is there a way to temporarily use different SMTP settings in Moodle, without overwriting the ones in the database?

If not, is there some other way or repurposing Moodle's existing PHPMailer to send emails using manual SMTP credentials - such as creating a new instance of PHPMailer using the same code packaged within Moodle by in which I can specify custom SMTP settings?

bwright
  • 347
  • 1
  • 15
  • 1
    I've no idea about Moodle, but you could find the path to Moodle's copy of the PHPMailer libraries, and load and use them yourself directly, just as in the PHPMailer examples, and possibly use other settings extracted from Moodle's own config. – Synchro Jul 28 '22 at 15:22
  • That's what I'm looking into now while hoping someone has a better answer. If I can figure out how to leverage PHPMailer directly, it should be a cakewalk from there. – bwright Jul 28 '22 at 17:15

1 Answers1

0

After digging through the files in Moodle's messaging and core libraries, as well as the PHPMailer docs, I came up with the following solution. This still piggybacks off of Moodle's internal mail system (PHPMailer), but is a much more direct way of creating a PHPMailer instance. Because this bypasses a lot of Moodle's additional functions, I was not able to call email_to_user() and this required manually configuring a lot of settings that Moodle would normally take care of. But, in the end, I am able to send email from the script under my Moodle webroot directory without having to install, configure, or maintain a separate mail sending library (i.e. a separate version of PHPMailer).

// create ne PHPMailer instance and configure as needed
$mail = get_mailer();
$mail->Sender = 'new@example.com';
$mail->Host = "smtp.host.com";
$mail->Port = "587";
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->setLanguage("en");
$mail->SetFrom("new@example.com","New Name");
$mail->Username = "new@example.com";
$mail->Password = "password";
$mail->AuthType = "Login";
$mail->IsHTML(true);

For each email that needs to go out, I then just call a few functions to update the required parameters.

// clear existing recipient list before forming new email
$mail->ClearAllRecipients();

// form new email by specifying only basic attributes
$mail->Subject = "Email Subject Line";
$mail->Body = "This is a new text body.";

// address directly to student's email
$mail->addAddress('recipient@example.com');

// send email
$success = $mail->send();
bwright
  • 347
  • 1
  • 15