[freeside-commits] freeside/FS/FS Conf.pm, 1.397.2.48, 1.397.2.49 Mason.pm, 1.55.2.8, 1.55.2.9 Misc.pm, 1.42.2.2, 1.42.2.3 Schema.pm, 1.239.2.41, 1.239.2.42 cust_msg.pm, NONE, 1.1.2.1 msg_template.pm, 1.14.2.4, 1.14.2.5

Mark Wells mark at wavetail.420.am
Fri May 20 13:19:27 PDT 2011


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv12171/FS/FS

Modified Files:
      Tag: FREESIDE_2_1_BRANCH
	Conf.pm Mason.pm Misc.pm Schema.pm msg_template.pm 
Added Files:
      Tag: FREESIDE_2_1_BRANCH
	cust_msg.pm 
Log Message:
logging of template-generated mail, #12809

Index: Conf.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Conf.pm,v
retrieving revision 1.397.2.48
retrieving revision 1.397.2.49
diff -u -w -d -r1.397.2.48 -r1.397.2.49
--- Conf.pm	5 May 2011 17:23:41 -0000	1.397.2.48
+++ Conf.pm	20 May 2011 20:19:22 -0000	1.397.2.49
@@ -622,6 +622,13 @@
   },
 
   {
+    'key'         => 'log_sent_mail',
+    'section'     => 'notification',
+    'description' => 'Enable logging of template-generated email.',
+    'type'        => 'checkbox',
+  },
+
+  {
     'key'         => 'alert_expiration',
     'section'     => 'notification',
     'description' => 'Enable alerts about billing method expiration (i.e. expiring credit cards).',

Index: Mason.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Mason.pm,v
retrieving revision 1.55.2.8
retrieving revision 1.55.2.9
diff -u -w -d -r1.55.2.8 -r1.55.2.9
--- Mason.pm	13 May 2011 20:03:50 -0000	1.55.2.8
+++ Mason.pm	20 May 2011 20:19:23 -0000	1.55.2.9
@@ -268,6 +268,7 @@
   use FS::areacode;
   use FS::msa;
   use FS::rate_center;
+  use FS::cust_msg;
   # Sammath Naur
 
   if ( $FS::Mason::addl_handler_use ) {

--- NEW FILE: cust_msg.pm ---
package FS::cust_msg;

use strict;
use base qw( FS::cust_main_Mixin FS::Record );
use FS::Record qw( qsearch qsearchs );
use vars qw( @statuses );

=head1 NAME

FS::cust_msg - Object methods for cust_msg records

=head1 SYNOPSIS

  use FS::cust_msg;

  $record = new FS::cust_msg \%hash;
  $record = new FS::cust_msg { 'column' => 'value' };

  $error = $record->insert;

  $error = $record->check;

=head1 DESCRIPTION

An FS::cust_msg object represents a template-generated message sent to 
a customer (see L<FS::msg_template>).  FS::cust_msg inherits from
FS::Record.  The following fields are currently supported:

=over 4

=item custmsgnum - primary key

=item custnum - customer number

=item msgnum - template number

=item _date - the time the message was sent

=item env_from - envelope From address

=item env_to - envelope To addresses, including Bcc, separated by newlines

=item header - message header

=item body - message body

=item error - Email::Sender error message (or null for success)

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new 

=cut

# the new method can be inherited from FS::Record, if a table method is defined

sub table { 'cust_msg'; }

sub nohistory_fields { ('header', 'body'); } 
# history is kind of pointless on this table

@statuses = qw( prepared sent failed );

=item insert

Adds this record to the database.  If there is an error, returns the error 
and emits a warning; otherwise returns false.

=cut

sub insert {
  # warn of all errors here; failing to insert/update one of these should 
  # cause a warning at worst
  my $self = shift;
  my $error = $self->SUPER::insert;
  warn "[cust_msg] error logging message status: $error\n" if $error;
  return $error;
}

=item delete

Delete this record from the database.  There's no reason to do this.

=cut

sub delete {
  my $self = shift;
  warn "[cust_msg] log entry deleted\n";
  return $self->SUPER::delete;
}

=item replace OLD_RECORD

Replaces the OLD_RECORD with this one in the database.  If there is an error,
returns the error and emits a warning, otherwise returns false.

=cut

sub replace {
  my $self = shift;
  my $error = $self->SUPER::replace(@_);
  warn "[cust_msg] error logging message status: $error\n" if $error;
  return $error;
}

=item check

Checks all fields to make sure this is a valid example.  If there is
an error, returns the error, otherwise returns false.  Called by the insert
and replace methods.

=cut

# the check method should currently be supplied - FS::Record contains some
# data checking routines

sub check {
  my $self = shift;

  my $error = 
    $self->ut_numbern('custmsgnum')
    || $self->ut_number('custnum')
    || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
    || $self->ut_numbern('msgnum')
    || $self->ut_foreign_keyn('msgnum', 'msg_template', 'msgnum')
    || $self->ut_numbern('_date')
    || $self->ut_textn('env_from')
    || $self->ut_textn('env_to')
    || $self->ut_anything('header')
    || $self->ut_anything('body')
    || $self->ut_enum('status', \@statuses)
    || $self->ut_textn('error')
  ;
  return $error if $error;

  $self->SUPER::check;
}

=back

=head1 SEE ALSO

L<FS::msg_template>, L<FS::cust_main>, L<FS::Record>.

=cut

1;


Index: msg_template.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/msg_template.pm,v
retrieving revision 1.14.2.4
retrieving revision 1.14.2.5
diff -u -w -d -r1.14.2.4 -r1.14.2.5
--- msg_template.pm	12 Apr 2011 20:57:12 -0000	1.14.2.4
+++ msg_template.pm	20 May 2011 20:19:25 -0000	1.14.2.5
@@ -7,11 +7,16 @@
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs );
 
+use FS::cust_main;
+use FS::cust_msg;
+
 use Date::Format qw( time2str );
 use HTML::Entities qw( decode_entities encode_entities ) ;
 use HTML::FormatText;
 use HTML::TreeBuilder;
-use vars '$DEBUG';
+use vars qw( $DEBUG $conf );
+
+FS::UID->install_callback( sub { $conf = new FS::Conf; } );
 
 $DEBUG=0;
 
@@ -188,6 +193,11 @@
 Destination address.  The default is to use the customer's 
 invoicing_list addresses.  Multiple addresses may be comma-separated.
 
+=item preview
+
+Set to true when preparing a message for previewing, rather than to actually 
+send it.  This turns off logging.
+
 =back
 
 =cut
@@ -298,7 +308,6 @@
   }
   # no warning when preparing with no destination
 
