Send email

Send email

by Frederic Nevers -
Number of replies: 2
Hello,

I'm trying to write a simple script that to send an email. I am trying to use the email_to_user() built-in function but am failing so far. Here is the code I am using:

$user = 'name@this_address_will_never_change.com';
$from = $USER->email;
$subject = 'Subject test';
$messagetext = 'This is just a test, please disregard it';
email_to_user($user, $from, $subject, $messagetext);

Could anyone please let me know what I am doing wrong?

I don't think I need to require_once moodlelib.php as it seems to be called by default on all Moodle pages where config.php is required. Am I correct in thinking this?

Any help with this would be much appreciated.

Cheers,
Fred
Average of ratings: -
In reply to Frederic Nevers

Re: Send email

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
$user has to be an object like:

$user = get_record('user', 'id', $id);

$from has the same requirement.

ammiccante
In reply to Daniele Cordella

Re: Send email

by Frederic Nevers -
Thanks a lot for your answer Daniele.

I have made the changes and it now works perfectly smile

Cheers,
Fred