email_to_user function restrictions

email_to_user function restrictions

by Daniel Lozano -
Number of replies: 7

Hi everyone!

I'm trying to build a module where a student can ask for a tutorship to a specific professor and both can receive an email which says the date, time and reason of the tutorship.

To do it, i'm trying to use the function email_to_user($receiver, $sender, $subject, $body), which is one of the functions provided by Moodle.

What i am seeing, is that you can call this function to send an email only to the logged user, and if you want, using admin as sender.

But my idea is to send this email to both, student and professor. My problem is that the professor is not logged in that moment, and this function needs to take the user object of this professor from the database to include it in.

My question is, it is possible to take the object of the professor from the database, put it into a variable and include it in this function, or this function can only send the email to the user who is logged? And, if it is possible, how i must to do the sentence to take the professor object from the database?

I hope you understand my question.

Thanks for your help in advance!


I provide my code to be more clear:


$subject = get_string('Reserve confirmation');
$admin = get_admin();

if (!empty($_REQUEST['reserve'])) {
    echo 'saving...';
    $sid  = $USER->id;
    $tid  = $_POST['teacher'];
    $dat  = $_POST['date'];
    $hour = $_POST['time'];
    $mot  = $_POST['motive'];   
    $type = $_POST['type'];   

$body = get_string("\nReserve confirmed to: \n" .$dat. "\n\nMotive: \n" .$mot. "\n\nType: \n" .$type. "\n");

    email_to_user($USER,$admin,$subject,$body); //Here user is the user logged


Note: Here, when I use $USER, it is not necessary to take the info of the student from database because Moodle take it by default.

Average of ratings: -
In reply to Daniel Lozano

Re: email_to_user function restrictions

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It's not what the comments for the function say... the first parameter is $user and can be anybody you like (logged in our not). It wouldn't be much use otherwise....

so

$to = $DB->get_record('user', array('username'=>'professorxyz'));
$from = $DB->get_record('user', array('username'=>'senderabc'));
email_to_user($to, $from, ....... );


In reply to Howard Miller

Re: email_to_user function restrictions

by Daniel Lozano -

Thanks for your help!

I suppose that where you put 'user' in the get_record() function, you are referring to the table where is the professor, it is correct?

And, if I use this sentence, what i'm taking in $to, is the complete object? I mean, I can do echos like $to->email or $to->firstname?

With this last question, my doubt will be completely resolve!

I hope you could help me! Thanks a lot!

In reply to Daniel Lozano

Re: email_to_user function restrictions

by Daniel Lozano -

I'm already trying what you said to me, and I see the following:

"Table mdl_user doesn't exists"

But as you can see in this pictures, the table already exists.


¿Am I doing something wrong?


Thanks for this help!! =)

Attachment code.png
Attachment table.png
In reply to Daniel Lozano

Re: email_to_user function restrictions

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It's just 'user' not 'mdl_user'. The prefix is added for you automatically. 

Yes - this is pulling the complete user object for your selected users. That's all you need for the 'to' and 'from' users. 

In reply to Howard Miller

Re: email_to_user function restrictions

by Daniel Lozano -

Now YES!!! It is already done!!!


Thanks a lot!

You're response solve me days of work and doubts! =)

In reply to Daniel Lozano

Re: email_to_user function restrictions

by Matteo Scaramuccia -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Hi Daniel,
just a quick review of your code: do not use isset() but adopt the Moodle way to read the HTTP GET/POST values: optional_param(). It will help you in validating the expected data too.

HTH,
Matteo