-  my $conf = new FS::Conf;
   my $from_addr = $self->from_addr;
 
   if ( !$from_addr ) {
@@ -309,8 +318,20 @@
     $from_addr ||= scalar( $conf->config('invoice_from',
                                          $cust_main->agentnum) );
   }
+  my @cust_msg = ();
+  if ( $conf->exists('log_sent_mail') and !$opt{'preview'} ) {
+    my $cust_msg = FS::cust_msg->new({
+        'custnum' => $cust_main->custnum,
+        'msgnum'  => $self->msgnum,
+        'status'  => 'prepared',
+      });
+    $cust_msg->insert;
+    @cust_msg = ('cust_msg' => $cust_msg);
+  }
 
   (
+    'custnum' => $cust_main->custnum,
+    'msgnum'  => $self->msgnum,
     'from' => $from_addr,
     'to'   => \@to,
     'bcc'  => $self->bcc_addr || undef,
@@ -318,6 +339,7 @@
     'html_body' => $body,
     'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70
                     )->format( HTML::TreeBuilder->new_from_content($body) ),
+    @cust_msg,
   );
 
 }
@@ -339,8 +361,7 @@
 # helper sub for package dates
 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
 
-# needed for some things
-my $conf = new FS::Conf;
+#my $conf = new FS::Conf;
 
 #return contexts and fill-in values
 # If you add anything, be sure to add a description in 
@@ -504,7 +525,6 @@
     [ 'warning_msgnum',  'warning_email',      'warning_email-subject', 'warning_email-from', '' ],
   );
  
-  my $conf = new FS::Conf;
   my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
   foreach my $agentnum (@agentnums) {
     foreach (@fixes) {

Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.239.2.41
retrieving revision 1.239.2.42
diff -u -w -d -r1.239.2.41 -r1.239.2.42
--- Schema.pm	16 May 2011 13:36:34 -0000	1.239.2.41
+++ Schema.pm	20 May 2011 20:19:23 -0000	1.239.2.42
@@ -3097,6 +3097,24 @@
       'index'       => [ ['agentnum'], ]
     },
 
+    'cust_msg' => {
+      'columns' => [
+        'custmsgnum', 'serial',     '',     '', '', '',
+        'custnum',       'int',     '',     '', '', '',
+        'msgnum',        'int', 'NULL',     '', '', '',
+        '_date',    @date_type,                 '', '',
+        'env_from',  'varchar', 'NULL',    255, '', '',
+        'env_to',    'varchar', 'NULL',    255, '', '',
+        'header',       'blob', 'NULL',     '', '', '',
+        'body',         'blob', 'NULL',     '', '', '',
+        'error',     'varchar', 'NULL',    255, '', '',
+        'status',    'varchar',     '',$char_d, '', '',
+      ],
+      'primary_key' => 'custmsgnum',
+      'unique'      => [ ],
+      'index'       => [ ['custnum'], ],
+    },
+
     'svc_cert' => {
       'columns' => [
         'svcnum',                'int',     '',      '', '', '', 

Index: Misc.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Misc.pm,v
retrieving revision 1.42.2.2
retrieving revision 1.42.2.3
diff -u -w -d -r1.42.2.2 -r1.42.2.3
--- Misc.pm	14 Apr 2011 00:11:05 -0000	1.42.2.2
+++ Misc.pm	20 May 2011 20:19:23 -0000	1.42.2.3
@@ -89,6 +89,11 @@
 
 (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
@@ -171,12 +176,13 @@
   }
   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,
@@ -239,12 +245,29 @@
                               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 ...
@@ -279,6 +302,10 @@
 
 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.
@@ -300,6 +327,7 @@
     'to'      => $args{'to'},
     'bcc'     => $args{'bcc'},
     'subject' => $args{'subject'},
+    'cust_msg'=> $args{'cust_msg'},
   );
 
   #if (ref($args{'to'}) eq 'ARRAY') {



More information about the freeside-commits mailing list