From: khoff Date: Thu, 17 Mar 2005 21:41:36 +0000 (+0000) Subject: Added options invoice_email_pdf and invoice_email_pdf_note. X-Git-Tag: BEFORE_FINAL_MASONIZE~644 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=821c540a2d41f633b1a4e1cd8bda82b8b2ff60de 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. --- diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 8ce51385b..61e74d0ea 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -532,6 +532,21 @@ httemplate/docs/config.html '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', diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index df9717068..71fe7e712 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -41,12 +41,15 @@ I - (optional) MIME type I - (required) arrayref of body text lines +I - (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 @@ sub send_email { $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; @@ -138,6 +165,9 @@ sub Mail::Internet::mysmtpsend { # Send it + #warn "Headers: \n" . join('',@{$hdr->header}); + #warn "Body: \n" . join('',@{$src->body}); + my $ok = $smtp->mail( $envelope ) && $smtp->to(@addr) && $smtp->data(join("", @{$hdr->header},"\n",@{$src->body})); diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index ecc6d5f4c..b00c38b2b 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -318,6 +318,80 @@ sub owed { $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. + +=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 @@ sub send { @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; diff --git a/httemplate/docs/install.html b/httemplate/docs/install.html index f55ce6452..eaebcde2b 100644 --- a/httemplate/docs/install.html +++ b/httemplate/docs/install.html @@ -66,6 +66,7 @@ Before installing, you need:
  • Spreadsheet::WriteExcel
  • IO-stringy (IO::Scalar)
  • Frontier::RPC (Frontier::RPC2) +
  • MIME::Entity (MIME-tools)
  • Apache::DBI (optional but recommended for better webinterface performance) diff --git a/httemplate/misc/email-invoice.cgi b/httemplate/misc/email-invoice.cgi index a560a1838..a3130c79f 100755 --- a/httemplate/misc/email-invoice.cgi +++ b/httemplate/misc/email-invoice.cgi @@ -10,10 +10,9 @@ my $cust_bill = qsearchs('cust_bill',{'invnum'=>$invnum}); die "Can't find invoice!\n" unless $cust_bill; my $error = send_email( - 'from' => $cust_bill->_agent_invoice_from || $conf->config('invoice_from'), - 'to' => [ grep { $_ ne 'POST' } $cust_bill->cust_main->invoicing_list ], - 'subject' => 'Invoice', - 'body' => [ $cust_bill->print_text ], + $cust_bill->generate_email( + 'from' => $cust_bill->_agent_invoice_from || $conf->config('invoice_from'), + ) ); eidiot($error) if $error;