4 use vars qw ( @ISA @EXPORT_OK $DEBUG );
10 @EXPORT_OK = qw( send_email send_fax states_hash state_label );
16 FS::Misc - Miscellaneous subroutines
20 use FS::Misc qw(send_email);
26 Miscellaneous subroutines. This module contains miscellaneous subroutines
27 called from multiple other modules. These are not OO or necessarily related,
28 but are collected here to elimiate code duplication.
34 =item send_email OPTION => VALUE ...
40 I<to> - (required) comma-separated scalar or arrayref of recipients
42 I<subject> - (required)
44 I<content-type> - (optional) MIME type for the body
46 I<body> - (required unless I<nobody> is true) arrayref of body text lines
48 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().
50 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,
51 I<content-type>, if specified, overrides the default "multipart/mixed" for the outermost MIME container.
53 I<content-encoding> - (optional) when using nobody, optional top-level MIME
54 encoding which, if specified, overrides the default "7bit".
56 I<type> - (optional) type parameter for multipart/related messages
63 use Mail::Internet 1.44;
67 FS::UID->install_callback( sub {
74 my %doptions = %options;
75 $doptions{'body'} = '(full body not shown in debug)';
76 warn "FS::Misc::send_email called with options:\n ". Dumper(\%doptions);
77 # join("\n", map { " $_: ". $options{$_} } keys %options ). "\n"
80 $ENV{MAILADDRESS} = $options{'from'};
81 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
85 if ( $options{'nobody'} ) {
87 croak "'mimeparts' option required when 'nobody' option given\n"
88 unless $options{'mimeparts'};
90 @mimeparts = @{$options{'mimeparts'}};
93 'Type' => ( $options{'content-type'} || 'multipart/mixed' ),
94 'Encoding' => ( $options{'content-encoding'} || '7bit' ),
99 @mimeparts = @{$options{'mimeparts'}}
100 if ref($options{'mimeparts'}) eq 'ARRAY';
102 if (scalar(@mimeparts)) {
105 'Type' => 'multipart/mixed',
106 'Encoding' => '7bit',
109 unshift @mimeparts, {
110 'Type' => ( $options{'content-type'} || 'text/plain' ),
111 'Data' => $options{'body'},
112 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
113 'Disposition' => 'inline',
119 'Type' => ( $options{'content-type'} || 'text/plain' ),
120 'Data' => $options{'body'},
121 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
129 if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
132 warn 'no domain found in invoice from address '. $options{'from'}.
133 '; constructing Message-ID @example.com';
134 $domain = 'example.com';
136 my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
138 my $message = MIME::Entity->build(
139 'From' => $options{'from'},
141 'Sender' => $options{'from'},
142 'Reply-To' => $options{'from'},
143 'Date' => time2str("%a, %d %b %Y %X %z", time),
144 'Subject' => $options{'subject'},
145 'Message-ID' => "<$message_id>",
149 if ( $options{'type'} ) {
150 #false laziness w/cust_bill::generate_email
151 $message->head->replace('Content-type',
153 '; boundary="'. $message->head->multipart_boundary. '"'.
154 '; type='. $options{'type'}
158 foreach my $part (@mimeparts) {
160 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
162 warn "attaching MIME part from MIME::Entity object\n"
164 $message->add_part($part);
166 } elsif ( ref($part) eq 'HASH' ) {
168 warn "attaching MIME part from hashref:\n".
169 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
171 $message->attach(%$part);
174 croak "mimepart $part isn't a hashref or MIME::Entity object!";
179 my $smtpmachine = $conf->config('smtpmachine');
182 $message->mysmtpsend( 'Host' => $smtpmachine,
183 'MailFrom' => $options{'from'},
188 #this kludges a "mysmtpsend" method into Mail::Internet for send_email above
189 package Mail::Internet;
194 sub Mail::Internet::mysmtpsend {
197 my $host = $opt{Host};
198 my $envelope = $opt{MailFrom};
201 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
203 push(@hello, 'Port', $opt{'Port'})
204 if exists $opt{'Port'};
206 push(@hello, 'Debug', $opt{'Debug'})
207 if exists $opt{'Debug'};
209 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
214 #local $SIG{__DIE__};
215 #$smtp = eval { Net::SMTP->new($host, @hello) };
216 $smtp = new Net::SMTP $host, @hello;
219 unless ( defined($smtp) ) {
221 $err =~ s/Invalid argument/Unknown host/;
222 return "can't connect to $host: $err"
225 my $hdr = $src->head->dup;
231 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
232 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
234 my @addr = map($_->address, Mail::Address->parse(@rcpt));
236 return 'No valid destination addresses found!'
239 $hdr->delete('Bcc'); # Remove blind Cc's
243 #warn "Headers: \n" . join('',@{$hdr->header});
244 #warn "Body: \n" . join('',@{$src->body});
246 my $ok = $smtp->mail( $envelope ) &&
248 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
255 return $smtp->code. ' '. $smtp->message;
262 =item send_fax OPTION => VALUE ...
266 I<dialstring> - (required) 10-digit phone number w/ area code
268 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
272 I<docfile> - (required) Filename of PostScript TIFF Class F document
274 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
283 die 'HylaFAX support has not been configured.'
284 unless $conf->exists('hylafax');
287 require Fax::Hylafax::Client;
291 if ($@ =~ /^Can't locate Fax.*/) {
292 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
298 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
300 die 'Called send_fax without a \'dialstring\'.'
301 unless exists($options{'dialstring'});
303 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
304 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
305 my $fh = new File::Temp(
306 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
309 ) or die "can't open temp file: $!\n";
311 $options{docfile} = $fh->filename;
313 print $fh @{$options{'docdata'}};
316 delete $options{'docdata'};
319 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
320 unless exists($options{'docfile'});
322 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
325 $options{'dialstring'} =~ s/[^\d\+]//g;
326 if ($options{'dialstring'} =~ /^\d{10}$/) {
327 $options{dialstring} = '+1' . $options{'dialstring'};
329 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
332 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
334 if ($faxjob->success) {
335 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
340 return 'Error while sending FAX: ' . $faxjob->trace;
345 =item states_hash COUNTRY
347 Returns a list of key/value pairs containing state (or other sub-country
348 division) abbriviations and names.
352 use FS::Record qw(qsearch);
353 use Locale::SubCountry;
360 map { s/[\n\r]//g; $_; }
364 'table' => 'cust_main_county',
365 'hashref' => { 'country' => $country },
366 'extra_sql' => 'GROUP BY state',
370 #it could throw a fatal "Invalid country code" error (for example "AX")
371 my $subcountry = eval { new Locale::SubCountry($country) }
372 or return ( '', '(n/a)' );
374 #"i see your schwartz is as big as mine!"
375 map { ( $_->[0] => $_->[1] ) }
376 sort { $a->[1] cmp $b->[1] }
377 map { [ $_ => state_label($_, $subcountry) ] }
381 =item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT
386 my( $state, $country ) = @_;
388 unless ( ref($country) ) {
389 $country = eval { new Locale::SubCountry($country) }
394 # US kludge to avoid changing existing behaviour
395 # also we actually *use* the abbriviations...
396 my $full_name = $country->country_code eq 'US'
398 : $country->full_name($state);
400 $full_name = '' if $full_name eq 'unknown';
401 $full_name =~ s/\(see also.*\)\s*$//;
402 $full_name .= " ($state)" if $full_name;
404 $full_name || $state || '(n/a)';
416 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
418 L<Fax::Hylafax::Client>