4 use vars qw ( @ISA @EXPORT_OK $DEBUG );
8 use IPC::Run qw( run timeout ); # for _pslatex
9 use IPC::Run3; # for do_print... should just use IPC::Run i guess
10 #do NOT depend on any FS:: modules here, causes weird (sometimes unreproducable
11 #until on client machine) dependancy loops. put them in FS::Misc::Something
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( send_email send_fax
16 states_hash counties state_label
18 generate_ps generate_pdf do_print
25 FS::Misc - Miscellaneous subroutines
29 use FS::Misc qw(send_email);
35 Miscellaneous subroutines. This module contains miscellaneous subroutines
36 called from multiple other modules. These are not OO or necessarily related,
37 but are collected here to elimiate code duplication.
43 =item send_email OPTION => VALUE ...
49 I<to> - (required) comma-separated scalar or arrayref of recipients
51 I<subject> - (required)
53 I<content-type> - (optional) MIME type for the body
55 I<body> - (required unless I<nobody> is true) arrayref of body text lines
57 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().
59 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,
60 I<content-type>, if specified, overrides the default "multipart/mixed" for the outermost MIME container.
62 I<content-encoding> - (optional) when using nobody, optional top-level MIME
63 encoding which, if specified, overrides the default "7bit".
65 I<type> - (optional) type parameter for multipart/related messages
72 use Mail::Internet 2.00;
76 FS::UID->install_callback( sub {
83 my %doptions = %options;
84 $doptions{'body'} = '(full body not shown in debug)';
85 warn "FS::Misc::send_email called with options:\n ". Dumper(\%doptions);
86 # join("\n", map { " $_: ". $options{$_} } keys %options ). "\n"
89 $ENV{MAILADDRESS} = $options{'from'};
90 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
94 if ( $options{'nobody'} ) {
96 croak "'mimeparts' option required when 'nobody' option given\n"
97 unless $options{'mimeparts'};
99 @mimeparts = @{$options{'mimeparts'}};
102 'Type' => ( $options{'content-type'} || 'multipart/mixed' ),
103 'Encoding' => ( $options{'content-encoding'} || '7bit' ),
108 @mimeparts = @{$options{'mimeparts'}}
109 if ref($options{'mimeparts'}) eq 'ARRAY';
111 if (scalar(@mimeparts)) {
114 'Type' => 'multipart/mixed',
115 'Encoding' => '7bit',
118 unshift @mimeparts, {
119 'Type' => ( $options{'content-type'} || 'text/plain' ),
120 'Data' => $options{'body'},
121 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
122 'Disposition' => 'inline',
128 'Type' => ( $options{'content-type'} || 'text/plain' ),
129 'Data' => $options{'body'},
130 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
138 if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
141 warn 'no domain found in invoice from address '. $options{'from'}.
142 '; constructing Message-ID @example.com';
143 $domain = 'example.com';
145 my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
147 my $message = MIME::Entity->build(
148 'From' => $options{'from'},
150 'Sender' => $options{'from'},
151 'Reply-To' => $options{'from'},
152 'Date' => time2str("%a, %d %b %Y %X %z", time),
153 'Subject' => $options{'subject'},
154 'Message-ID' => "<$message_id>",
158 if ( $options{'type'} ) {
159 #false laziness w/cust_bill::generate_email
160 $message->head->replace('Content-type',
162 '; boundary="'. $message->head->multipart_boundary. '"'.
163 '; type='. $options{'type'}
167 foreach my $part (@mimeparts) {
169 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
171 warn "attaching MIME part from MIME::Entity object\n"
173 $message->add_part($part);
175 } elsif ( ref($part) eq 'HASH' ) {
177 warn "attaching MIME part from hashref:\n".
178 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
180 $message->attach(%$part);
183 croak "mimepart $part isn't a hashref or MIME::Entity object!";
188 my $smtpmachine = $conf->config('smtpmachine');
191 $message->mysmtpsend( 'Host' => $smtpmachine,
192 'MailFrom' => $options{'from'},
197 #this kludges a "mysmtpsend" method into Mail::Internet for send_email above
198 #now updated for MailTools v2!
199 package Mail::Internet;
205 sub Mail::Internet::mysmtpsend($@) {
206 my ($self, %opt) = @_;
208 my $host = $opt{Host};
209 my $envelope = $opt{MailFrom}; # || mailaddress();
214 push @hello, Hello => $opt{Hello}
215 if defined $opt{Hello};
217 push @hello, Port => $opt{Port}
218 if exists $opt{Port};
220 push @hello, Debug => $opt{Debug}
221 if exists $opt{Debug};
224 # { local $SIG{__DIE__};
225 # my @hosts = qw(mailhost localhost);
226 # unshift @hosts, split /\:/, $ENV{SMTPHOSTS}
227 # if defined $ENV{SMTPHOSTS};
229 # foreach $host (@hosts)
230 # { $smtp = eval { Net::SMTP->new($host, @hello) };
231 # last if defined $smtp;
234 # elsif(ref($host) && UNIVERSAL::isa($host,'Net::SMTP'))
235 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP'))
240 { #local $SIG{__DIE__};
241 #$smtp = eval { Net::SMTP->new($host, @hello) };
242 $smtp = Net::SMTP->new($host, @hello);
245 unless ( defined($smtp) ) {
247 $err =~ s/Invalid argument/Unknown host/;
248 return "can't connect to $host: $err"
251 my $head = $self->cleaned_header_dup;
253 $head->delete('Bcc');
257 my @rcpt = map { ref $_ ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
258 @rcpt = map { $head->get($_) } qw(To Cc Bcc)
261 my @addr = map {$_->address} Mail::Address->parse(@rcpt);
263 return 'No valid destination addresses found!'
268 my $ok = $smtp->mail($envelope)
270 && $smtp->data(join("", @{$head->header}, "\n", @{$self->body}));
272 #$quit && $smtp->quit;
275 $quit && $smtp->quit;
278 return $smtp->code. ' '. $smtp->message;
284 =item send_fax OPTION => VALUE ...
288 I<dialstring> - (required) 10-digit phone number w/ area code
290 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
294 I<docfile> - (required) Filename of PostScript TIFF Class F document
296 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
305 die 'HylaFAX support has not been configured.'
306 unless $conf->exists('hylafax');
309 require Fax::Hylafax::Client;
313 if ($@ =~ /^Can't locate Fax.*/) {
314 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
320 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
322 die 'Called send_fax without a \'dialstring\'.'
323 unless exists($options{'dialstring'});
325 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
326 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
327 my $fh = new File::Temp(
328 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
331 ) or die "can't open temp file: $!\n";
333 $options{docfile} = $fh->filename;
335 print $fh @{$options{'docdata'}};
338 delete $options{'docdata'};
341 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
342 unless exists($options{'docfile'});
344 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
347 $options{'dialstring'} =~ s/[^\d\+]//g;
348 if ($options{'dialstring'} =~ /^\d{10}$/) {
349 $options{dialstring} = '+1' . $options{'dialstring'};
351 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
354 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
356 if ($faxjob->success) {
357 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
362 return 'Error while sending FAX: ' . $faxjob->trace;
367 =item states_hash COUNTRY
369 Returns a list of key/value pairs containing state (or other sub-country
370 division) abbriviations and names.
374 use FS::Record qw(qsearch);
375 use Locale::SubCountry;
382 map { s/[\n\r]//g; $_; }
386 'table' => 'cust_main_county',
387 'hashref' => { 'country' => $country },
388 'extra_sql' => 'GROUP BY state',
391 #it could throw a fatal "Invalid country code" error (for example "AX")
392 my $subcountry = eval { new Locale::SubCountry($country) }
393 or return ( '', '(n/a)' );
395 #"i see your schwartz is as big as mine!"
396 map { ( $_->[0] => $_->[1] ) }
397 sort { $a->[1] cmp $b->[1] }
398 map { [ $_ => state_label($_, $subcountry) ] }
402 =item counties STATE COUNTRY
404 Returns a list of counties for this state and country.
409 my( $state, $country ) = @_;
411 sort map { s/[\n\r]//g; $_; }
414 'select' => 'DISTINCT county',
415 'table' => 'cust_main_county',
416 'hashref' => { 'state' => $state,
417 'country' => $country,
422 =item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT
427 my( $state, $country ) = @_;
429 unless ( ref($country) ) {
430 $country = eval { new Locale::SubCountry($country) }
435 # US kludge to avoid changing existing behaviour
436 # also we actually *use* the abbriviations...
437 my $full_name = $country->country_code eq 'US'
439 : $country->full_name($state);
441 $full_name = '' if $full_name eq 'unknown';
442 $full_name =~ s/\(see also.*\)\s*$//;
443 $full_name .= " ($state)" if $full_name;
445 $full_name || $state || '(n/a)';
451 Returns a hash reference of the accepted credit card types. Keys are shorter
452 identifiers and values are the longer strings used by the system (see
453 L<Business::CreditCard>).
460 my $conf = new FS::Conf;
463 #displayname #value (Business::CreditCard)
464 "VISA" => "VISA card",
465 "MasterCard" => "MasterCard",
466 "Discover" => "Discover card",
467 "American Express" => "American Express card",
468 "Diner's Club/Carte Blanche" => "Diner's Club/Carte Blanche",
469 "enRoute" => "enRoute",
471 "BankCard" => "BankCard",
472 "Switch" => "Switch",
475 my @conf_card_types = grep { ! /^\s*$/ } $conf->config('card-types');
476 if ( @conf_card_types ) {
477 #perhaps the hash is backwards for this, but this way works better for
478 #usage in selfservice
479 %card_types = map { $_ => $card_types{$_} }
482 grep { $card_types{$d} eq $_ } @conf_card_types
490 =item generate_ps FILENAME
492 Returns an postscript rendition of the LaTex file, as a scalar.
493 FILENAME does not contain the .tex suffix and is unlinked by this function.
497 use String::ShellQuote;
502 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
507 system('dvips', '-q', '-t', 'letter', "$file.dvi", '-o', "$file.ps" ) == 0
508 or die "dvips failed";
510 open(POSTSCRIPT, "<$file.ps")
511 or die "can't open $file.ps: $! (error in LaTeX template?)\n";
513 unlink("$file.dvi", "$file.log", "$file.aux", "$file.ps", "$file.tex");
517 if ( $conf->exists('lpr-postscript_prefix') ) {
518 my $prefix = $conf->config('lpr-postscript_prefix');
519 $ps .= eval qq("$prefix");
522 while (<POSTSCRIPT>) {
528 if ( $conf->exists('lpr-postscript_suffix') ) {
529 my $suffix = $conf->config('lpr-postscript_suffix');
530 $ps .= eval qq("$suffix");
537 =item generate_pdf FILENAME
539 Returns an PDF rendition of the LaTex file, as a scalar. FILENAME does not
540 contain the .tex suffix and is unlinked by this function.
544 use String::ShellQuote;
549 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
552 #system('pdflatex', "$file.tex");
553 #system('pdflatex', "$file.tex");
554 #! LaTeX Error: Unknown graphics extension: .eps.
558 my $sfile = shell_quote $file;
560 #system('dvipdf', "$file.dvi", "$file.pdf" );
562 "dvips -q -t letter -f $sfile.dvi ".
563 "| gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$sfile.pdf ".
566 or die "dvips | gs failed: $!";
568 open(PDF, "<$file.pdf")
569 or die "can't open $file.pdf: $! (error in LaTeX template?)\n";
571 unlink("$file.dvi", "$file.log", "$file.aux", "$file.pdf", "$file.tex");
587 #my $sfile = shell_quote $file;
591 '-interaction=batchmode',
592 '\AtBeginDocument{\RequirePackage{pslatex}}',
593 '\def\PSLATEXTMP{\futurelet\PSLATEXTMP\PSLATEXTMPB}',
594 '\def\PSLATEXTMPB{\ifx\PSLATEXTMP\nonstopmode\else\input\fi}',
599 my $timeout = 30; #? should be more than enough
603 local($SIG{CHLD}) = sub {};
604 #run( \@cmd, '>'=>'/dev/null', '2>'=>'/dev/null', timeout($timeout) )
605 run( \@cmd, timeout($timeout) )
606 or die "pslatex $file.tex failed; see $file.log for details?\n";
614 Sends the lines in ARRAYREF to the printer.
621 my $lpr = $conf->config('lpr');
624 run3 $lpr, $data, \$outerr, \$outerr;
626 $outerr = ": $outerr" if length($outerr);
627 die "Error from $lpr (exit status ". ($?>>8). ")$outerr\n";
640 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
642 L<Fax::Hylafax::Client>