Ticket #2413: fax-process.pl

File fax-process.pl, 4.3 kB (added by splante, 1 year ago)

Modified version of fax-process.pl with patch included

Line 
1 #!/usr/bin/perl -w
2
3 # Small program to process a tiff file into a PDF and email it.
4 #
5 # Distributed under the terms of the GNU General Public License (GPL) Version 2
6 # Copyright 2005 by Rob Thomas (xrobau@gmail.com)
7
8 use MIME::Base64;
9 use Net::SMTP;
10
11 # Default paramaters
12 my $to = "xrobau\@gmail.com";
13 my $from = "fax\@";
14 my $subject = "Fax received";
15 my $ct = "application/x-pdf";
16 my $file = undef;
17 my $attachment = undef;
18
19 # Care about the hostname.
20 my $hostname = `/bin/hostname`;
21 chomp ($hostname);
22 if ($hostname =~ /localhost/) {
23         $hostname = "set.your.hostname.com";
24 }
25 $from .= $hostname;
26
27 # Usage:
28 my $usage="Usage: --file filename [--attachment filename] [--to email_address] [--from email_address] [--type content/type] [--subject \"Subject Of Email\"]";
29
30 # Parse command line..
31 while (my $cmd = shift @ARGV) {
32   chomp $cmd;
33   # My kingdom for a 'switch'
34   if ($cmd eq "--to") {
35         my $tmp = shift @ARGV;
36         $to = $tmp if (defined $tmp);
37   } elsif ($cmd eq "--subject") {
38         my $tmp = shift @ARGV;
39         if ($tmp =~ /\^(\")|^(\')/) {
40                 # It's a quoted string
41                 my $delim = $+;   # $+ is 'last match', which is ' or "
42                 $tmp =~ s/\Q$delim\E//; # Strip out ' or "
43                 $subject = $tmp;
44                 while ($tmp = shift @ARGV) {
45                         if ($tmp =~ /\Q$delim\E/) {
46                                 $tmp =~ s/\Q$delim\E//;
47                                 last;
48                         }
49                 $subject .= $tmp;
50                 }
51         } else {
52                 # It's a single word
53                 $subject = $tmp;
54         }
55   # Convert %20 to spaces, leave anythign else alone.
56   $subject =~ s/\%20/ /g;
57   } elsif ($cmd eq "--type") {
58         my $tmp = shift @ARGV;
59         $ct = $tmp if (defined $tmp);
60   } elsif ($cmd eq "--from") {
61         my $tmp = shift @ARGV;
62         $from = $tmp if (defined $tmp);
63   } elsif ($cmd eq "--file") {
64         my $tmp = shift @ARGV;
65         $file = $tmp if (defined $tmp);
66   } elsif ($cmd eq "--attachment") {
67         my $tmp = shift @ARGV;
68         $attachment = $tmp if (defined $tmp);
69   } else {
70         die "$cmd not understood\n$usage\n";
71   }
72
73 }
74
75 # OK. All our variables are set up.
76 # Lets make sure that we know about a file...
77 die $usage unless $file;
78 # and that the file exists...
79 open( FILE, $file ) or die "Error opening $file: $!";
80 # Oh, did we possibly not specify an attachment name?
81 $attachment = $file unless ($attachment);
82
83 my $encoded="";
84 my $enc_gif="";
85 my $buf="";
86 # First, lets find out if it's a TIFF file
87 read(FILE, $buf, 4);
88 if ($buf eq "MM\x00\x2a" || $buf eq "II\x2a\x00") {
89         # Tiff magic - We need to convert it to pdf first
90         # Need to do some error testing here - what happens if tiff2pdf
91         # doesn't exist?
92         open PDF, "tiff2pdf $file|";
93         $buf = "";
94         while (read(PDF, $buf, 60*57))  {
95                 $encoded .= encode_base64($buf);
96         }
97         close PDF;
98
99         open GIF, "convert -resize '50%' -monochrome -delay 300 ${file}[0,1] gif:- |";
100         $buf = "";
101         while (read(GIF, $buf, 60*57))  {
102                 $enc_gif .= encode_base64($buf);
103         }
104         close GIF;
105 } else {
106         # It's a PDF already
107         # Go back to the start of the file, and start again
108         seek(FILE, 0, 0);
109         while (read(FILE, $buf, 60*57)) {
110                 $encoded .= encode_base64($buf);
111         }
112 }
113 close FILE;
114
115 # Now we have the file, we should ensure that there's no paths on the
116 # filename..
117 $attachment =~ s/^.+\///;
118
119 # And that's pretty much all the hard work done. Now we just create the
120 # headers for the MIME encapsulation:
121 my $boundary = '------FREEPBX_FAX_MAIL:';
122 my $dtime = `date`;
123 chomp $dtime;
124 my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
125 foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; }
126
127 my $len = length $encoded;
128 my $len_gif = length $enc_gif;
129 # message body..
130 my $msg ="Content-Class: urn:content-classes:message
131 Content-Transfer-Encoding: 7bit
132 MIME-Version: 1.0
133 Content-Type: multipart/mixed; boundary=\"$boundary\"
134 From: $from
135 Date: $dtime
136 Reply-To: $from
137 X-Mailer: dofaxmail.pl
138 To: $to
139 Subject: $subject
140
141 This is a multi-part message in MIME format.
142
143 --$boundary
144 Content-Type: text/plain; charset=\"us-ascii\"
145 Content-Transfer-Encoding: quoted-printable
146
147 A Fax has been received by the fax gateway, and is attached to this message.
148
149
150 --$boundary
151 Content-Type: image/gif; name=\"thumb.gif\"
152 Content-Transfer-Encoding: base64
153
154 $enc_gif
155 --$boundary
156 Content-Type: $ct; name=\"$attachment\"
157 Content-Transfer-Encoding: base64
158 Content-Disposition: attachment; filename=\"$attachment\"
159
160 $encoded
161 --$boundary--
162 ";
163
164 #print "$msg";
165 # Now we just send it.
166 my $smtp = Net::SMTP-> new("127.0.0.1", Debug => 0) or
167   die "Net::SMTP::new: $!";
168 $smtp-> mail($from);
169 $smtp-> recipient($to);
170 $smtp-> data();
171 $smtp-> datasend($msg);
172 $smtp-> dataend();
Donate



Support
Download
Develop
Forums
News
Documentation
Paid Support
About

Paid Ads