4 use vars qw ( @ISA @EXPORT_OK $DEBUG );
8 #do NOT depend on any FS:: modules here, causes weird (sometimes unreproducable
9 #until on client machine) dependancy loops. put them in FS::Misc::Something
12 @ISA = qw( Exporter );
13 @EXPORT_OK = qw( send_email send_fax
14 states_hash counties state_label
22 FS::Misc - Miscellaneous subroutines
26 use FS::Misc qw(send_email);
32 Miscellaneous subroutines. This module contains miscellaneous subroutines
33 called from multiple other modules. These are not OO or necessarily related,
34 but are collected here to elimiate code duplication.
40 =item send_email OPTION => VALUE ...
46 I<to> - (required) comma-separated scalar or arrayref of recipients
48 I<subject> - (required)
50 I<content-type> - (optional) MIME type for the body
52 I<body> - (required unless I<nobody> is true) arrayref of body text lines
54 I<mimeparts> - (optional, but required if I<nobody> is true) arrayref of MIME::Entity->build PARAMHASH refs or MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach().
56 I<nobody> - (optional) when set true, send_email will ignore the I<body> option and simply construct a message with the given I<mimeparts>. In this case,
57 I<content-type>, if specified, overrides the default "multipart/mixed" for the outermost MIME container.
59 I<content-encoding> - (optional) when using nobody, optional top-level MIME
60 encoding which, if specified, overrides the default "7bit".
62 I<type> - (optional) type parameter for multipart/related messages
69 use Mail::Internet 1.44;
73 FS::UID->install_callback( sub {
80 my %doptions = %options;
81 $doptions{'body'} = '(full body not shown in debug)';
82 warn "FS::Misc::send_email called with options:\n ". Dumper(\%doptions);
83 # join("\n", map { " $_: ". $options{$_} } keys %options ). "\n"
86 $ENV{MAILADDRESS} = $options{'from'};
87 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
91 if ( $options{'nobody'} ) {
93 croak "'mimeparts' option required when 'nobody' option given\n"
94 unless $options{'mimeparts'};
96 @mimeparts = @{$options{'mimeparts'}};
99 'Type' => ( $options{'content-type'} || 'multipart/mixed' ),
100 'Encoding' => ( $options{'content-encoding'} || '7bit' ),
105 @mimeparts = @{$options{'mimeparts'}}
106 if ref($options{'mimeparts'}) eq 'ARRAY';
108 if (scalar(@mimeparts)) {
111 'Type' => 'multipart/mixed',
112 'Encoding' => '7bit',
115 unshift @mimeparts, {
116 'Type' => ( $options{'content-type'} || 'text/plain' ),
117 'Data' => $options{'body'},
118 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
119 'Disposition' => 'inline',
125 'Type' => ( $options{'content-type'} || 'text/plain' ),
126 'Data' => $options{'body'},
127 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
135 if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
138 warn 'no domain found in invoice from address '. $options{'from'}.
139 '; constructing Message-ID @example.com';
140 $domain = 'example.com';
142 my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
144 my $message = MIME::Entity->build(
145 'From' => $options{'from'},
147 'Sender' => $options{'from'},
148 'Reply-To' => $options{'from'},
149 'Date' => time2str("%a, %d %b %Y %X %z", time),
150 'Subject' => $options{'subject'},
151 'Message-ID' => "<$message_id>",
155 if ( $options{'type'} ) {
156 #false laziness w/cust_bill::generate_email
157 $message->head->replace('Content-type',
159 '; boundary="'. $message->head->multipart_boundary. '"'.
160 '; type='. $options{'type'}
164 foreach my $part (@mimeparts) {
166 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
168 warn "attaching MIME part from MIME::Entity object\n"
170 $message->add_part($part);
172 } elsif ( ref($part) eq 'HASH' ) {
174 warn "attaching MIME part from hashref:\n".
175 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
177 $message->attach(%$part);
180 croak "mimepart $part isn't a hashref or MIME::Entity object!";
185 my $smtpmachine = $conf->config('smtpmachine');
188 $message->mysmtpsend( 'Host' => $smtpmachine,
189 'MailFrom' => $options{'from'},
194 #this kludges a "mysmtpsend" method into Mail::Internet for send_email above
195 package Mail::Internet;
200 sub Mail::Internet::mysmtpsend {
203 my $host = $opt{Host};
204 my $envelope = $opt{MailFrom};
207 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
209 push(@hello, 'Port', $opt{'Port'})
210 if exists $opt{'Port'};
212 push(@hello, 'Debug', $opt{'Debug'})
213 if exists $opt{'Debug'};
215 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
220 #local $SIG{__DIE__};
221 #$smtp = eval { Net::SMTP->new($host, @hello) };
222 $smtp = new Net::SMTP $host, @hello;
225 unless ( defined($smtp) ) {
227 $err =~ s/Invalid argument/Unknown host/;
228 return "can't connect to $host: $err"
231 my $hdr = $src->head->dup;
237 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
238 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
240 my @addr = map($_->address, Mail::Address->parse(@rcpt));
242 return 'No valid destination addresses found!'
245 $hdr->delete('Bcc'); # Remove blind Cc's
249 #warn "Headers: \n" . join('',@{$hdr->header});
250 #warn "Body: \n" . join('',@{$src->body});
252 my $ok = $smtp->mail( $envelope ) &&
254 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
261 return $smtp->code. ' '. $smtp->message;
268 =item send_fax OPTION => VALUE ...
272 I<dialstring> - (required) 10-digit phone number w/ area code
274 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
278 I<docfile> - (required) Filename of PostScript TIFF Class F document
280 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
289 die 'HylaFAX support has not been configured.'
290 unless $conf->exists('hylafax');
293 require Fax::Hylafax::Client;
297 if ($@ =~ /^Can't locate Fax.*/) {
298 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
304 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
306 die 'Called send_fax without a \'dialstring\'.'
307 unless exists($options{'dialstring'});
309 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
310 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
311 my $fh = new File::Temp(
312 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
315 ) or die "can't open temp file: $!\n";
317 $options{docfile} = $fh->filename;
319 print $fh @{$options{'docdata'}};
322 delete $options{'docdata'};
325 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
326 unless exists($options{'docfile'});
328 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
331 $options{'dialstring'} =~ s/[^\d\+]//g;
332 if ($options{'dialstring'} =~ /^\d{10}$/) {
333 $options{dialstring} = '+1' . $options{'dialstring'};
335 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
338 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
340 if ($faxjob->success) {
341 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
346 return 'Error while sending FAX: ' . $faxjob->trace;
351 =item states_hash COUNTRY
353 Returns a list of key/value pairs containing state (or other sub-country
354 division) abbriviations and names.
358 use FS::Record qw(qsearch);
359 use Locale::SubCountry;
366 map { s/[\n\r]//g; $_; }
370 'table' => 'cust_main_county',
371 'hashref' => { 'country' => $country },
372 'extra_sql' => 'GROUP BY state',
375 #it could throw a fatal "Invalid country code" error (for example "AX")
376 my $subcountry = eval { new Locale::SubCountry($country) }
377 or return ( '', '(n/a)' );
379 #"i see your schwartz is as big as mine!"
380 map { ( $_->[0] => $_->[1] ) }
381 sort { $a->[1] cmp $b->[1] }
382 map { [ $_ => state_label($_, $subcountry) ] }
386 =item counties STATE COUNTRY
388 Returns a list of counties for this state and country.
393 my( $state, $country ) = @_;
395 sort map { s/[\n\r]//g; $_; }
398 'select' => 'DISTINCT county',
399 'table' => 'cust_main_county',
400 'hashref' => { 'state' => $state,
401 'country' => $country,
406 =item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT
411 my( $state, $country ) = @_;
413 unless ( ref($country) ) {
414 $country = eval { new Locale::SubCountry($country) }
419 # US kludge to avoid changing existing behaviour
420 # also we actually *use* the abbriviations...
421 my $full_name = $country->country_code eq 'US'
423 : $country->full_name($state);
425 $full_name = '' if $full_name eq 'unknown';
426 $full_name =~ s/\(see also.*\)\s*$//;
427 $full_name .= " ($state)" if $full_name;
429 $full_name || $state || '(n/a)';
435 Returns a hash reference of the accepted credit card types. Keys are shorter
436 identifiers and values are the longer strings used by the system (see
437 L<Business::CreditCard>).
444 my $conf = new FS::Conf;
447 #displayname #value (Business::CreditCard)
448 "VISA" => "VISA card",
449 "MasterCard" => "MasterCard",
450 "Discover" => "Discover card",
451 "American Express" => "American Express card",
452 "Diner's Club/Carte Blanche" => "Diner's Club/Carte Blanche",
453 "enRoute" => "enRoute",
455 "BankCard" => "BankCard",
456 "Switch" => "Switch",
459 my @conf_card_types = grep { ! /^\s*$/ } $conf->config('card-types');
460 if ( @conf_card_types ) {
461 #perhaps the hash is backwards for this, but this way works better for
462 #usage in selfservice
463 %card_types = map { $_ => $card_types{$_} }
466 grep { $card_types{$d} eq $_ } @conf_card_types
482 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
484 L<Fax::Hylafax::Client>