From 4b80fe118da16b21603fcdbd090bc03d8fbf0578 Mon Sep 17 00:00:00 2001 From: mark Date: Fri, 1 Jul 2011 05:33:55 +0000 Subject: [PATCH] send email from customer view, #13444 --- FS/FS/Misc.pm | 58 ++++++++++++++++-------------------- FS/FS/cust_main/Search.pm | 27 +++++++++++++++++ FS/FS/cust_main_Mixin.pm | 10 +++---- FS/FS/msg_template.pm | 26 +++++++--------- httemplate/misc/email-customers.html | 17 +++++++---- httemplate/search/cust_msg.html | 10 +++---- httemplate/view/cust_main.cgi | 11 +++++++ 7 files changed, 94 insertions(+), 65 deletions(-) diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index d5f02de1d..3b0de3dc3 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -89,10 +89,14 @@ encoding which, if specified, overrides the default "7bit". (optional) type parameter for multipart/related messages -=item cust_msg +=item custnum -(optional) L object. If provided, it will be updated -with the message envelope information, contents, and server response. +(optional) L key; if passed, the message will be logged +(if logging is enabled) with this custnum. + +=item msgnum + +(optional) L key, for logging. =back @@ -255,18 +259,20 @@ sub send_email { } # 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; + if ( $conf->exists('log_sent_mail') and $options{'custnum'} ) { + my $cust_msg = FS::cust_msg->new({ + 'env_from' => $options{'from'}, + 'env_to' => join(', ', @to), + 'header' => $message->header_as_string, + 'body' => $message->body_as_string, + '_date' => $time, + 'error' => $error, + 'custnum' => $options{'custnum'}, + 'msgnum' => $options{'msgnum'}, + 'status' => ($error ? 'failed' : 'sent'), + }); + $cust_msg->insert; # ignore errors + } } @@ -302,9 +308,9 @@ Will be placed inside an HTML tag. Email body (Text alternative). Arrayref of lines, or scalar. -=item cust_msg (optional) +=item custnum, msgnum (optional) -An L object. Will be passed through to send_email. +Customer and template numbers, passed through to send_email for logging. =back @@ -322,21 +328,9 @@ sub generate_email { my $me = '[FS::Misc::generate_email]'; - my %return = ( - 'from' => $args{'from'}, - 'to' => $args{'to'}, - 'bcc' => $args{'bcc'}, - 'subject' => $args{'subject'}, - 'cust_msg'=> $args{'cust_msg'}, - ); - - #if (ref($args{'to'}) eq 'ARRAY') { - # $return{'to'} = $args{'to'}; - #} else { - # $return{'to'} = [ grep { $_ !~ /^(POST|FAX)$/ } - # $self->cust_main->invoicing_list - # ]; - #} + my @fields = qw(from to bcc subject custnum msgnum); + my %return; + @return{@fields} = @args{@fields}; warn "$me creating HTML/text multipart message" if $DEBUG; diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm index 06a4522f4..5636fc6e4 100644 --- a/FS/FS/cust_main/Search.pm +++ b/FS/FS/cust_main/Search.pm @@ -471,6 +471,33 @@ sub search { my @where = (); my $orderby; + # initialize these to prevent warnings + $params = { + 'custnum' => '', + 'agentnum' => '', + 'usernum' => '', + 'status' => '', + 'address' => '', + 'paydate_year' => '', + 'invoice_terms' => '', + 'custbatch' => '', + %$params + }; + + ## + # explicit custnum(s) + ## + + if ( $params->{'custnum'} ) { + my @custnums = ref($params->{'custnum'}) ? + @{ $params->{'custnum'} } : + $params->{'custnum'}; + push @where, + 'cust_main.custnum IN (' . + join(',', map { $_ =~ /^(\d+)$/ ? $1 : () } @custnums ) . + ')' if scalar(@custnums) > 0; + } + ## # parse agent ## diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index 8c8553c09..e8e243f2d 100644 --- a/FS/FS/cust_main_Mixin.pm +++ b/FS/FS/cust_main_Mixin.pm @@ -408,9 +408,6 @@ sub email_search_result { or die "msgnum $msgnum not found\n"; } - $param->{'payby'} = [ split(/\0/, $param->{'payby'}) ] - unless ref($param->{'payby'}); - my $sql_query = $class->search($param->{'search'}); my $count_query = delete($sql_query->{'count_query'}); @@ -463,15 +460,16 @@ sub email_search_result { @message = $msg_template->prepare( 'cust_main' => $cust_main ); } else { - my $to = $cust_main->invoicing_list_emailonly_scalar; - next if !$to; + my @to = $cust_main->invoicing_list_emailonly; + next if !@to; @message = ( 'from' => $from, - 'to' => $to, + 'to' => \@to, 'subject' => $subject, 'html_body' => $html_body, 'text_body' => $text_body, + 'custnum' => $cust_main->custnum, ); } #if $msg_template diff --git a/FS/FS/msg_template.pm b/FS/FS/msg_template.pm index 4a1e34584..e90cffd73 100644 --- a/FS/FS/msg_template.pm +++ b/FS/FS/msg_template.pm @@ -193,11 +193,6 @@ The I field in the template takes precedence over this. 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 @@ -318,16 +313,16 @@ sub prepare { $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); - } +# 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, @@ -339,7 +334,6 @@ sub prepare { 'html_body' => $body, 'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70 )->format( HTML::TreeBuilder->new_from_content($body) ), - @cust_msg, ); } diff --git a/httemplate/misc/email-customers.html b/httemplate/misc/email-customers.html index 97ad8d8d4..d981aeae9 100644 --- a/httemplate/misc/email-customers.html +++ b/httemplate/misc/email-customers.html @@ -76,7 +76,7 @@ @@ -107,18 +107,22 @@ Template: <% include('/elements/tr-input-text.html', 'field' => 'from', 'label' => 'From:', + 'size' => 50, ) %> <% include('/elements/tr-input-text.html', 'field' => 'subject', 'label' => 'Subject:', + 'size' => 50, ) %> - Message: - <% include('/elements/htmlarea.html', 'field'=>'html_body') %> + Message: + <& '/elements/htmlarea.html', + 'field' => 'html_body', + 'width' => 600 &> @@ -160,7 +164,7 @@ else { @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search; } -my $title = 'Send bulk customer notices'; +my $title = 'Send customer notices'; my $num_cust; my $from = $cgi->param('from') || ''; @@ -185,8 +189,9 @@ if ( $cgi->param('action') eq 'preview' ) { or die "template not found: ".$cgi->param('msgnum'); $sql_query->{'extra_sql'} .= ' LIMIT 1'; $sql_query->{'order_by'} = ''; - my $cust = qsearchs($sql_query)->cust_main; - my %message = $msg_template->prepare( 'cust_main' => $cust, 'preview' => 1 ); + my $object = qsearchs($sql_query); + my $cust = $object->cust_main; + my %message = $msg_template->prepare( 'cust_main' => $cust ); ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'}; } } diff --git a/httemplate/search/cust_msg.html b/httemplate/search/cust_msg.html index 7932ab3a2..64fae30bd 100644 --- a/httemplate/search/cust_msg.html +++ b/httemplate/search/cust_msg.html @@ -16,7 +16,9 @@ $date ? time2str('%Y-%m-%d %T',$_[0]->_date) : '' }, 'msgname', - 'env_to', + sub { + join('
', split(/,\s*/, $_[0]->env_to) ) + }, 'status', sub { encode_entities($_[0]->error) }, ], @@ -83,7 +85,6 @@ my $sub_popup_link = sub { }; my %color = ( - 'prepared' => '0000FF', 'failed' => 'FF0000', 'sent' => '', ); @@ -104,11 +105,10 @@ include('/elements/input-date-field.html', include('/elements/select.html', 'field' => 'status', 'curr_value' => $cgi->param('status') || '', - 'options' => [ '', 'failed', 'sent', 'prepared' ], + 'options' => [ '', 'failed', 'sent', ], 'labels' => { '' => '(any)', 'failed' => 'failed', - 'sent' => 'sent', - 'prepared'=> 'prepared' }, + 'sent' => 'sent', }, ) . ' diff --git a/httemplate/view/cust_main.cgi b/httemplate/view/cust_main.cgi index a6c917f20..dcadf9912 100755 --- a/httemplate/view/cust_main.cgi +++ b/httemplate/view/cust_main.cgi @@ -97,6 +97,17 @@ function areyousure(href, message) { % $br=1; <% mt('View billing events for this customer') |h %> % } +% +% my $email_link = ($cust_main->invoicing_list_emailonly) && +% include('/elements/email-link.html', +% 'table' => 'cust_main', +% 'search_hash' => { 'custnum' => $custnum }, +% 'label' => 'Email a notice to this customer', +% ); +% if ( $email_link and $br ) { + | +% } +<% $email_link || '' %> % if ( $conf->config('cust_main-external_links') ) { <% $br++ ? ' | ' : '' %> -- 2.11.0