root/modules/branches/2.4/core/bin/fax-process.pl

Revision 5848, 4.6 kB (checked in by p_lindheimer, 4 years ago)

#2791, #2449 - use RFC 2822 date format for fax email

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
  • Property svn:executable set to *
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 %2x to proper characters, leave anything else alone.
56   $subject =~ s/\%20/ /g;
57     $subject =~ s/\%21/\!/g;
58     $subject =~ s/\%22/\"/g;
59     $subject =~ s/\%23/\#/g;
60     $subject =~ s/\%24/\$/g;
61     $subject =~ s/\%25/\%/g;
62     $subject =~ s/\%26/\&/g;
63     $subject =~ s/\%27/\'/g;
64     $subject =~ s/\%28/\(/g;
65     $subject =~ s/\%29/\)/g;
66     $subject =~ s/\%2a/\*/g;
67     $subject =~ s/\%2A/\*/g;
68     $subject =~ s/\%2b/\+/g;
69     $subject =~ s/\%2B/\+/g;
70     $subject =~ s/\%2c/\,/g;
71     $subject =~ s/\%2C/\,/g;
72     $subject =~ s/\%2d/\-/g;
73     $subject =~ s/\%2D/\-/g;
74     $subject =~ s/\%2e/\./g;
75     $subject =~ s/\%2E/\./g;
76     $subject =~ s/\%2f/\//g;
77     $subject =~ s/\%2F/\//g;
78   } elsif ($cmd eq "--type") {
79   my $tmp = shift @ARGV;
80   $ct = $tmp if (defined $tmp);
81   } elsif ($cmd eq "--from") {
82   my $tmp = shift @ARGV;
83   $from = $tmp if (defined $tmp);
84   } elsif ($cmd eq "--file") {
85   my $tmp = shift @ARGV;
86   $file = $tmp if (defined $tmp);
87   } elsif ($cmd eq "--attachment") {
88   my $tmp = shift @ARGV;
89   $attachment = $tmp if (defined $tmp);
90   } else {
91   die "$cmd not understood\n$usage\n";
92   }
93
94 }
95
96 # OK. All our variables are set up.
97 # Lets make sure that we know about a file...
98 die $usage unless $file;
99 # and that the file exists...
100 open( FILE, $file ) or die "Error opening $file: $!";
101 # Oh, did we possibly not specify an attachment name?
102 $attachment = $file unless ($attachment);
103
104 my $encoded="";
105 my $buf="";
106 # First, lets find out if it's a TIFF file
107 read(FILE, $buf, 4);
108 if ($buf eq "MM\x00\x2a" || $buf eq "II\x2a\x00") {
109   # Tiff magic - We need to convert it to pdf first
110   # Need to do some error testing here - what happens if tiff2pdf
111   # doesn't exist?
112   open PDF, "tiff2pdf $file|";
113   $buf = "";
114   while (read(PDF, $buf, 60*57))  {
115       $encoded .= encode_base64($buf);
116   }
117   close PDF;
118 } else {
119   # It's a PDF already
120   # Go back to the start of the file, and start again
121   seek(FILE, 0, 0);
122   while (read(FILE, $buf, 60*57)) {
123     $encoded .= encode_base64($buf);
124   }
125 }
126 close FILE;
127
128 # Now we have the file, we should ensure that there's no paths on the
129 # filename..
130 $attachment =~ s/^.+\///;
131
132 # And that's pretty much all the hard work done. Now we just create the
133 # headers for the MIME encapsulation:
134 my $boundary = '------FREEPBX_FAX_MAIL:';
135 my $dtime = `date -R`;
136 chomp $dtime;
137 my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
138 foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; }
139
140 my $len = length $encoded;
141 # message body..
142 my $msg ="Content-Class: urn:content-classes:message
143 Content-Transfer-Encoding: 7bit
144 MIME-Version: 1.0
145 Content-Type: multipart/mixed; boundary=\"$boundary\"
146 From: $from
147 Date: $dtime
148 Reply-To: $from
149 X-Mailer: dofaxmail.pl
150 To: $to
151 Subject: $subject
152
153 This is a multi-part message in MIME format.
154
155 --$boundary
156 Content-Type: text/plain; charset=\"us-ascii\"
157 Content-Transfer-Encoding: quoted-printable
158
159 A Fax has been recieved by the fax gateway, and is attached to this message.
160
161
162 --$boundary
163 Content-Type: $ct; name=\"$attachment\"
164 Content-Transfer-Encoding: base64
165 Content-Disposition: attachment; filename=\"$attachment\"
166
167 $encoded
168 --$boundary--
169 ";
170
171 #print "$msg";
172 # Now we just send it.
173 my $smtp = Net::SMTP-> new("127.0.0.1", Debug => 0) or
174   die "Net::SMTP::new: $!";
175 $smtp-> mail($from);
176 $smtp-> recipient($to);
177 $smtp-> data();
178 $smtp-> datasend($msg);
179 $smtp-> dataend();
Note: See TracBrowser for help on using the browser.