1 #################################################################################
2 # Xestia Scanner Server - Email Export Module. #
3 # Sends the page as an email. #
5 # Copyright (C) 2011 Steve Brokenshire <sbrokenshire@xestia.co.uk> #
7 # This module is licensed under the same license as Xestia Scanner Server which #
8 # is licensed under the GPL version 3. #
10 # This program is free software: you can redistribute it and/or modify #
11 # it under the terms of the GNU General Public License as published by #
12 # the Free Software Foundation, version 3 of the License. #
14 # This program is distributed in the hope that it will be useful, #
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
17 # GNU General Public License for more details. #
19 # You should have received a copy of the GNU General Public License #
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
21 #################################################################################
23 # Define the package (perl module) name.
25 package Modules::Export::Email;
27 # Enable strict and use warnings.
31 use Encode qw(decode_utf8);
33 use Modules::System::Common;
34 use MIME::Base64 3.13 qw(encode_base64);
36 use Net::Domain qw(hostfqdn);
41 # Set the following values.
43 our $VERSION = "0.1.0";
46 my $error_message = "";
47 my $language_name = "";
51 #################################################################################
52 # new: Create an instance of Modules::Output::Normal #
56 # $dbmodule = Modules::Output::Normal->new(); #
57 #################################################################################
59 # Get the perl module name.
64 return bless($self, $class);
69 #################################################################################
70 # initialise: Initialises the output module. #
74 # $outputmodule->initialise(); #
75 #################################################################################
80 #################################################################################
81 # loadsettings: Loads some settings for the output module. #
85 # $outputmodule->loadsettings(language); #
87 # language Specifies the language to use. #
88 #################################################################################
91 my $passed_lang = shift;
93 $language_name = $passed_lang;
98 #################################################################################
99 # getoptions: Gets the options that will be used. #
103 # %options = $outputmodule->getoptions(); #
104 #################################################################################
106 my (%options, $options);
107 tie(%options, "Tie::IxHash");
109 $options{servername}{type} = "textbox";
110 $options{servername}{string} = languagestrings("servername");
111 $options{servername}{maxlength} = "256";
112 $options{servername}{size} = "64";
114 $options{serverport}{type} = "textbox";
115 $options{serverport}{string} = languagestrings("serverport");
116 $options{serverport}{value} = "25";
117 $options{serverport}{maxlength} = "5";
118 $options{serverport}{size} = "5";
120 #$options{smtpssl}{type} = "checkbox";
121 #$options{smtpssl}{string} = "Enable SSL/TLS";
122 #$options{smtpssl}{checked} = 1;
124 $options{senderaddress}{type} = "textbox";
125 $options{senderaddress}{string} = languagestrings("senderaddress");
126 $options{senderaddress}{maxlength} = "256";
127 $options{senderaddress}{size} = "64";
129 $options{emailaddress}{type} = "textbox";
130 $options{emailaddress}{string} = languagestrings("emailaddress");
131 $options{emailaddress}{maxlength} = "256";
132 $options{emailaddress}{size} = "64";
134 $options{username}{type} = "textbox";
135 $options{username}{string} = languagestrings("username");
136 $options{username}{maxlength} = "256";
137 $options{username}{size} = "32";
139 $options{password}{type} = "textbox";
140 $options{password}{string} = languagestrings("password");
141 $options{password}{maxlength} = "256";
142 $options{password}{password} = 1;
143 $options{password}{size} = "32";
145 $options{filename}{type} = "textbox";
146 $options{filename}{string} = languagestrings("filename");
147 $options{filename}{maxlength} = "256";
148 $options{filename}{size} = "64";
150 $options{subjectline}{type} = "textbox";
151 $options{subjectline}{string} = languagestrings("subjectline");
152 $options{subjectline}{maxlength} = "256";
153 $options{subjectline}{value} = languagestrings("scannedimage");
154 $options{subjectline}{size} = "64";
156 $options{personalmessage}{type} = "textbox";
157 $options{personalmessage}{string} = languagestrings("personalmessage");
158 $options{personalmessage}{maxlength} = "1024";
159 $options{personalmessage}{size} = "64";
166 #################################################################################
167 # errorflag: Returns an error flag (if any). #
171 # $errorflag = $outputmodule->errorflag(); #
172 #################################################################################
179 #################################################################################
180 # errormessage: Returns an error message (if any). #
184 # $errormessage = $outputmodule->errormessage(); #
185 #################################################################################
187 return $error_message;
192 #################################################################################
193 # clearflag: Clears the error message flag and the error message itself. #
197 # $outputmodule->clearflag(); #
198 #################################################################################
206 #################################################################################
207 # exportimage: Exports the image. #
211 # $exportmodule->exportimage(processedfilename, scansuridirectory, #
212 # scansfsdirectory, exportoptions); #
214 # processedfilename Specifies the processed file to export. #
215 # scansuridirectory Specifies the URI of the scans directory. #
216 # scansfsdirectory Specifies the FS location of the scans directory. #
217 # exportoptions Specifies the options for the export module. #
218 #################################################################################
220 my ($exportoptions, %exportoptions);
223 my $processed_filename = shift;
224 my $scans_uri = shift;
225 my $scans_fs = shift;
228 if (!$exportoptions{filename}){
230 $exportoptions{filename} = basename($processed_filename);
234 # Try to connect to the SMTP server.
236 my $smtp = Net::SMTP->new($exportoptions{servername},
238 Port => $exportoptions{serverport}
239 ) or ($error_flag = 1, $error_message = $!, return);
241 $smtp->auth($exportoptions{username}, $exportoptions{password}) or ($error_flag = 1, $error_message = $smtp->message(), return);
243 # Setup the sender and recipient addresses.
245 $smtp->mail($exportoptions{senderaddress}) or ($error_flag = 1, $error_message = $smtp->message(), return);
247 $smtp->recipient($exportoptions{emailaddress}) or ($error_flag = 1, $error_message = $smtp->message(), return);
249 $smtp->data() or ($error_flag = 1, $error_message = $smtp->message(), return);
251 # Generate a random value and get the MIME type for the file.
253 my $randomhex = uc(sprintf("%x",int(rand(75000000))));
254 my $mime_type = mimetype($processed_filename);
256 # Write out the header.
258 $smtp->datasend("Content-Disposition: inline\n");
259 $smtp->datasend("Content-Type: multipart/mixed; boundary=\"=-" . $randomhex ."\"\n");
260 $smtp->datasend("MIME-Version: 1.0\n");
261 $smtp->datasend("Subject: " . $exportoptions{subjectline} . "\n");
262 $smtp->datasend("To:" . $exportoptions{emailaddress} . "\n");
263 $smtp->datasend("X-Mailer: Xestia Scanner Server 0.1.0 (Email.pm; http://xestia.co.uk/scannerserver)\n");
265 # Write the message (if there).
267 $smtp->datasend("\n");
269 $smtp->datasend("--=-" . $randomhex . "\n");
270 $smtp->datasend("Content-Type: text/plain;\n");
271 $smtp->datasend("\tcharset=utf-8\n");
272 $smtp->datasend("Content-Transfer-Encoding: 7bit\n\n");
274 $smtp->datasend($exportoptions{personalmessage} . "\n\n");
276 $smtp->datasend("--=-" . $randomhex . "\n");
278 # Encode the file to Base64 and "attach" to the email.
280 $smtp->datasend("Content-Disposition: attachment;\n");
281 $smtp->datasend("\tfilename=" . $exportoptions{filename} . "\n");
282 $smtp->datasend("Content-Type: " . $mime_type . ";\n");
283 $smtp->datasend("\tcharset=us-ascii;\n");
284 $smtp->datasend("\tname=" . $exportoptions{filename} . "\n");
285 $smtp->datasend("Content-Transfer-Encoding: base64\n\n");
287 # Open the file and process it into Base64.
289 open((my $picture), "<", $processed_filename) or ($error_flag = 1, $error_message = $!, return);
294 while (read($picture, $line, 60*57)){
296 $smtp->datasend(encode_base64($line));
302 $smtp->datasend("--" . $randomhex . "--\n");
303 $smtp->datasend("\n--=-" . $randomhex . "--\n");
312 #################################################################################
313 # languagestrings: Language strings that are used in this module. #
317 # $string = $outputmodule->languagestrings("langstring"); #
318 #################################################################################
320 my $langstring = shift;
323 my ($language_strings, %language_strings);
326 if ($language_name eq "en-GB" or !$language_name){
328 # Language strings for English (British) language.
330 $language_strings{servername} = "Server name:";
331 $language_strings{serverport} = "Server port:";
332 $language_strings{senderaddress} = "Sender email address:";
333 $language_strings{emailaddress} = "Recipient email address:";
334 $language_strings{username} = "Username:";
335 $language_strings{password} = "Password:";
336 $language_strings{filename} = "Filename for image (blank for default):";
337 $language_strings{subjectline} = "Subject line:";
338 $language_strings{scannedimage} = "Scanned Image";
339 $language_strings{personalmessage} = "Personal Message:";
343 # Invalid language so use English (British) as default.
345 $language_strings{servername} = "Server name:";
346 $language_strings{serverport} = "Server port:";
347 $language_strings{senderaddress} = "Sender email address:";
348 $language_strings{emailaddress} = "Recipient email address:";
349 $language_strings{username} = "Username:";
350 $language_strings{password} = "Password:";
351 $language_strings{filename} = "Filename for image (blank for default):";
352 $language_strings{subjectline} = "Subject line:";
353 $language_strings{scannedimage} = "Scanned Image";
354 $language_strings{personalmessage} = "Personal Message:";
358 $language_string = $language_strings{$langstring};
362 $language_string =~ s/%s/$item/;
366 return $language_string;