Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Forked Xestia Scanner Server!
[xestiascansrv/.git] / cgi-files / Modules / Export / Email.pm
1 #################################################################################
2 # Xestia Scanner Server - Email Export Module.                                  #
3 # Sends the page as an email.                                                   #
4 #                                                                               #
5 # Copyright (C) 2011 Steve Brokenshire <sbrokenshire@xestia.co.uk>              #
6 #                                                                               #
7 # This module is licensed under the same license as Xestia Scanner Server which #
8 # is licensed under the GPL version 3.                                          #
9 #                                                                               #
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.                       #
13 #                                                                               #
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.                                  #
18 #                                                                               #
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.
29 use strict;
30 use warnings;
31 use Encode qw(decode_utf8);
32 use Tie::IxHash;
33 use Modules::System::Common;
34 use MIME::Base64 3.13 qw(encode_base64);
35 use File::Basename;
36 use Net::Domain qw(hostfqdn);
37 use Net::SMTP;
38 use File::Basename;
39 use File::MimeInfo;
41 # Set the following values.
43 our $VERSION = "0.1.0";
45 my $error_flag = 0;
46 my $error_message = "";
47 my $language_name = "";
48 my %optionshash = ();
50 sub new{
51 #################################################################################
52 # new: Create an instance of Modules::Output::Normal                            #
53 #                                                                               #
54 # Usage:                                                                        #
55 #                                                                               #
56 # $dbmodule = Modules::Output::Normal->new();                                   #
57 #################################################################################
58         
59         # Get the perl module name.
60         
61         my $class = shift;
62         my $self = {};
63         
64         return bless($self, $class);
65         
66 }
68 sub initialise{
69 #################################################################################
70 # initialise: Initialises the output module.                                    #
71 #                                                                               #
72 # Usage:                                                                        #
73 #                                                                               #
74 # $outputmodule->initialise();                                                  #
75 #################################################################################
76         
77 }
79 sub loadsettings{
80 #################################################################################
81 # loadsettings: Loads some settings for the output module.                      #
82 #                                                                               #
83 # Usage:                                                                        #
84 #                                                                               #
85 # $outputmodule->loadsettings(language);                                        #
86 #                                                                               #
87 # language      Specifies the language to use.                                  #
88 #################################################################################
89         
90         my $class = shift;
91         my $passed_lang = shift;
92         
93         $language_name = $passed_lang;
95 }
97 sub getoptions{
98 #################################################################################
99 # getoptions: Gets the options that will be used.                               #
100 #                                                                               #
101 # Usage:                                                                        #
102 #                                                                               #
103 # %options = $outputmodule->getoptions();                                       #
104 #################################################################################
105         
106         my (%options, $options);
107         tie(%options, "Tie::IxHash");
108         
109         $options{servername}{type} = "textbox";
110         $options{servername}{string} = languagestrings("servername");
111         $options{servername}{maxlength} = "256";
112         $options{servername}{size} = "64";
113         
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";
119         
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";
128         
129         $options{emailaddress}{type} = "textbox";
130         $options{emailaddress}{string} = languagestrings("emailaddress");
131         $options{emailaddress}{maxlength} = "256";
132         $options{emailaddress}{size} = "64";
133         
134         $options{username}{type} = "textbox";
135         $options{username}{string} = languagestrings("username");
136         $options{username}{maxlength} = "256";
137         $options{username}{size} = "32";
138         
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";
149         
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";
155         
156         $options{personalmessage}{type} = "textbox";
157         $options{personalmessage}{string} = languagestrings("personalmessage");
158         $options{personalmessage}{maxlength} = "1024";
159         $options{personalmessage}{size} = "64";
160         
161         return %options;
162         
165 sub errorflag{
166 #################################################################################
167 # errorflag: Returns an error flag (if any).                                    #
168 #                                                                               #
169 # Usage:                                                                        #
170 #                                                                               #
171 # $errorflag    = $outputmodule->errorflag();                                   #
172 #################################################################################
173         
174         return $error_flag;
175         
178 sub errormessage{
179 #################################################################################
180 # errormessage: Returns an error message (if any).                              #
181 #                                                                               #
182 # Usage:                                                                        #
183 #                                                                               #
184 # $errormessage = $outputmodule->errormessage();                                #
185 #################################################################################
186         
187         return $error_message;
188         
191 sub clearflag{
192 #################################################################################
193 # clearflag: Clears the error message flag and the error message itself.        #
194 #                                                                               #
195 # Usage:                                                                        #
196 #                                                                               #
197 # $outputmodule->clearflag();                                                   #
198 #################################################################################
199         
200         $error_flag     = 0;
201         $error_message  = "";
202         
205 sub exportimage{
206 #################################################################################
207 # exportimage: Exports the image.                                               #
208 #                                                                               #
209 # Usage:                                                                        #
210 #                                                                               #
211 # $exportmodule->exportimage(processedfilename, scansuridirectory,              #
212 #                               scansfsdirectory, exportoptions);               #
213 #                                                                               #
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);
221         
222         my $class = shift;
223         my $processed_filename = shift;
224         my $scans_uri = shift;
225         my $scans_fs = shift;
226         %exportoptions = @_;
227         
228         if (!$exportoptions{filename}){
229         
230                 $exportoptions{filename} = basename($processed_filename);
231                 
232         }
233         
234         # Try to connect to the SMTP server.
235         
236         my $smtp = Net::SMTP->new($exportoptions{servername},
237                 Hello => hostfqdn,
238                 Port => $exportoptions{serverport}
239         ) or ($error_flag = 1, $error_message = $!, return);
240         
241         $smtp->auth($exportoptions{username}, $exportoptions{password}) or ($error_flag = 1, $error_message = $smtp->message(), return);
242         
243         # Setup the sender and recipient addresses.
244         
245         $smtp->mail($exportoptions{senderaddress}) or ($error_flag = 1, $error_message = $smtp->message(), return);
246         
247         $smtp->recipient($exportoptions{emailaddress}) or ($error_flag = 1, $error_message = $smtp->message(), return);
248         
249         $smtp->data() or ($error_flag = 1, $error_message = $smtp->message(), return);
250         
251         # Generate a random value and get the MIME type for the file.
252         
253         my $randomhex = uc(sprintf("%x",int(rand(75000000))));
254         my $mime_type = mimetype($processed_filename);
255         
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");
264         
265         # Write the message (if there).
266         
267         $smtp->datasend("\n");
268         
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");
273         
274         $smtp->datasend($exportoptions{personalmessage} . "\n\n");
276         $smtp->datasend("--=-" . $randomhex . "\n");
277         
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.
288         
289         open((my $picture), "<", $processed_filename) or ($error_flag = 1, $error_message = $!, return);
290         binmode($picture);
291         
292         my $line;
293         
294         while (read($picture, $line, 60*57)){
295                 
296                 $smtp->datasend(encode_base64($line));
297                 
298         }
299         
300         close($picture);
301         
302         $smtp->datasend("--" . $randomhex . "--\n");
303         $smtp->datasend("\n--=-" . $randomhex . "--\n");
304         
305         $smtp->dataend();
306         
307         $smtp->quit();
308         
311 sub languagestrings{
312 #################################################################################
313 # languagestrings: Language strings that are used in this module.               #
314 #                                                                               #
315 # Usage:                                                                        #
316 #                                                                               #
317 # $string = $outputmodule->languagestrings("langstring");                       #
318 #################################################################################
319         
320         my $langstring = shift;
321         
322         my $language_string;
323         my ($language_strings, %language_strings);
324         my $item;
325         
326         if ($language_name eq "en-GB" or !$language_name){
327                 
328                 # Language strings for English (British) language.
329                 
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:";
340                 
341         } else {
342                 
343                 # Invalid language so use English (British) as default.
344                 
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:";
355                 
356         }
357         
358         $language_string = $language_strings{$langstring};
359         
360         foreach $item (@_){
361                 
362                 $language_string =~ s/%s/$item/;
363                 
364         }
365         
366         return $language_string;
367         
370 1;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy