Email Notification for each course enrollment

Re: Email Notification for each course enrollment

by Yogesh Gupta -
Number of replies: 30

I worked around it and there is a programming tweak for sending notification to users for course enrollment.

Create a custom function for send email

require_once("config.php");
require_once($CFG->libdir.'/moodlelib.php');

function custom_send_mail($to_email_id,$sender_email_id,$subject,$title,$body)
{
$mail = get_mailer();   // This will get the moodle mailer configuration.

$mail-> ....

$mail->send();

}

Now all you need to do is find when and where does admin enroll user to the course. I found it here

project_moodle/enrol/manual/ajax.php

Include function in this page

add a line before sending ouput via ajax. (In my case, I added it before echo json_encode($outcome);)

custom_send_mail($user->email,$USER->email,"Project_Moodle Notification","Enrollment Notification","You have been enrolled to course '".$course->fullname."'");

 

Note: I did this in moodle 2.6 with SMTP configured in moodle.

Average of ratings: Useful (1)
In reply to Yogesh Gupta

Re: Email Notification for each course enrollment

by Mauro Villoldo -

Hi there, I know that this is a old post, but I want to implement this very same functionality

When I manually enrol a user moodle should send him an email to let him know that he has been invited to take a course...

Im using Moodle 2.7 

where should I put the code, I mean in wich file and in wich part of the file...


require_once("config.php");

require_once($CFG->libdir.'/moodlelib.php');

function custom_send_mail($to_email_id,$sender_email_id,$subject,$title,$body)
{
$mail = get_mailer();   // This will get the moodle mailer configuration.

$mail-> ....

$mail->send();

}

I have found the last part and I have no problems with that 

thank you very much!


Mauro.



In reply to Mauro Villoldo

Re: Email Notification for each course enrollment

by Nishant Pandya -

Hi Mauro Villoldo, I can't find out where am I put this code. May you please help me where am I put this code, I need this same functionality.

Thanks,

Nishant

In reply to Nishant Pandya

Re: Email Notification for each course enrollment

by Mauro Villoldo -

Hi there Nishant Pandya, I'm sorry but I'm still unable to resolve this issue, we need someone with dev skills.

After hours of investigation and internet search, this code is the best solution I could find but I was unable to make it work.

If you find any other information please let me know.

I will let you know as well...

What I don't understand is why this functionality is not required by the rest of Moodlers or admins or teachers, I mean, how users or students know they have a pending course or that they have been invited to a course.


Thank you very much and I'm sorry I wasn't able to help.


Mauro Villoldo.


In reply to Mauro Villoldo

Re: Email Notification for each course enrollment

by Nishant Pandya -

Yes, same how users or students get notification that they are enrolled for any course.

Thanks Mauro Villoldo, thanks a lot for quick reply. 

Guys please help.

Thanks,

Nishant

In reply to Yogesh Gupta

Re: Email Notification for each course enrollment

by Mauro Villoldo -

Hi there Yogesh Gupta please can you clarify on how you manage to make this code to work.

I'm in charge of this implementation and this is the only missing piece of the puzzle.


I appreciate any help you can give me.

I'm using Moodle 2.7


Thank you very much


Mauro Villoldo.


In reply to Yogesh Gupta

Re: Email Notification for each course enrollment

by Nishant Pandya -

Please help Yogesh Gupta. Where am I put this code or please suggest if any other solution is available.

Thanks,

Nishant

In reply to Yogesh Gupta

Re: Email Notification for each course enrollment

by Nishant Pandya -

Hi Yogesh Gupta. Please help me where am I put this code. Means in which am I add this code. I have try to find but not able to find. Please help.

Thanks,

Nishant

In reply to Nishant Pandya

Re: Email Notification for each course enrollment

by Nidhi Tiwari -

Hi,

I have put the code in ajax,php. Can any one help me to complete this code :

