-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailAbooks.pl
executable file
·134 lines (112 loc) · 4.16 KB
/
mailAbooks.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use strict;
use warnings;
use lib '/usr/local/cpanel';
use Time::Piece;
use POSIX qw(strftime);
use Cpanel::Email::Send;
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure qw(gnu_getopt);
use Sys::Hostname;
#Global Vars
my $sourceDir;
my $help;
my $emailTemplate;
my $host = hostname;
my $logFile = "/root/cpanel-squirrelmail-export-mail.log";
my $silent = 0;
GetOptions(
'source=s' => \$sourceDir,
'help' => \$help,
) or die "Use --help";
if ($help) {
(my $helpText = qq{
This script will find all files that end with .abook.csv in the source directory.
It will then mail all of those files to the email address that is contained within the filename.
This script is designed to work exclusively with the files generated by the exportAbooks.pl script.
--source /path/to/source/directory
The source directory should be one of the export directories created by the exportAbooks.pl script.
}) =~ s/^ {4}//mg;
print $helpText;
exit;
}
($emailTemplate = qq{
Hello,
Your SquirrelMail address book has been exported and attached to this email.
You may import the attached file to RoundCube and Horde with the following instructions:
First, download the attached CSV file to your computer.
RoundCube:
- Login to the RoundCube webmail client
- Click on the Contacts button in the upper right corner
- Click on the Import button on the upper left corner
- Upload the csv file from your computer
Horde:
- Login to the Horde webmail client
- Click on Address Book in the horizontal menu
- Click on Import/Export in the upper left area
- Upload the csv file from your computer
}) =~ s/^ {4}//mg;
if (-d $sourceDir) {
chdir $sourceDir;
my @files = glob("*.abook.csv");
if (scalar @files < 1 ) { message ("ERROR: Source directory does not contain any files ending in .abook.csv . No addressbooks were mailed.", 1, 1, 1) }
foreach my $file (@files) {
my $count = `wc -l < $file`;
if ($count < 1) { message ("NOTICE: $file appears to be empty so it has been skipped and not mailed.", 1, 1, 0); next; }
open( my $fh, "<", $file) or die "Can't open < $file: $!";
my $file_content = do { local $/; $fh};
if ($count == 1 and $file_content =~ "Nickname" ) { message ("NOTICE: $file appears to only contain header information so it has been skipped and not mailed.", 1, 1, 0); next; }
my %attachment;
$attachment{'content'} = $fh;
$attachment{'name'} = $file;
my @attachments = (\%attachment);
my @to = ($file);
$to[0] =~ s/.abook.csv//ig;
sendMessage(\@to, "Your SquirrelMail AddressBook", $emailTemplate, "root\@$host", \@attachments);
message ("INFO: $file file has been mailed to $to[0] .", 1, 1, 0);
close $fh;
}
message ("INFO: Mailing has concluded. Check /var/log/exim_mainlog if you have any doubts or concerns about the messages that were sent.", 1, 1, 0);
message ("INFO: You may view the above output at the following log $logFile", 1, 1, 0);
} else {
die "--source is not a directory. Use --help\n";
}
sub sendMessage {
my %opts;
$opts{'to'} = $_[0];
$opts{'subject'} = $_[1];
$opts{'text_body'} = \$_[2];
$opts{'from'} = $_[3];
$opts{'attachments'} = $_[4];
Cpanel::Email::Send::email_message( \%opts );
return;
}
sub writeLog {
my $message = $_[0];
my $timeStamp = strftime "%D %T", localtime;
open (my $fh, '>>', $logFile) or die "Could not open $logFile for writing";
print $fh "$timeStamp $message";
close $fh;
return;
}
sub message {
my $message = $_[0];
my $stdOut = $_[1];
my $log = $_[1];
my $die = $_[3];
chomp($message);
$message = "$message\n";
if ($log) { writeLog($message) }
if ($die and not $silent) {
die $message;
} elsif ($die and $silent and $log) {
writeLog ($message);
exit;
} elsif ($die and $silent and not $log) {
# Even if the original intention was to not log, we're going to override this so that the script never dies without saying *something*
writeLog($message);
exit;
}
if ($stdOut and not $silent) { print $message }
return;
}
1;