freeside/FS/FS Conf.pm,1.120,1.121 Misc.pm,1.4,1.5 cust_bill.pm,1.103,1.104

Kristian Hoffmann khoff at pouncequick.420.am
Thu Mar 17 13:41:40 PST 2005


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory pouncequick:/tmp/cvs-serv27536/FS/FS

Modified Files:
	Conf.pm Misc.pm cust_bill.pm 
Log Message:
Added options invoice_email_pdf and invoice_email_pdf_note.
invoice_email_pdf - Attach PDF invoice to emailed plain text invoices.
invoice_email_pdf_note - Replace plain text invoice with this note, when attaching a PDF.


Index: cust_bill.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill.pm,v
retrieving revision 1.103
retrieving revision 1.104
diff -u -d -r1.103 -r1.104
--- cust_bill.pm	16 Feb 2005 02:53:54 -0000	1.103
+++ cust_bill.pm	17 Mar 2005 21:41:35 -0000	1.104
@@ -318,6 +318,80 @@
   $balance;
 }
 
+
+=item generate_email PARAMHASH
+
+PARAMHASH can contain the following:
+
+=over 4
+
+=item from       => sender address, required
+
+=item tempate    => alternate template name, optional
+
+=item print_text => text attachment arrayref, optional
+
+=item subject    => email subject, optional
+
+=back
+
+Returns an argument list to be passed to L<FS::cust_bill::send>.
+
+=cut
+
+sub generate_email {
+
+  my $self = shift;
+  my %args = @_;
+
+  my $mimeparts;
+  if ($conf->exists('invoice_email_pdf')) {
+    #warn "[FS::cust_bill::send] creating PDF attachment";
+    #mime parts arguments a la MIME::Entity->build().
+    $mimeparts = [
+      {
+        'Type'        => 'application/pdf',
+        'Encoding'    => 'base64',
+        'Data'        => [ $self->print_pdf('', $args{'template'}) ],
+        'Disposition' => 'attachment',
+        'Filename'    => 'invoice.pdf',
+      },
+    ];
+  }
+
+  my $email_text;
+  if ($conf->exists('invoice_email_pdf')
+      and scalar($conf->config('invoice_email_pdf_note'))) {
+
+    #warn "[FS::cust_bill::send] using 'invoice_email_pdf_note'";
+    $email_text = [ map { $_ . "\n" } $conf->config('invoice_email_pdf_note') ];
+  } else {
+    #warn "[FS::cust_bill::send] not using 'invoice_email_pdf_note'";
+    if (ref($args{'print_text'}) eq 'ARRAY') {
+      $email_text = $args{'print_text'};
+    } else {
+      $email_text = [ $self->print_text('', $args{'template'}) ];
+    }
+  }
+
+  my @invoicing_list;
+  if (ref($args{'to'} eq 'ARRAY')) {
+    @invoicing_list = @{$args{'to'}};
+  } else {
+    @invoicing_list = grep { $_ ne 'POST' } $self->cust_main->invoicing_list;
+  }
+
+  return (
+    'from'      => $args{'from'},
+    'to'        => [ @invoicing_list ],
+    'subject'   => (($args{'subject'}) ? $args{'subject'} : 'Invoice'),
+    'body'      => $email_text,
+    'mimeparts' => $mimeparts,
+  );
+
+
+}
+
 =item send [ TEMPLATENAME [ , AGENTNUM [ , INVOICE_FROM ] ] ]
 
 Sends this invoice to the destinations configured for this customer: send
@@ -350,10 +424,11 @@
     @invoicing_list = ($invoice_from) unless @invoicing_list;
 
     my $error = send_email(
-      'from'    => $invoice_from,
-      'to'      => [ grep { $_ ne 'POST' } @invoicing_list ],
-      'subject' => 'Invoice',
-      'body'    => \@print_text,
+      $self->generate_email(
+        'from'   => $invoice_from,
+        'to'     => [ grep { $_ ne 'POST' } @invoicing_list ],
+	'print_text' => [ @print_text ],
+      )
     );
     die "can't email invoice: $error\n" if $error;
     #die "$error\n" if $error;

