[freeside-commits] freeside/FS/FS Conf.pm, 1.452, 1.453 Mason.pm, 1.75, 1.76 Misc.pm, 1.44, 1.45 Schema.pm, 1.300, 1.301 cust_msg.pm, 1.1, 1.2 msg_template.pm, 1.18, 1.19
Mark Wells
mark at wavetail.420.am
Fri May 20 13:19:46 PDT 2011
Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv12299/FS/FS
Modified Files:
Conf.pm Mason.pm Misc.pm Schema.pm msg_template.pm
Added Files:
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.452
retrieving revision 1.453
diff -u -w -d -r1.452 -r1.453
--- Conf.pm 11 May 2011 16:20:12 -0000 1.452
+++ Conf.pm 20 May 2011 20:19:43 -0000 1.453
@@ -623,6 +623,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.75
retrieving revision 1.76
diff -u -w -d -r1.75 -r1.76
--- Mason.pm 18 May 2011 00:33:34 -0000 1.75
+++ Mason.pm 20 May 2011 20:19:43 -0000 1.76
@@ -285,6 +285,7 @@
use FS::did_order_item;
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.18
retrieving revision 1.19
diff -u -w -d -r1.18 -r1.19
--- msg_template.pm 12 Apr 2011 20:56:47 -0000 1.18
+++ msg_template.pm 20 May 2011 20:19:44 -0000 1.19
@@ -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.300
retrieving revision 1.301
diff -u -w -d -r1.300 -r1.301
--- Schema.pm 16 May 2011 02:04:16 -0000 1.300
+++ Schema.pm 20 May 2011 20:19:43 -0000 1.301
@@ -3354,6 +3354,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.44
retrieving revision 1.45
diff -u -w -d -r1.44 -r1.45
--- Misc.pm 14 Apr 2011 00:11:04 -0000 1.44
+++ Misc.pm 20 May 2011 20:19:43 -0000 1.45
@@ -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