Auto Email Reminder - Certificate is going to expire soon! Code Inside.

Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Dustin Elliott -
Number of replies: 10
Hello,

I have been working on some code to check the database for certificates that are nearing 2 years old and then send an email to the students letting them know that their certificate is about to expire.

My certificates are valid for 2 years, then the student has to log back in and and the class again to renew it. I want to have an email sent out 90 day and 30 day before the certificate expires reminding the student.

I have accomplished this using a custom cron job that runs the code below. I have also added to fields to the certificate_issues table so that I can keep track of when a reminder email is sent. I called the new fields "reminder1" and "reminder2".

The code below will check for certificates that have a "certdate" that is 21 months old or older and then send out an email to those people. I have removed some names and images for privacy reasons.

I'm very new to PHP and SQL so if anyone has any suggestions on how to improve the code please let me know!

START OF CODE

$firstreminder = "SELECT tbl_certificate_issues.id , tbl_certificate_issues.userid , tbl_certificate_issues.studentname , tbl_certificate_issues.classname , tbl_certificate_issues.certdate, tbl_user.email, tbl_user_info_data.data
FROM tbl_certificate_issues, tbl_user, tbl_user_info_data
WHERE DATE_ADD( FROM_UNIXTIME( `certdate` ) , INTERVAL 21
MONTH ) < DATE_FORMAT( NOW() , GET_FORMAT( DATETIME, 'ISO' ) )
AND `reminder1` = '0'
AND tbl_certificate_issues.userid=tbl_user.id
AND tbl_user.id=tbl_user_info_data.userid";

//Run query and send email for certifications that expire in 90 days or less
$r = mysql_query($firstreminder);

for ($count=0; $count <= mysql_numrows($r)-1; $count++) {

$certid = mysql_result($r,$count,"id");
$username = mysql_result($r,$count,"studentname");
$certname = mysql_result($r,$count,"classname");
$certdateunix = mysql_result($r,$count, "certdate");
$certnumber = mysql_result($r,$count,"data");

$certdate = strftime("%B %d %Y", $certdateunix);
//Calculate experation date by adding 2 years to certdate
$expdate = str_replace(' 0', ' ', strftime('%B %d, %Y',(strtotime("+2 years", $certdateunix))));

$to = "email@www.com";
$subject = "Your Certification Expires in 90 Days or Less!";

$message = "

<style type=\"text/css\">
<!--
.p_main {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
margin-left: 12px;
margin-right: 12px;
}
#email_border {
border: 2px solid #057CB1;
position: relative;
height: auto;
width: 760px;
margin-right: auto;
margin-left: auto;
}
.blue {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: 700;
color: #0000FF;
}
-->
</style>
<body>
<div id=\"email_border\">
<p><a href=\"http://www.com\"><img src=\"\" alt=\"Site\" width=\"750\" height=\"133\" border=\"0\" /></a></p>
<p class=\"p_main\">Hello $username,</p>
<p class=\"p_main\">You are receiving this email because our records indicate that your <strong>$certname</strong> certification expires in the next 90 days.</p>
<p class=\"p_main\"><strong>Certification Date:</strong> $certdate</p>
<p class=\"p_main\"><strong>Expiration Date:</strong> $expdate</p>
<p class=\"p_main\">Please login to <a href=\"http://www.com\">Site</a> at http://www.com to sign-up for a class and renew your certification. It is important to keep your certification current and learn of changes that have been made since you received your certification almost 2 years ago!</p>
<p class=\"p_main\">If you have any questions or concerns about your certification or any FFS product please contact us toll free at 1-800-555-5555 or +1-555-555-5555.</p>
<p class=\"p_main\"><strong>Your certification number is:</strong><span class=\"blue\"> $certnumber</span></p>
<p class=\"p_main\">Thank you for your support of our products!</p>
<p class=\"p_main\"><strong>Technical Service</strong><br />
http://www.com<br />
Toll Free 1-555-555-5555<br />
International +1-555-555-5555</p>
</div>
</body>
</html>";

$headers = 'From: noreply@www.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1rn' .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

//Put time stamp in reminder1 field to mark that a 90 day reminder was sent and when.
execute_sql("UPDATE {$CFG->prefix}certificate_issues
SET reminder1 = UNIX_TIMESTAMP(NOW())
WHERE id = $certid");
}
Average of ratings: -
In reply to Dustin Elliott

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Paul McCullough -

hi Dustin - this concept looks great.  I don't have a clue about PHP or programming, but need to have an expiry for my certificates - pretty much like this one.  Did you develop this into a module or plugin?  Do you have a step by step approach to implementing this?

Best wishes

Paul.

In reply to Paul McCullough

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Kate Rhodes -

Hey! I just stumbled onto this post while searching for something else! That's an awesome idea, one which I can use for sure. I have the same question as @Paul though... how did you come up with the expiry date to begin with?

 

I know this is an old thread but I'm hoping someone is still out there!

Cheers! Kate

In reply to Kate Rhodes

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Claire Feirn -

Hi 

I have just found this as well - did anyone get this working?

Cheers

Claire

In reply to Claire Feirn

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

After a quick glance of the code it appears that what is needed is to manually edit the certificate_issues table by adding in two fields, which are flags that show whether the message has been sent or not, 1 meaning yes, 0 no.

These fields aren't necessary as you could use the last time the cron was run, and check to see if the last time was within the time range when an email would have been sent out. The query also should use the moodle database functionality for cross database support, eg. $DB->get_record, $DB->update_record etc.

The code would be put into the function certificate_cron function in mod/certificate/lib.php - which is called everytime the Moodle cron is run.

I could re-write the code in the initial post, but atm any spare time I have for coding is put into the redevelopment of the certificate module. If there is a large demand I can divert some time and write it. I wouldn't put it into the core certificate code though as I don't like the idea of a static time limit. Would be better to add a setting in the certificate that specified whether an email is sent out, and if so, what the expiry time is and currently that would be an additional feature that is a possibility after I have deployed the new code.

Regards,

Mark

In reply to Mark Nelson

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Richard Bakos -

Hello Mark,

You mean like this:

function certificate_cron () {
     code goes here
}

?

Additionally... I agree with what you are saying about the unnecessary fields added to the database table. I simply want to check if the certificate is going to expire in ten days and if so, send them a email. When the cron is run again those users won't meet that criteria (10 days until expiration) so they won't get another email. What in the original script would need to be altered to do this?

Thanks!

In reply to Richard Bakos

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Richard,

That's correct. That is the place I would put the code to send out the emails.

Regards,

Mark

In reply to Mark Nelson

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Richard Bakos -

Also, the value for $to is "email@www.com"... Isn't the students email address supposed to be dynamically filled into this value?

In reply to Mark Nelson

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by Mike Walters -

Hi All,

I was just reading through this thread and wondering if there has been any more development/plans made for this functionality to be added to the certificate module?

It's a feature that I have been asked about on more than one occasion and I simply don't have the coding knowledge to apply it myself.

Hoping that it is still on the cards!

Cheers,

Mike

In reply to Mark Nelson

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by mohamed el amine tamtam -

Hi Mark

Please , if you could share the CODE with us we will appreciate that,

because I'm not good in php and I was working on the same project

with best regards,

Amine .

 

In reply to Dustin Elliott

Re: Auto Email Reminder - Certificate is going to expire soon! Code Inside.

by tim eber -

This might help solve my reset problem....but how did you get your certificates to expire in the first place? Does this expiration remove the old certificate from their records and force them to re-take the course? 


Thanks!