| 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 $enc_gif=""; |
|---|
| 106 |
my $buf=""; |
|---|
| 107 |
# First, lets find out if it's a TIFF file |
|---|
| 108 |
read(FILE, $buf, 4); |
|---|
| 109 |
if ($buf eq "MM\x00\x2a" || $buf eq "II\x2a\x00") { |
|---|
| 110 |
# Tiff magic - We need to convert it to pdf first |
|---|
| 111 |
# Need to do some error testing here - what happens if tiff2pdf |
|---|
| 112 |
# doesn't exist? |
|---|
| 113 |
open PDF, "tiff2pdf $file|"; |
|---|
| 114 |
$buf = ""; |
|---|
| 115 |
while (read(PDF, $buf, 60*57)) { |
|---|
| 116 |
$encoded .= encode_base64($buf); |
|---|
| 117 |
} |
|---|
| 118 |
close PDF; |
|---|
| 119 |
|
|---|
| 120 |
open GIF, "convert -resize '50%' -monochrome -delay 300 ${file}[0,1] gif:- |"; |
|---|
| 121 |
$buf = ""; |
|---|
| 122 |
while (read(GIF, $buf, 60*57)) { |
|---|
| 123 |
$enc_gif .= encode_base64($buf); |
|---|
| 124 |
} |
|---|
| 125 |
close GIF; |
|---|
| 126 |
} else { |
|---|
| 127 |
# It's a PDF already |
|---|
| 128 |
# Go back to the start of the file, and start again |
|---|
| 129 |
seek(FILE, 0, 0); |
|---|
| 130 |
while (read(FILE, $buf, 60*57)) { |
|---|
| 131 |
$encoded .= encode_base64($buf); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
close FILE; |
|---|
| 135 |
|
|---|
| 136 |
# Now we have the file, we should ensure that there's no paths on the |
|---|
| 137 |
# filename.. |
|---|
| 138 |
$attachment =~ s/^.+\///; |
|---|
| 139 |
|
|---|
| 140 |
# And that's pretty much all the hard work done. Now we just create the |
|---|
| 141 |
# headers for the MIME encapsulation: |
|---|
| 142 |
my $boundary = '------FREEPBX_FAX_MAIL:'; |
|---|
| 143 |
my $dtime = `date`; |
|---|
| 144 |
chomp $dtime; |
|---|
| 145 |
my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z'); |
|---|
| 146 |
foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; } |
|---|
| 147 |
|
|---|
| 148 |
my $len = length $encoded; |
|---|
| 149 |
my $len_gif = length $enc_gif; |
|---|
| 150 |
# message body.. |
|---|
| 151 |
my $msg ="Content-Class: urn:content-classes:message |
|---|
| 152 |
Content-Transfer-Encoding: 7bit |
|---|
| 153 |
MIME-Version: 1.0 |
|---|
| 154 |
Content-Type: multipart/mixed; boundary=\"$boundary\" |
|---|
| 155 |
From: $from |
|---|
| 156 |
Date: $dtime |
|---|
| 157 |
Reply-To: $from |
|---|
| 158 |
X-Mailer: dofaxmail.pl |
|---|
| 159 |
To: $to |
|---|
| 160 |
Subject: $subject |
|---|
| 161 |
|
|---|
| 162 |
This is a multi-part message in MIME format. |
|---|
| 163 |
|
|---|
| 164 |
--$boundary |
|---|
| 165 |
Content-Type: text/plain; charset=\"us-ascii\" |
|---|
| 166 |
Content-Transfer-Encoding: quoted-printable |
|---|
| 167 |
|
|---|
| 168 |
A Fax has been received by the fax gateway, and is attached to this message. |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
--$boundary |
|---|
| 172 |
Content-Type: image/gif; name=\"thumb.gif\" |
|---|
| 173 |
Content-Transfer-Encoding: base64 |
|---|
| 174 |
|
|---|
| 175 |
$enc_gif |
|---|
| 176 |
--$boundary |
|---|
| 177 |
Content-Type: $ct; name=\"$attachment\" |
|---|
| 178 |
Content-Transfer-Encoding: base64 |
|---|
| 179 |
Content-Disposition: attachment; filename=\"$attachment\" |
|---|
| 180 |
|
|---|
| 181 |
$encoded |
|---|
| 182 |
--$boundary-- |
|---|
| 183 |
"; |
|---|
| 184 |
|
|---|
| 185 |
#print "$msg"; |
|---|
| 186 |
# Now we just send it. |
|---|
| 187 |
my $smtp = Net::SMTP-> new("127.0.0.1", Debug => 0) or |
|---|
| 188 |
die "Net::SMTP::new: $!"; |
|---|
| 189 |
$smtp-> mail($from); |
|---|
| 190 |
$smtp-> recipient($to); |
|---|
| 191 |
$smtp-> data(); |
|---|
| 192 |
$smtp-> datasend($msg); |
|---|
| 193 |
$smtp-> dataend(); |
|---|