Guest User

Untitled

a guest
Apr 16th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # Small program to process a tiff file into a PDF and email it via gmail.
  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. # Additions:
  8. # 2009 Sept 10: Modified to use Gmail (Daniel Parker <daniel@behindlogic.com>)
  9.  
  10. use Net::SMTP::SSL;
  11. use MIME::Base64;
  12.  
  13. # Default paramaters
  14. my $to = "jonny\@gmail.com";
  15. my $from = "fax\@";
  16. my $subject = "Fax received";
  17. my $ct = "application/x-pdf";
  18. my $file = undef;
  19. my $attachment = undef;
  20.  
  21. our $username = 'johnny@gmail.com';
  22. our $password = 'MySecretGmailPassword';
  23.  
  24. # Care about the hostname.
  25. my $hostname = `/bin/hostname`;
  26. chomp ($hostname);
  27. if ($hostname =~ /localhost/) {
  28. $hostname = "set.your.hostname.com";
  29. }
  30. $from .= $hostname;
  31.  
  32. # Usage:
  33. my $usage="Usage: --file filename [--attachment filename] [--to email_address] [--from email_address] [--type content/type] [--subject \"Subject Of Email\"]";
  34.  
  35. # Parse command line..
  36. while (my $cmd = shift @ARGV) {
  37. chomp $cmd;
  38. # My kingdom for a 'switch'
  39. if ($cmd eq "--to") {
  40. my $tmp = shift @ARGV;
  41. $to = $tmp if (defined $tmp);
  42. } elsif ($cmd eq "--subject") {
  43. my $tmp = shift @ARGV;
  44. if ($tmp =~ /\^(\")|^(\')/) {
  45. # It's a quoted string
  46. my $delim = $+; # $+ is 'last match', which is ' or "
  47. $tmp =~ s/\Q$delim\E//; # Strip out ' or "
  48. $subject = $tmp;
  49. while ($tmp = shift @ARGV) {
  50. if ($tmp =~ /\Q$delim\E/) {
  51. $tmp =~ s/\Q$delim\E//;
  52. last;
  53. }
  54. $subject .= $tmp;
  55. }
  56. } else {
  57. # It's a single word
  58. $subject = $tmp;
  59. }
  60. # Convert %2x to proper characters, leave anything else alone.
  61. $subject =~ s/\%20/ /g;
  62. $subject =~ s/\%21/\!/g;
  63. $subject =~ s/\%22/\"/g;
  64. $subject =~ s/\%23/\#/g;
  65. $subject =~ s/\%24/\$/g;
  66. $subject =~ s/\%25/\%/g;
  67. $subject =~ s/\%26/\&/g;
  68. $subject =~ s/\%27/\'/g;
  69. $subject =~ s/\%28/\(/g;
  70. $subject =~ s/\%29/\)/g;
  71. $subject =~ s/\%2a/\*/g;
  72. $subject =~ s/\%2A/\*/g;
  73. $subject =~ s/\%2b/\+/g;
  74. $subject =~ s/\%2B/\+/g;
  75. $subject =~ s/\%2c/\,/g;
  76. $subject =~ s/\%2C/\,/g;
  77. $subject =~ s/\%2d/\-/g;
  78. $subject =~ s/\%2D/\-/g;
  79. $subject =~ s/\%2e/\./g;
  80. $subject =~ s/\%2E/\./g;
  81. $subject =~ s/\%2f/\//g;
  82. $subject =~ s/\%2F/\//g;
  83. } elsif ($cmd eq "--type") {
  84. my $tmp = shift @ARGV;
  85. $ct = $tmp if (defined $tmp);
  86. } elsif ($cmd eq "--from") {
  87. my $tmp = shift @ARGV;
  88. $from = $tmp if (defined $tmp);
  89. } elsif ($cmd eq "--file") {
  90. my $tmp = shift @ARGV;
  91. $file = $tmp if (defined $tmp);
  92. } elsif ($cmd eq "--attachment") {
  93. my $tmp = shift @ARGV;
  94. $attachment = $tmp if (defined $tmp);
  95. } else {
  96. die "$cmd not understood\n$usage\n";
  97. }
  98.  
  99. }
  100.  
  101. # OK. All our variables are set up.
  102. # Lets make sure that we know about a file...
  103. die $usage unless $file;
  104. # and that the file exists...
  105. open( FILE, $file ) or die "Error opening $file: $!";
  106. # Oh, did we possibly not specify an attachment name?
  107. $attachment = $file unless ($attachment);
  108.  
  109. my $encoded="";
  110. my $buf="";
  111. # First, lets find out if it's a TIFF file
  112. read(FILE, $buf, 4);
  113. if ($buf eq "MM\x00\x2a" || $buf eq "II\x2a\x00") {
  114. # Tiff magic - We need to convert it to pdf first
  115. # Need to do some error testing here - what happens if tiff2pdf
  116. # doesn't exist?
  117. open PDF, "tiff2pdf $file|";
  118. $buf = "";
  119. while (read(PDF, $buf, 60*57)) {
  120. $encoded .= encode_base64($buf);
  121. }
  122. close PDF;
  123. } else {
  124. # It's a PDF already
  125. # Go back to the start of the file, and start again
  126. seek(FILE, 0, 0);
  127. while (read(FILE, $buf, 60*57)) {
  128. $encoded .= encode_base64($buf);
  129. }
  130. }
  131. close FILE;
  132.  
  133. # Now we have the file, we should ensure that there's no paths on the
  134. # filename..
  135. $attachment =~ s/^.+\///;
  136.  
  137. # And that's pretty much all the hard work done. Now we just create the
  138. # headers for the MIME encapsulation:
  139. my $boundary = '------FREEPBX_FAX_MAIL:';
  140. my $dtime = `date -R`;
  141. chomp $dtime;
  142. my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
  143. foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; }
  144.  
  145. my $len = length $encoded;
  146. # message body..
  147. my $msg ="Content-Class: urn:content-classes:message
  148. Content-Transfer-Encoding: 7bit
  149. MIME-Version: 1.0
  150. Content-Type: multipart/mixed; boundary=\"$boundary\"
  151. From: $from
  152. Date: $dtime
  153. Reply-To: $from
  154. X-Mailer: dofaxmail.pl
  155. To: $to
  156. Subject: $subject
  157.  
  158. This is a multi-part message in MIME format.
  159.  
  160. --$boundary
  161. Content-Type: text/plain; charset=\"us-ascii\"
  162. Content-Transfer-Encoding: quoted-printable
  163.  
  164. A Fax has been recieved by the fax gateway, and is attached to this message.
  165.  
  166.  
  167. --$boundary
  168. Content-Type: $ct; name=\"$attachment\"
  169. Content-Transfer-Encoding: base64
  170. Content-Disposition: attachment; filename=\"$attachment\"
  171.  
  172. $encoded
  173. --$boundary--
  174. ";
  175.  
  176. #print "$msg";
  177. # Now we just send it.
  178. # my $smtp = Net::SMTP-> new("127.0.0.1", Debug => 0) or
  179. # die "Net::SMTP::new: $!";
  180. my $smtp;
  181. # Create the SSL SMTP object
  182. unless($smtp = Net::SMTP::SSL->new('smtp.gmail.com', Port => 465, Debug => 1)) {
  183. die "Could not connect to server\n";
  184. }
  185. # Authenticate
  186. $smtp->auth($username, $password) || die "Authentication failed!\n";
  187. # Send the mail!
  188. $smtp-> mail($from);
  189. $smtp-> recipient($to);
  190. $smtp-> data();
  191. $smtp-> datasend($msg);
  192. $smtp-> dataend();
  193. $smtp->quit;
Add Comment
Please, Sign In to add comment