X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FMisc.pm;h=101a2d4e01827694432b8f733c65fedf94f86747;hp=7d7b7d06169a3fb66d6056ed6c80d56474a7a659;hb=929f432c766bbe3bdeed5b80818a12ddf6ec6339;hpb=bdda4e1ae0e0247f0f641529b4779f8b61de809b diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index 7d7b7d061..101a2d4e0 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -1,11 +1,15 @@ package FS::Misc; use strict; -use vars qw ( @ISA @EXPORT_OK ); +use vars qw ( @ISA @EXPORT_OK $DEBUG ); use Exporter; +use Carp; +use Data::Dumper; @ISA = qw( Exporter ); -@EXPORT_OK = qw( send_email send_fax ); +@EXPORT_OK = qw( send_email send_fax states_hash state_label ); + +$DEBUG = 0; =head1 NAME @@ -37,11 +41,19 @@ I - (required) comma-separated scalar or arrayref of recipients I - (required) -I - (optional) MIME type +I - (optional) MIME type for the body + +I - (required unless I is true) arrayref of body text lines + +I - (optional, but required if I is true) arrayref of MIME::Entity->build PARAMHASH refs or MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach(). -I - (required) arrayref of body text lines +I - (optional) when set true, send_email will ignore the I option and simply construct a message with the given I. In this case, +I, if specified, overrides the default "multipart/mixed" for the outermost MIME container. -I - (optional) arrayref of MIME::Entity->build PARAMHASH refs, not MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach(). +I - (optional) when using nobody, optional top-level MIME +encoding which, if specified, overrides the default "7bit". + +I - (optional) type parameter for multipart/related messages =cut @@ -58,48 +70,110 @@ FS::UID->install_callback( sub { sub send_email { my(%options) = @_; + if ( $DEBUG ) { + my %doptions = %options; + $doptions{'body'} = '(full body not shown in debug)'; + warn "FS::Misc::send_email called with options:\n ". Dumper(\%doptions); +# join("\n", map { " $_: ". $options{$_} } keys %options ). "\n" + } $ENV{MAILADDRESS} = $options{'from'}; my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to}; - my @mimeparts = (ref($options{'mimeparts'}) eq 'ARRAY') - ? @{$options{'mimeparts'}} : (); - my $mimetype = (scalar(@mimeparts)) ? 'multipart/mixed' : 'text/plain'; + my @mimeargs = (); + my @mimeparts = (); + if ( $options{'nobody'} ) { + + croak "'mimeparts' option required when 'nobody' option given\n" + unless $options{'mimeparts'}; + + @mimeparts = @{$options{'mimeparts'}}; - my @mimeargs; - if (scalar(@mimeparts)) { @mimeargs = ( - 'Type' => 'multipart/mixed', + 'Type' => ( $options{'content-type'} || 'multipart/mixed' ), + 'Encoding' => ( $options{'content-encoding'} || '7bit' ), ); - push @mimeparts, - { + } else { + + @mimeparts = @{$options{'mimeparts'}} + if ref($options{'mimeparts'}) eq 'ARRAY'; + + if (scalar(@mimeparts)) { + + @mimeargs = ( + 'Type' => 'multipart/mixed', + 'Encoding' => '7bit', + ); + + unshift @mimeparts, { + 'Type' => ( $options{'content-type'} || 'text/plain' ), 'Data' => $options{'body'}, + 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ), 'Disposition' => 'inline', - 'Type' => (($options{'content-type'} ne '') - ? $options{'content-type'} : 'text/plain'), }; + + } else { + + @mimeargs = ( + 'Type' => ( $options{'content-type'} || 'text/plain' ), + 'Data' => $options{'body'}, + 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ), + ); + + } + + } + + my $domain; + if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) { + $domain = $1; } else { - @mimeargs = ( - 'Type' => (($options{'content-type'} ne '') - ? $options{'content-type'} : 'text/plain'), - 'Data' => $options{'body'}, - ); + warn 'no domain found in invoice from address '. $options{'from'}. + '; constructing Message-ID @example.com'; + $domain = 'example.com'; } + my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain"; 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'}, + 'From' => $options{'from'}, + 'To' => $to, + 'Sender' => $options{'from'}, + 'Reply-To' => $options{'from'}, + 'Date' => time2str("%a, %d %b %Y %X %z", time), + 'Subject' => $options{'subject'}, + 'Message-ID' => "<$message_id>", @mimeargs, ); + if ( $options{'type'} ) { + #false laziness w/cust_bill::generate_email + $message->head->replace('Content-type', + $message->mime_type. + '; boundary="'. $message->head->multipart_boundary. '"'. + '; type='. $options{'type'} + ); + } + foreach my $part (@mimeparts) { - next unless ref($part) eq 'HASH'; #warn? - $message->attach(%$part); + + if ( UNIVERSAL::isa($part, 'MIME::Entity') ) { + + warn "attaching MIME part from MIME::Entity object\n" + if $DEBUG; + $message->add_part($part); + + } elsif ( ref($part) eq 'HASH' ) { + + warn "attaching MIME part from hashref:\n". + join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n" + if $DEBUG; + $message->attach(%$part); + + } else { + croak "mimepart $part isn't a hashref or MIME::Entity object!"; + } + } my $smtpmachine = $conf->config('smtpmachine'); @@ -111,6 +185,80 @@ sub send_email { } +#this kludges a "mysmtpsend" method into Mail::Internet for send_email above +package Mail::Internet; + +use Mail::Address; +use Net::SMTP; + +sub Mail::Internet::mysmtpsend { + my $src = shift; + my %opt = @_; + my $host = $opt{Host}; + my $envelope = $opt{MailFrom}; + my $noquit = 0; + my $smtp; + my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : (); + + push(@hello, 'Port', $opt{'Port'}) + if exists $opt{'Port'}; + + push(@hello, 'Debug', $opt{'Debug'}) + if exists $opt{'Debug'}; + + if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) { + $smtp = $host; + $noquit = 1; + } + else { + #local $SIG{__DIE__}; + #$smtp = eval { Net::SMTP->new($host, @hello) }; + $smtp = new Net::SMTP $host, @hello; + } + + unless ( defined($smtp) ) { + my $err = $!; + $err =~ s/Invalid argument/Unknown host/; + return "can't connect to $host: $err" + } + + my $hdr = $src->head->dup; + + _prephdr($hdr); + + # Who is it to + + my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'}; + @rcpt = map { $hdr->get($_) } qw(To Cc Bcc) + unless @rcpt; + my @addr = map($_->address, Mail::Address->parse(@rcpt)); + + return 'No valid destination addresses found!' + unless(@addr); + + $hdr->delete('Bcc'); # Remove blind Cc's + + # 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})); + + if ( $ok ) { + $smtp->quit + unless $noquit; + return ''; + } else { + return $smtp->code. ' '. $smtp->message; + } + +} +package FS::Misc; +#eokludge + =item send_fax OPTION => VALUE ... Options: @@ -185,86 +333,77 @@ sub send_fax { if ($faxjob->success) { warn "Successfully queued fax to '$options{dialstring}' with jobid " . - $faxjob->jobid; + $faxjob->jobid + if $DEBUG; + return ''; } else { return 'Error while sending FAX: ' . $faxjob->trace; } - return ''; - } -package Mail::Internet; - -use Mail::Address; -use Net::SMTP; +=item states_hash COUNTRY -sub Mail::Internet::mysmtpsend { - my $src = shift; - my %opt = @_; - my $host = $opt{Host}; - my $envelope = $opt{MailFrom}; - my $noquit = 0; - my $smtp; - my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : (); +Returns a list of key/value pairs containing state (or other sub-country +division) abbriviations and names. - push(@hello, 'Port', $opt{'Port'}) - if exists $opt{'Port'}; - - push(@hello, 'Debug', $opt{'Debug'}) - if exists $opt{'Debug'}; - - if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) { - $smtp = $host; - $noquit = 1; - } - else { - #local $SIG{__DIE__}; - #$smtp = eval { Net::SMTP->new($host, @hello) }; - $smtp = new Net::SMTP $host, @hello; - } - - unless ( defined($smtp) ) { - my $err = $!; - $err =~ s/Invalid argument/Unknown host/; - return "can't connect to $host: $err" - } - - my $hdr = $src->head->dup; +=cut - _prephdr($hdr); +use FS::Record qw(qsearch); +use Locale::SubCountry; + +sub states_hash { + my($country) = @_; + + my @states = +# sort + map { s/[\n\r]//g; $_; } + map { $_->state; } + qsearch( 'cust_main_county', + { 'country' => $country }, + 'DISTINCT ON ( state ) *', + ) + ; + + #it could throw a fatal "Invalid country code" error (for example "AX") + my $subcountry = eval { new Locale::SubCountry($country) } + or return ( '', '(n/a)' ); + + #"i see your schwartz is as big as mine!" + map { ( $_->[0] => $_->[1] ) } + sort { $a->[1] cmp $b->[1] } + map { [ $_ => state_label($_, $subcountry) ] } + @states; +} - # Who is it to +=item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT - my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'}; - @rcpt = map { $hdr->get($_) } qw(To Cc Bcc) - unless @rcpt; - my @addr = map($_->address, Mail::Address->parse(@rcpt)); +=cut - return 'No valid destination addresses found!' - unless(@addr); +sub state_label { + my( $state, $country ) = @_; - $hdr->delete('Bcc'); # Remove blind Cc's + unless ( ref($country) ) { + $country = eval { new Locale::SubCountry($country) } + or return'(n/a)'; - # Send it + } - #warn "Headers: \n" . join('',@{$hdr->header}); - #warn "Body: \n" . join('',@{$src->body}); + # US kludge to avoid changing existing behaviour + # also we actually *use* the abbriviations... + my $full_name = $country->country_code eq 'US' + ? '' + : $country->full_name($state); - my $ok = $smtp->mail( $envelope ) && - $smtp->to(@addr) && - $smtp->data(join("", @{$hdr->header},"\n",@{$src->body})); + $full_name = '' if $full_name eq 'unknown'; + $full_name =~ s/\(see also.*\)\s*$//; + $full_name .= " ($state)" if $full_name; - if ( $ok ) { - $smtp->quit - unless $noquit; - return ''; - } else { - return $smtp->code. ' '. $smtp->message; - } + $full_name || $state || '(n/a)'; } -package FS::Misc; + +=back =head1 BUGS