logging of template-generated mail, #12809
[freeside.git] / FS / FS / Misc.pm
index 0e8d92b..d5f02de 100644 (file)
@@ -20,6 +20,7 @@ use Tie::IxHash;
                  pkg_freqs
                  generate_ps generate_pdf do_print
                  csv_from_fixed
+                 ocr_image
                );
 
 $DEBUG = 0;
@@ -88,6 +89,11 @@ encoding which, if specified, overrides the default "7bit".
 
 (optional) type parameter for multipart/related messages
 
+=item cust_msg
+
+(optional) L<FS::cust_msg> object.  If provided, it will be updated 
+with the message envelope information, contents, and server response.
+
 =back
 
 =cut
@@ -170,12 +176,13 @@ sub send_email {
   }
   my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
 
+  my $time = time;
   my $message = MIME::Entity->build(
     'From'       => $options{'from'},
     'To'         => join(', ', @to),
     'Sender'     => $options{'from'},
     'Reply-To'   => $options{'from'},
-    'Date'       => time2str("%a, %d %b %Y %X %z", time),
+    'Date'       => time2str("%a, %d %b %Y %X %z", $time),
     'Subject'    => $options{'subject'},
     'Message-ID' => "<$message_id>",
     @mimeargs,
@@ -237,13 +244,30 @@ sub send_email {
   eval { sendmail($message, { transport => $transport,
                               from      => $options{from},
                               to        => \@to }) };
+
+  my $error = '';
   if(ref($@) and $@->isa('Email::Sender::Failure')) {
-    return ($@->code ? $@->code.' ' : '').$@->message
+    $error = $@->code.' ' if $@->code;
+    $error .= $@->message;
   }
   else {
-    return $@;
+    $error = $@;
   }
+
+  # Logging
+  my $cust_msg = $options{'cust_msg'};
+  if ( $cust_msg ) {
+    $cust_msg->env_from($options{from});
+    $cust_msg->env_to(join(",", @to));
+    $cust_msg->header($message->header_as_string);
+    $cust_msg->body($message->body_as_string);
+    $cust_msg->_date($time);
+    $cust_msg->error($error);
+    $cust_msg->status( $error ? 'failed' : 'sent' );
+    $cust_msg->replace;
+  };
+  return $error;
+   
 }
 
 =item generate_email OPTION => VALUE ...
@@ -278,6 +302,10 @@ Will be placed inside an HTML <BODY> tag.
 
 Email body (Text alternative).  Arrayref of lines, or scalar.
 
+=item cust_msg (optional)
+
+An L<FS::cust_msg> object.  Will be passed through to send_email.
+
 =back
 
 Constructs a multipart message from text_body and html_body.
@@ -299,6 +327,7 @@ sub generate_email {
     'to'      => $args{'to'},
     'bcc'     => $args{'bcc'},
     'subject' => $args{'subject'},
+    'cust_msg'=> $args{'cust_msg'},
   );
 
   #if (ref($args{'to'}) eq 'ARRAY') {
@@ -322,9 +351,9 @@ sub generate_email {
 
   my $data;
   if ( ref($args{'text_body'}) eq 'ARRAY' ) {
-    $data = $args{'text_body'};
+    $data = join("\n", @{ $args{'text_body'} });
   } else {
-    $data = [ split(/\n/, $args{'text_body'}) ];
+    $data = $args{'text_body'};
   }
 
   $alternative->attach(
@@ -766,10 +795,13 @@ sub _pslatex {
 
     local($SIG{CHLD}) = sub {};
     run( \@cmd, '>'=>'/dev/null', '2>'=>'/dev/null', timeout($timeout) )
-      or die "pslatex $file.tex failed; see $file.log for details?\n";
+      or warn "bad exit status from pslatex pass $_\n";
 
   }
 
+  return if -e "$file.dvi" && -s "$file.dvi";
+  die "pslatex $file.tex failed; see $file.log for details?\n";
+
 }
 
 =item print ARRAYREF
@@ -850,6 +882,41 @@ sub csv_from_fixed {
   '';
 }
 
+=item ocr_image IMAGE_SCALAR
+
+Runs OCR on the provided image data and returns a list of text lines.
+
+=cut
+
+sub ocr_image {
+  my $logo_data = shift;
+
+  #XXX use conf dir location from Makefile
+  my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
+  my $fh = new File::Temp(
+    TEMPLATE => 'bizcard.XXXXXXXX',
+    SUFFIX   => '.png', #XXX assuming, but should handle jpg, gif, etc. too
+    DIR      => $dir,
+    UNLINK   => 0,
+  ) or die "can't open temp file: $!\n";
+
+  my $filename = $fh->filename;
+
+  print $fh $logo_data;
+  close $fh;
+
+  run( [qw(ocroscript recognize), $filename], '>'=>"$filename.hocr" )
+    or die "ocroscript recognize failed\n";
+
+  run( [qw(ocroscript hocr-to-text), "$filename.hocr"], '>pipe'=>\*OUT )
+    or die "ocroscript hocr-to-text failed\n";
+
+  my @lines = split(/\n/, <OUT> );
+
+  foreach (@lines) { s/\.c0m\s*$/.com/; }
+
+  @lines;
+}
 
 =back