function custom_send_mail($to_email_id,$sender_email_id,$subject,$title,$body)
{
$mail = get_mailer();   // This will get the moodle mailer configuration.

$mail-> .... (What should be there?????)

$mail->send();

}

Thanks in advance.
Average of ratings: Useful (1)
In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Nidhi Tiwari -

Hi Nishant,

I found the solution.


Add below code in /enrol/manual/ajax.php before echo json_encode($outcome);

require_once($CFG->libdir.'/moodlelib.php');

$body = "You have been enrolled to course"." ".$course->fullname."<br/><br/>"."Please login to start your course."."<br/><br/>"."Thanks,"."<br/>Admin";

email_to_user($user,$USER,'Enrollment Notification','The text of the message',$body);



Average of ratings: Useful (6)
In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Nishant Pandya -

Hey Nidhi Tiwari. Thanks. Thanks a ton. You have solved my issue. Really I am happy that someone who solved my issue.

And I feel that this is a bug on moodle & I think this code need to update on manual enrolment module.

Thanks,

Nishant

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Mauro Villoldo -

Hi Nidhi Tiwari !


The code you have provided worked as expected !!!


thank you very much!!!!


Case closed! :P


Thanks!!!!!

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by rabin sk -

Hi Nidhi,


Thanks a lot for this solution . smile

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Gaurav P -

Thanks Nidhi, for posting useful piece of code smile

In reply to Gaurav P

Re: Email Notification for each course enrollment

by Charlie Horwood -

Hi I have added this code to enrol/manual/ajax.php before the closing statement "echo json_encode($outcome);"

but when I manually enroll a user they don't receive an email notification.


Does anybody have any ideas?


This is what I added:

// Custom Manual Enrol Notification Test

require_once($CFG->libdir.'/moodlelib.php');

$body = "You have been enrolled to course"." ".$course->fullname."<br/><br/>"."Please login to start your course."."<br/><br/>"."Thanks,"."<br/>Admin";

email_to_user($user,$USER,'Enrollment Notification','The text of the message',$body);


Thanks in advance for any help


Charlie

In reply to Charlie Horwood

Re: Email Notification for each course enrollment

by Nidhi Tiwari -

Hi Charlie Horwood,


Please refer the attached file to have sequence of code.


Regards,

Nidhi

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Charlie Horwood -

Hi, Thanks for the post however it still doesn't seem to work.


I enrolled myself onto a course but didn't receive an email notification

Any ideas why this wouldn't do anything?


All the best.


Charlie

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by T K -
Hi Nidhi, THanks for sharing, it really works. I get email with the following content: 

 _______ 

 You have been enrolled to course EBRMS System Admin Walk-Through Please login to start your course. Thanks, Admin

 ________ 


 However for the complete info I would like to include the moodle site link, their user Id and password in the email, what parameters can I add into the email?

In reply to T K

Re: Email Notification for each course enrollment

by Nidhi Tiwari -

Hi Karishma,

You need to concatenate moodle site link ,user id ,password & what ever you need to add in body.

For site link you can use $CFG->wwwroot . For userid & passaword you can check $USer,$Page values. Just use print_object() ;exit; with these parameters to check the values. I am bit busy now a days & not getting time to debug this hope this will be helpful for you.

Example :

$body = "You have been enrolled to course"." ".$course->fullname."<br/><br/>"."Please login to start your course."."<br/><br/>"."Thanks,"."<br/>Admin"."<br/>$CFG->wwwroot;


In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Patrick Sauerwein -

Hi Nidhi,

i currently face the Problem that the Notification works without any Problems if I Enroll 1 by 1 User Manually but if I Manually enroll a whole Global Group to a Coruse it doesn´t send any Notification.

Do you have a Idea how to fix this Issue?

Thanks in advance!

Br


In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by rabin sk -

Hi Nidhi,

This code works perfectly when you assign a user to course but it does not when you assign a cohort to a course.


It would be great if you could help me on this,.


Thanks in Advance

In reply to rabin sk

