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?