5 Send-reminders - send email reminders for special occasions
9 Send emails reminders set by users for special occasions.
13 Email-reminder allows users to define events that they want to be
14 reminded of by email. Possible events include birthdays,
15 anniversaries and yearly events. Reminders can be sent on the day of
16 the event and a few days beforehand.
18 This script is meant to be invoked everyday by a cron job. It mails
19 the actual reminders out.
21 When run by the root user, it processes all of the spooled reminders.
22 When run by a specific user, it only processes reminders set by that
31 Displays basic usage message.
35 Does not actually send any emails out.
39 Prints out information about what the program is doing, including the
40 full emails being sent out.
44 Displays the version number.
50 F<~/.email-reminders>, F</etc/email-reminder.conf>
54 Francois Marier <francois@email-reminder.org.nz>
58 email-reminder-editor, collect-reminders
62 Copyright (C) 2004-2009 by Francois Marier
64 Email-Reminder is free software; you can redistribute it and/or
65 modify it under the terms of the GNU General Public License as
66 published by the Free Software Foundation; either version 3 of the
67 License, or (at your option) any later version.
69 Email-Reminder is distributed in the hope that it will be useful,
70 but WITHOUT ANY WARRANTY; without even the implied warranty of
71 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72 General Public License for more details.
74 You should have received a copy of the GNU General Public License
75 along with Email-Reminder; if not, write to the Free Software
76 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
87 use MIME
::QuotedPrint
;
90 use EmailReminder
::EventList
;
91 use EmailReminder
::Utils
;
92 use Date
::Manip
qw(ParseDate UnixDate);
95 my $PREFERENCE_FILE = '/etc/email-reminder.conf';
97 $preferences{"send_reminders"} = 1;
98 $preferences{"smtp_server"} = 'localhost';
99 $preferences{"smtp_ssl"} = 0;
100 $preferences{"smtp_username"} = '';
101 $preferences{"smtp_password"} = '';
102 $preferences{"mail_from"} = 'root@localhost';
109 # Command-line parameters
114 GetOptions
( "verbose" => \
$verbose,
115 "simulate" => \
$simulate,
116 "version" => \
$version,
120 # Override preferences with system values
123 print "Reading preferences from '$PREFERENCE_FILE'\n" if $verbose;
125 if (open my $config_fh, '<', $PREFERENCE_FILE) {
126 # Stolen off of the Cookbook (section 8.16)
127 while (<$config_fh>) {
129 s/#.*//; # no comments
130 s/^\s+//; # no leading white
131 s/\s+$//; # no trailing white
132 next unless length; # anything left?
133 my ($var, $value) = split(/\s*=\s*/, $_, 2);
134 $preferences{$var} = $value;
139 print "Warning: cannot read configuration file at $PREFERENCE_FILE.\nMake sure that the user running $0 has read permissions on that configuration file.\n";
149 my $user_name = shift;
150 my $user_email = shift;
152 unless ($user_email) {
156 my $to = $user_email;
157 $to = "$user_name <$user_email>" if ($user_name);
159 print "--> Emailing '$to':\n".encode
("UTF-8", $subject."\n\n".$message) if $verbose;
162 my $smtp_server = '';
163 if ($preferences{"smtp_server"} =~ /^([A-Za-z_0-9\-\/.]+)$/) {
167 if ($preferences{"smtp_ssl"}) {
168 eval 'use Net::SMTP::SSL';
169 die "Couldn't load module : $!" if ($@
);
170 $smtp = Net
::SMTP
::SSL
->new($smtp_server, Port
=> 465, Debug
=> 0);
174 $smtp = Net
::SMTP
->new($smtp_server, Debug
=> 0);
176 die "Error: couldn't connect to server '$smtp_server'\n" unless $smtp;
178 # SMTP SASL authentication (if necessary)
179 if ($preferences{"smtp_username"} and $preferences{"smtp_password"}) {
180 unless ($smtp->auth($preferences{"smtp_username"}, $preferences{"smtp_password"})) {
181 die "Error: authentication with the SMTP server failed with error code ".$smtp->status."\n";
185 unless ($smtp->mail($preferences{"mail_from"})) {
186 die "Error: the sending address was not accepted. Try setting the 'mail_from' variable to a valid email address in the configuration file\n";
190 $ok = $ok && $smtp->to($to);
191 $ok = $ok && $smtp->data();
192 $ok = $ok && $smtp->datasend("From: Email-Reminder <" . $preferences{"mail_from"} . ">\n");
194 # Create an RFC822 compliant date (current time)
195 my $rfc822_format = "%a, %d %b %Y %H:%M %z";
196 my $today = ParseDate
("Now");
197 my $rfc822_date = UnixDate
($today,$rfc822_format);
198 $ok = $ok && $smtp->datasend("Date: $rfc822_date\n");
200 $ok = $ok && $smtp->datasend("To: $to\n");
201 $ok = $ok && $smtp->datasend("Subject: =?utf-8?B?".encode_base64
(encode
("UTF-8", $subject), '')."?=\n");
202 $ok = $ok && $smtp->datasend("Mime-Version: 1.0\n");
203 $ok = $ok && $smtp->datasend("Content-Type: text/plain; charset=utf-8\n");
204 $ok = $ok && $smtp->datasend("Content-Disposition: inline\n");
205 $ok = $ok && $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
206 $ok = $ok && $smtp->datasend("\n");
207 $ok = $ok && $smtp->datasend(encode_qp
(encode
("UTF-8", $message)));
208 $ok = $ok && $smtp->dataend();
212 die "Error: could not mail the reminder out\n" unless $ok;
218 sub send_author_wishes
220 my $user_name = shift;
221 my $user_email = shift;
223 print "--> Processing event Email-Reminder Author's Birthday\n" if $verbose;
225 my $today = ParseDate
('now');
226 my $current_month = UnixDate
($today, '%m');
227 my $current_day = UnixDate
($today, '%d');
229 if (1 == $current_month and 30 == $current_day) {
230 print "--> Event Email-Reminder Author's Birthday is occurring\n" if $verbose;
232 my $recipient_name = 'Francois Marier';
233 my $recipient_email = 'francois@email-reminder.org.nz';
234 my $subject = 'Happy birthday from an email-reminder user';
235 my $message = <<"MESSAGEEND";
238 Happy birthday and thank you for email-reminder!
244 Sent by Email-Reminder $EmailReminder::Utils::VERSION
245 http://www.email-reminder.org.nz/
248 if (!send_email
($message, $subject, $recipient_name, $recipient_email)) {
260 print "==> Processing $file\n" if $verbose;
262 my $list = EmailReminder
::EventList
->new($file);
264 my @fullname = $list->get_user_name();
265 my $user_fname = $fullname[0];
266 my $user_lname = $fullname[1];
267 my $user_name = $user_fname;
268 $user_name .= " " . $user_lname if defined($user_lname);
269 my $user_email = $list->get_user_email();
271 foreach my $event ($list->get_events()) {
272 print '--> Processing event '.$event->get_name()."\n" if $verbose;
274 if ($event->is_occurring()) {
275 print '--> Event '.$event->get_name()." is occurring\n" if $verbose;
278 if (!process_event
($event, $user_name, $user_email)) {
283 print '!!! Error while sending reminder for '.$event->get_name()."\n" if $verbose;
285 my $msg = 'WARNING: Due to an error, the email reminder for Event "' . $event->get_name() .
286 '" cannot be processed and all I could do was to let you know that there was a problem.';
287 $msg .= "\n\n".'Since this event is OCCURRING TODAY, you should really check your reminders manually.';
288 $msg .= "\n\n".'Please forward this email to the email-reminder author so that this problem can be fixed in future versions:';
289 $msg .= "\n\n".' Francois Marier <francois@email-reminder.org.nz>';
290 $msg .= "\n\n".'Thanks!';
291 $msg .= "\n\n--------------------------------------------------------------";
292 $msg .= EmailReminder
::Utils
::debug_info
($event, 2);
293 my $subject = 'Email-reminder ERROR: ' . $event->get_name();
295 if (!send_email
($msg, $subject, $user_name, $user_email)) {
302 if ($list->get_author_wishes()) {
303 send_author_wishes
($user_name, $user_email);
309 # Send reminders for an event which is occurring
313 my $user_name = shift;
314 my $user_email = shift;
316 my $subject = $event->get_subject();
318 my @recipients = @
{$event->get_recipients()};
319 if ($#recipients > -1) {
320 foreach my $recipient (@recipients) {
321 my $recipient_email = shift @
{$recipient};
322 my $recipient_fname = shift @
{$recipient};
323 my $recipient_lname = shift @
{$recipient};
325 my $recipient_name = $recipient_fname;
326 $recipient_name .= " $recipient_lname" if defined($recipient_lname);
328 my $msg = $event->get_message($recipient_fname);
329 if ($msg && !send_email
($msg, $subject, $recipient_name, $recipient_email)) {
334 my $msg = $event->get_message($user_fname);
335 if ($msg && !send_email
($msg, $subject, $user_name, $user_email)) {
343 my $running_uid = $>;
344 if (0 == $running_uid) {
345 print STDERR
"Warning: for security reasons, this script should not be not as root.\n";
348 my $spool_dir = $EmailReminder::Utils
::SPOOL_DIRECTORY
;
350 # Iterate through all spooled files
351 while (defined(my $file = glob("$spool_dir/*"))) {
353 if ($file =~ /^([A-Za-z_0-9\-\/]+)$/) {
356 print STDERR
"Skipped unclean filename" if $verbose;
360 unless (process_file
($file, 0, 1)) {
364 # Delete the file once we're done with it
365 unless (unlink($file)) {
366 print STDERR
"Could not remove $file.\n" if $verbose;
371 # Normal users only get to test their own reminders
372 my @pwinfo = getpwuid($>);
373 my $homedir = $pwinfo[7];
374 my $file = "$homedir/" . $EmailReminder::Utils
::USER_CONFIG_FILE
;
377 return process_file
($file, 0, 1);
379 print STDERR
"Warning: could not find your .email-reminders file.\n";
385 if ($help || $version) {
386 print "send-reminders $EmailReminder::Utils::VERSION\n";
391 } elsif ($preferences{"send_reminders"}) {
393 print STDERR
"Could not send reminders.\n";