1 package FS::part_export::send_email;
3 use vars qw(@ISA %info);
6 use FS::Record qw(qsearch qsearchs);
9 use FS::Misc qw(send_email);
11 @ISA = qw(FS::part_export);
14 my %template_select = (
20 option_values => sub {
21 %templates = (0 => '',
22 map { $_->msgnum, $_->msgname }
23 qsearch({ table => 'msg_template',
25 order_by => 'ORDER BY msgnum ASC'
28 sort keys (%templates);
32 tie my %options, 'Tie::IxHash', (
33 'insert_template' => {
37 <TR><TH></TH><TH>Template</TH></TR>
38 <TR><TD>New service</TD><TD>',
43 'delete_template' => {
45 <TR><TD>Delete</TD><TD>',
50 'replace_template' => {
52 <TR><TD>Modify</TD><TD>',
57 'suspend_template' => {
59 <TR><TD>Suspend</TD><TD>',
64 'unsuspend_template' => {
66 <TR><TD>Unsuspend</TD><TD>',
73 label => 'Send to customer',
77 label => 'Send to other address: ',
83 'svc' => [qw( svc_acct svc_broadband svc_phone svc_domain )],
85 'Send an email message',
86 'options' => \%options,
89 Send an email message. The subject and body of the message
90 will be generated from a message template.'
94 my( $self, $action, $svc ) = (shift, shift, shift);
95 my $conf = new FS::Conf;
97 my $msgnum = $self->option($action.'_template');
100 my $msg_template = FS::msg_template->by_key($msgnum);
101 return "Template $msgnum not found\n" if !$msg_template;
103 my $cust_pkg = $svc->cust_svc->cust_pkg;
104 my $cust_main = $svc->cust_svc->cust_pkg->cust_main if $cust_pkg;
105 my $custnum = $cust_main->custnum if $cust_main;
106 my $svcnum = $svc->svcnum if $action ne 'delete';
108 my @to = split(',', $self->option('to_address') || '');
109 push @to, $cust_main->invoicing_list_emailonly
110 if $self->option('to_customer') and $cust_main;
112 warn 'No destination address for send_email export: custnum '.$cust_main->custnum;
113 # warn, don't die, but also avoid sending the template with _no_ 'to'=>
114 # param, which would send to the customer by default.
118 if ( $action eq 'replace' ) {
120 return $msg_template->send(
121 'cust_main' => $cust_main,
122 'object' => [ $svc, $old ],
123 'to' => join(',', @to),
127 return $msg_template->send(
128 'cust_main' => $cust_main,
130 'to' => join(',', @to),
136 my($self, $svc) = (shift, shift);
137 $self->_export('insert', $svc);
140 sub _export_replace {
141 my($self, $new, $old) = (shift, shift, shift);
142 $self->_export('replace', $new, $old);
146 my($self, $svc) = (shift, shift);
147 $self->_export('delete', $svc);
150 sub _export_suspend {
151 my($self, $svc) = (shift, shift);
152 $self->_export('suspend', $svc);
155 sub _export_unsuspend {
156 my($self, $svc) = (shift, shift);
157 $self->_export('unsuspend', $svc);