Re: Email Notification for each course enrollment

by Sazia Arif -

Hi Rabin,

Did you got any solution for sending  email notification when assigning a cohort to a course with following steps.

Course->Participants->Settings -> Enrolled User-> Enroll Users -> Browse Cohorts (option button in pop up) -> Enroll button click

Course->Participants->Settings -> Enrollment Methods -> Add Method -> Cohort Sync -> Add method click.


In reply to Sazia Arif

Re: Email Notification for each course enrollment

by Rama chandran -

Have you got any solution for this issue?

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by T K -

Hi Nidhi,

THanks for sharing, it really works. 
I get email with the following content:


_______

You have been enrolled to course EBRMS System Admin Walk-Through

Please login to start your course.

Thanks,
Admin

________

However for the complete info I would like to include the moodle site link, their user Id and password in the email, what parameters can I add into the email?

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Vladislav K -

Hi, Nidhi!

Thank you for this code, but it sends me 2 messages instead of 1.

One of them is "The text of the message" and second with $body content.

My Moodle version is Moodle 3.1.2+ (Build: 20160923)

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by Rocio Forero -

I am using the same code but when registering the user it shows me the error "unexpected token a in json at position 0"

The moodle version is 3.1

In reply to Rocio Forero

Re: Email Notification for each course enrollment

by marisol castro -

The code works perfectly but how do you make the $courselink clickable in the email?  I have tried so many ways but I keep getting syntax errors.  Any ideas??

Here is the code I did to try making the link clickable.  There must be a period or quote missing somewhere.  Please help!

require_once($CFG->libdir.'/moodlelib.php');
global $CFG;

$courselink = $CFG->wwwroot . "/course/view.php?id=" . $course->id;
$url = $CFG->wwwroot . "/course/view.php?id=" . $course->id;
$body = "You have been enrolled to course"." ".$course->fullname."."<br/><br/>"."Please login to start your course."."<br/><br/>echo <a href =". $url ."></a><br/><br/>"."Thanks,"."<br/>Admin";
email_to_user($user,$USER,'Enrollment Notification','The text of the message',$body);
echo json_encode($outcome);

In reply to marisol castro

Re: Email Notification for each course enrollment

by Nidhi Tiwari -

Try this:

echo <a href =". $courselink ."></a>;

In reply to Nidhi Tiwari

Re: Email Notification for each course enrollment

by marisol castro -

That didn't work.  I still get a syntax error.  Am I missing a quote or comma or  semicolon somewhere?


require_once($CFG->libdir.'/moodlelib.php');
global $CFG;

$courselink = $CFG->wwwroot . "/course/view.php?id=" . $course->id;
$body = "You have been enrolled to course"." ".$course->fullname."."<br/><br/>"."Please login to start your course."."<br/><br/>echo<a href =".$courselink."></a><br/><br/>"."Thanks,"."<br/>Admin";
email_to_user($user,$USER,'Enrollment Notification','The text of the message',$body);
echo json_encode($outcome);

In reply to marisol castro

Re: Email Notification for each course enrollment

by Guido Hornig -
Picture of Plugin developers

try:

$body = "You have been enrolled to course. ".$course->fullname." <br/><br/> Please login to start your course. <br/><br/> <a href =\"$courselink\">click here to go to the course...</a><br/><br/>Thanks.<br/>Admin";

1)

In PHP $name will be substituted in double quotes.  Not in single quotes

$name ="Miller";

echo "Tom $name";

:> Tom Miller

echo 'Tom $name';

:> Tom $name

2)

all the HTML, like <br> is just string content like letters a...z

echo "<h1> text</h1>";

:>

text


but when you want to print a " (double quotes)  than you need a so called escape character before. the excape character is the backslash in PHP 

echo "\"";

:> "

echo "\";

Syntax error

echo ' this is a double quote " ' ;

:> this is a double quote "

(please rate me,  if it helps)

Guido

from lern.link