#!/usr/bin/perl # # REAN.pl - Retired Email Address Notifier # # Sends message from STDIO to all unread messages # Preferably run this script as a cron job # # VERSION: 0.2, written May 2016, updated July 2020 # AUTHOR: Philip K. # EMAIL: # LICENSE: CC0 use strict; use warnings; # modules use Net::IMAP::Simple; use Email::Sender::Simple qw(sendmail); use Email::MIME; # Data setup my ($imap_server, $imap_user, $imap_pass, $ignore_re) = @ARGV; die "usage: ./rean.pl [server] [user] [password]" unless $imap_user and $imap_pass and $imap_server; my $message = do { local $/; } or die "No message specified\n"; # IMAP setup my $imap = Net::IMAP::Simple->new($imap_server) or die "Unable to connect to $imap_server: $Net::IMAP::Simple::errstr\n"; $imap->login($imap_user, $imap_pass) or die "IMAP login error: ${$imap->errstr}\n"; foreach my $i (1..$imap->select("INBOX")) { unless ($imap->seen($i)){ my $mail = Email::Simple->new(join '', @{ $imap->top($i) }); print "Noticed ${$mail->header(\"Subject\")}\n"; if (($ignore_re =~ $mail->header("Subject")) || ($ignore_re =~ $mail->header("From"))) { continue ; } sendmail(Email::MIME->create (header_str => [ From => $mail->header("To"), To => $mail->header("From"), Subject => ("Retired email-address notification"), "X-Mailer" => "REAN v0.2" ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1' }, body_str => $message)); print "Replied to: ${$mail->header(\"To\")}\n"; } }