Index: Misc.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Misc.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Misc.pm	20 Dec 2004 10:13:37 -0000	1.4
+++ Misc.pm	17 Mar 2005 21:41:35 -0000	1.5
@@ -41,12 +41,15 @@
 
 I<body> - (required) arrayref of body text lines
 
+I<mimeparts> - (optional) arrayref of MIME::Entity->build PARAMHASH refs, not MIME::Entity objects.  These will be passed as arguments to MIME::Entity->attach().
+
 =cut
 
 use vars qw( $conf );
 use Date::Format;
 use Mail::Header;
 use Mail::Internet 1.44;
+use MIME::Entity;
 use FS::UID;
 
 FS::UID->install_callback( sub {
@@ -58,23 +61,47 @@
 
   $ENV{MAILADDRESS} = $options{'from'};
   my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
-  my @header = (
-    'From: '.     $options{'from'},
-    'To: '.       $to,
-    'Sender: '.   $options{'from'},
-    'Reply-To: '. $options{'from'},
-    'Date: '.     time2str("%a, %d %b %Y %X %z", time),
-    'Subject: '.  $options{'subject'},
-  );
-  push @header, 'Content-Type: '. $options{'content-type'}
-    if exists($options{'content-type'});
-  my $header = new Mail::Header ( \@header );
 
-  my $message = new Mail::Internet (
-    'Header' => $header,
-    'Body'   => $options{'body'},
+  my @mimeparts = (ref($options{'mimeparts'}) eq 'ARRAY')
+                  ? @{$options{'mimeparts'}} : ();
+  my $mimetype = (scalar(@mimeparts)) ? 'multipart/mixed' : 'text/plain';
+
+  my @mimeargs;
+  if (scalar(@mimeparts)) {
+    @mimeargs = (
+      'Type'  => 'multipart/mixed',
+    );
+
+    push @mimeparts,
+      { 
+        'Data'        => $options{'body'},
+        'Disposition' => 'inline',
+        'Type'        => (($options{'content-type'} ne '')
+                          ? $options{'content-type'} : 'text/plain'),
+      };
+  } else {
+    @mimeargs = (
+      'Type'  => (($options{'content-type'} ne '')
+                  ? $options{'content-type'} : 'text/plain'),
+      'Data'  => $options{'body'},
+    );
+  }
+
+  my $message = MIME::Entity->build(
+    'From'      =>    $options{'from'},
+    'To'        =>    $to,
+    'Sender'    =>    $options{'from'},
+    'Reply-To'  =>    $options{'from'},
+    'Date'      =>    time2str("%a, %d %b %Y %X %z", time),
+    'Subject'   =>    $options{'subject'},
+    @mimeargs,
   );
 
+  foreach my $part (@mimeparts) {
+    next unless ref($part) eq 'HASH'; #warn?
+    $message->attach(%$part);
+  }
+
   my $smtpmachine = $conf->config('smtpmachine');
   $!=0;
 
@@ -137,6 +164,9 @@
     $hdr->delete('Bcc'); # Remove blind Cc's
 
     # Send it
+
+    #warn "Headers: \n" . join('',@{$hdr->header});
+    #warn "Body: \n" . join('',@{$src->body});
 
     my $ok = $smtp->mail( $envelope ) &&
 		$smtp->to(@addr) &&

Index: Conf.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Conf.pm,v
retrieving revision 1.120
retrieving revision 1.121
diff -u -d -r1.120 -r1.121
--- Conf.pm	8 Feb 2005 20:22:46 -0000	1.120
+++ Conf.pm	17 Mar 2005 21:41:35 -0000	1.121
@@ -532,6 +532,21 @@
     'type'        => 'textarea',
   },
 
+  {
+    'key'         => 'invoice_email_pdf',
+    'section'     => 'billing',
+    'description' => 'Send PDF invoice as an attachment to emailed invoices.  By default, includes the plain text invoice as the email body, unless invoice_email_pdf_note is set.',
+    'type'        => 'checkbox'
+  },
+
+  {
+    'key'         => 'invoice_email_pdf_note',
+    'section'     => 'billing',
+    'description' => 'If defined, this text will replace the default plain text invoice as the body of emailed PDF invoices.',
+    'type'        => 'textarea'
+  },
+
+
   { 
     'key'         => 'invoice_default_terms',
     'section'     => 'billing',




More information about the freeside-commits mailing list