Freshmeat.net is now known as Freecode.com
[email-reminder.git] / send-reminders
blob9a8a1e0312303b25839b4b39e9437c02d4819c87
1 #!/usr/bin/perl -T
3 =head1 NAME
5 Send-reminders - send email reminders for special occasions
7 =head1 SYNOPSIS
9 Send emails reminders set by users for special occasions.
11 =head1 DESCRIPTION
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
23 user.
25 =head1 OPTIONS
27 =over 6
29 =item B<--help>
31 Displays basic usage message.
33 =item B<--simulate>
35 Does not actually send any emails out.
37 =item B<--verbose>
39 Prints out information about what the program is doing, including the
40 full emails being sent out.
42 =item B<--version>
44 Displays the version number.
46 =back
48 =head1 FILES
50 F<~/.email-reminders>, F</etc/email-reminder.conf>
52 =head1 AUTHOR
54 Francois Marier <francois@fmarier.org>
56 =head1 SEE ALSO
58 email-reminder-editor, collect-reminders
60 =head1 COPYRIGHT
62 Copyright (C) 2004-2014 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
77 02110-1301, USA.
79 =cut
81 use strict;
82 use warnings;
84 use Encode;
85 use Getopt::Long;
86 use MIME::Base64;
87 use MIME::QuotedPrint;
88 use Pod::Usage;
90 use EmailReminder::EventList;
91 use EmailReminder::Utils;
92 use Date::Manip qw(ParseDate UnixDate);
94 # Default preferences
95 my $PREFERENCE_FILE = '/etc/email-reminder.conf';
96 my %preferences;
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';
103 read_config();
105 # Global variables
106 my $user_fname;
107 my $user_lname;
109 # Command-line parameters
110 my $verbose = 0;
111 my $simulate = 0;
112 my $version = 0;
113 my $help = 0;
114 GetOptions( "verbose" => \$verbose,
115 "simulate" => \$simulate,
116 "version" => \$version,
117 "help" => \$help,
120 # Override preferences with system values
121 sub read_config
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>) {
128 chomp; # no newline
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 $value = 1 if $value eq 'true' or $value eq 'yes';
135 $value = 0 if $value eq 'false' or $value eq 'no';
136 $preferences{$var} = $value;
138 close $config_fh;
140 else {
141 print "Warning: cannot read configuration file at $PREFERENCE_FILE.\nMake sure that the user running $0 has read permissions on that configuration file.\n";
142 return;
144 return 1;
147 sub send_email
149 my $message = shift;
150 my $subject = shift;
151 my $user_name = shift;
152 my $user_email = shift;
154 unless ($user_email) {
155 return 0;
158 my $to = $user_email;
159 $to = "$user_name <$user_email>" if ($user_name);
161 print "--> Emailing '$to':\n".encode("UTF-8", $subject."\n\n".$message) if $verbose;
163 unless ($simulate) {
164 my $smtp_server = '';
165 if ($preferences{"smtp_server"} =~ /^([A-Za-z_0-9\-\/.]+)$/) {
166 $smtp_server = $1;
168 my $smtp = undef;
169 if ($preferences{"smtp_ssl"}) {
170 eval 'use Net::SMTP::SSL';
171 die "Couldn't load module : $!" if ($@);
172 $smtp = Net::SMTP::SSL->new($smtp_server, Port => 465, Debug => 0);
174 else {
175 use Net::SMTP;
176 $smtp = Net::SMTP->new($smtp_server, Debug => 0);
178 die "Error: couldn't connect to server '$smtp_server'\n" unless $smtp;
180 # SMTP SASL authentication (if necessary)
181 if ($preferences{"smtp_username"} and $preferences{"smtp_password"}) {
182 unless ($smtp->auth($preferences{"smtp_username"}, $preferences{"smtp_password"})) {
183 die "Error: authentication with the SMTP server failed with error code ".$smtp->status."\n";
187 unless ($smtp->mail($preferences{"mail_from"})) {
188 die "Error: the sending address was not accepted. Try setting the 'mail_from' variable to a valid email address in the configuration file\n";
191 my $ok = 1;
192 $ok = $ok && $smtp->to($to);
193 $ok = $ok && $smtp->data();
194 $ok = $ok && $smtp->datasend("From: Email-Reminder <" . $preferences{"mail_from"} . ">\n");
196 # Create an RFC822 compliant date (current time)
197 my $rfc822_format = "%a, %d %b %Y %H:%M %z";
198 my $today = ParseDate("Now");
199 my $rfc822_date = UnixDate($today,$rfc822_format);
200 $ok = $ok && $smtp->datasend("Date: $rfc822_date\n");
202 $ok = $ok && $smtp->datasend("To: $to\n");
203 $ok = $ok && $smtp->datasend("Subject: =?utf-8?B?".encode_base64(encode("UTF-8", $subject), '')."?=\n");
204 $ok = $ok && $smtp->datasend("Mime-Version: 1.0\n");
205 $ok = $ok && $smtp->datasend("Content-Type: text/plain; charset=utf-8\n");
206 $ok = $ok && $smtp->datasend("Content-Disposition: inline\n");
207 $ok = $ok && $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
208 $ok = $ok && $smtp->datasend("\n");
209 $ok = $ok && $smtp->datasend(encode_qp(encode("UTF-8", $message)));
210 $ok = $ok && $smtp->dataend();
212 $smtp->quit();
214 die "Error: could not mail the reminder out\n" unless $ok;
217 return 1;
220 sub send_author_wishes
222 my $user_name = shift;
223 my $user_email = shift;
225 print "--> Processing event Email-Reminder Author's Birthday\n" if $verbose;
227 my $today = ParseDate('now');
228 my $current_month = UnixDate($today, '%m');
229 my $current_day = UnixDate($today, '%d');
231 if (1 == $current_month and 30 == $current_day) {
232 print "--> Event Email-Reminder Author's Birthday is occurring\n" if $verbose;
234 my $recipient_name = 'Francois Marier';
235 my $recipient_email = 'francois@fmarier.org';
236 my $subject = 'Happy birthday from an email-reminder user';
237 my $message = <<"MESSAGEEND";
238 Hi Francois,
240 Happy birthday and thank you for email-reminder!
242 $user_name
243 $user_email
246 Sent by Email-Reminder $EmailReminder::Utils::VERSION
247 https://launchpad.net/email-reminder
248 MESSAGEEND
250 if (!send_email($message, $subject, $recipient_name, $recipient_email)) {
251 return;
255 return 1;
258 sub process_file
260 my $file = shift;
262 print "==> Processing $file\n" if $verbose;
264 my $list = EmailReminder::EventList->new($file);
266 my @fullname = $list->get_user_name();
267 my $user_fname = $fullname[0];
268 my $user_lname = $fullname[1];
269 my $user_name = $user_fname;
270 $user_name .= " " . $user_lname if defined($user_lname);
271 my $user_email = $list->get_user_email();
273 foreach my $event ($list->get_events()) {
274 print '--> Processing event '.$event->get_name()."\n" if $verbose;
276 if ($event->is_occurring()) {
277 print '--> Event '.$event->get_name()." is occurring\n" if $verbose;
279 eval {
280 if (!process_event($event, $user_name, $user_email)) {
281 return;
284 if($@) {
285 print '!!! Error while sending reminder for '.$event->get_name()."\n" if $verbose;
287 my $msg = 'WARNING: Due to an error, the email reminder for Event "' . $event->get_name() .
288 '" cannot be processed and all I could do was to let you know that there was a problem.';
289 $msg .= "\n\n".'Since this event is OCCURRING TODAY, you should really check your reminders manually.';
290 $msg .= "\n\n".'Please forward this email to the email-reminder author so that this problem can be fixed in future versions:';
291 $msg .= "\n\n".' Francois Marier <francois@fmarier.org>';
292 $msg .= "\n\n".'Thanks!';
293 $msg .= "\n\n--------------------------------------------------------------";
294 $msg .= EmailReminder::Utils::debug_info($event, 2);
295 my $subject = 'Email-reminder ERROR: ' . $event->get_name();
297 if (!send_email($msg, $subject, $user_name, $user_email)) {
298 return;
304 if ($list->get_author_wishes()) {
305 send_author_wishes($user_name, $user_email);
308 return 1;
311 # Send reminders for an event which is occurring
312 sub process_event
314 my $event = shift;
315 my $user_name = shift;
316 my $user_email = shift;
318 my $subject = $event->get_subject();
320 my @recipients = @{$event->get_recipients()};
321 if ($#recipients > -1) {
322 foreach my $recipient (@recipients) {
323 my $recipient_email = shift @{$recipient};
324 my $recipient_fname = shift @{$recipient};
325 my $recipient_lname = shift @{$recipient};
327 my $recipient_name = $recipient_fname;
328 $recipient_name .= " $recipient_lname" if defined($recipient_lname);
330 my $msg = $event->get_message($recipient_fname);
331 if ($msg && !send_email($msg, $subject, $recipient_name, $recipient_email)) {
332 return;
335 } else {
336 my $msg = $event->get_message($user_fname);
337 if ($msg && !send_email($msg, $subject, $user_name, $user_email)) {
338 return;
343 sub main
345 my $running_uid = $>;
346 if (0 == $running_uid) {
347 print STDERR "Warning: for security reasons, this script should not be not as root.\n";
350 my $spool_dir = $EmailReminder::Utils::SPOOL_DIRECTORY;
351 if (-w $spool_dir) {
352 # Iterate through all spooled files
353 while (defined(my $file = glob("$spool_dir/*"))) {
354 # Untaint filename
355 if ($file =~ /^([A-Za-z_0-9\-\/]+)$/) {
356 $file = $1;
357 } else {
358 print STDERR "Skipped unclean filename" if $verbose;
359 next;
362 unless (process_file($file, 0, 1)) {
363 return;
366 # Delete the file once we're done with it
367 unless (unlink($file)) {
368 print STDERR "Could not remove $file.\n" if $verbose;
371 return 1;
372 } else {
373 # Normal users only get to test their own reminders
374 my @pwinfo = getpwuid($>);
375 my $homedir = $pwinfo[7];
376 my $file = "$homedir/" . $EmailReminder::Utils::USER_CONFIG_FILE;
378 if (-e $file) {
379 return process_file($file, 0, 1);
380 } else {
381 print STDERR "Warning: could not find your .email-reminders file.\n";
382 return;
387 if ($help || $version) {
388 print "send-reminders $EmailReminder::Utils::VERSION\n";
389 if ($help) {
390 print "\n";
391 pod2usage(1);
393 } elsif ($preferences{"send_reminders"}) {
394 unless (main()) {
395 print STDERR "Could not send reminders.\n";