4 use vars qw ( @ISA @EXPORT_OK $DEBUG );
9 @EXPORT_OK = qw( send_email send_fax );
15 FS::Misc - Miscellaneous subroutines
19 use FS::Misc qw(send_email);
25 Miscellaneous subroutines. This module contains miscellaneous subroutines
26 called from multiple other modules. These are not OO or necessarily related,
27 but are collected here to elimiate code duplication.
33 =item send_email OPTION => VALUE ...
39 I<to> - (required) comma-separated scalar or arrayref of recipients
41 I<subject> - (required)
43 I<content-type> - (optional) MIME type for the body
45 I<body> - (required unless I<nobody> is true) arrayref of body text lines
47 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().
49 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,
50 I<content-type>, if specified, overrides the default "multipart/mixed" for the outermost MIME container.
52 I<content-encoding> - (optional) when using nobody, optional top-level MIME
53 encoding which, if specified, overrides the default "7bit".
55 I<type> - (optional) type parameter for multipart/related messages
62 use Mail::Internet 1.44;
66 FS::UID->install_callback( sub {
72 warn "FS::Misc::send_email called with options:\n ".
73 join("\n", map { " $_: ". $options{$_} } keys %options ). "\n"
76 $ENV{MAILADDRESS} = $options{'from'};
77 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
81 if ( $options{'nobody'} ) {
83 croak "'mimeparts' option required when 'nobody' option given\n"
84 unless $options{'mimeparts'};
86 @mimeparts = @{$options{'mimeparts'}};
89 'Type' => ( $options{'content-type'} || 'multipart/mixed' ),
90 'Encoding' => ( $options{'content-encoding'} || '7bit' ),
95 @mimeparts = @{$options{'mimeparts'}}
96 if ref($options{'mimeparts'}) eq 'ARRAY';
98 if (scalar(@mimeparts)) {
101 'Type' => 'multipart/mixed',
102 'Encoding' => '7bit',
105 unshift @mimeparts, {
106 'Type' => ( $options{'content-type'} || 'text/plain' ),
107 'Data' => $options{'body'},
108 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
109 'Disposition' => 'inline',
115 'Type' => ( $options{'content-type'} || 'text/plain' ),
116 'Data' => $options{'body'},
117 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
125 if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
128 warn 'no domain found in invoice from address '. $options{'from'}.
129 '; constructing Message-ID @example.com';
130 $domain = 'example.com';
132 my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
134 my $message = MIME::Entity->build(
135 'From' => $options{'from'},
137 'Sender' => $options{'from'},
138 'Reply-To' => $options{'from'},
139 'Date' => time2str("%a, %d %b %Y %X %z", time),
140 'Subject' => $options{'subject'},
141 'Message-ID' => "<$message_id>",
145 if ( $options{'type'} ) {
146 #false laziness w/cust_bill::generate_email
147 $message->head->replace('Content-type',
149 '; boundary="'. $message->head->multipart_boundary. '"'.
150 '; type='. $options{'type'}
154 foreach my $part (@mimeparts) {
156 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
158 warn "attaching MIME part from MIME::Entity object\n"
160 $message->add_part($part);
162 } elsif ( ref($part) eq 'HASH' ) {
164 warn "attaching MIME part from hashref:\n".
165 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
167 $message->attach(%$part);
170 croak "mimepart $part isn't a hashref or MIME::Entity object!";
175 my $smtpmachine = $conf->config('smtpmachine');
178 $message->mysmtpsend( 'Host' => $smtpmachine,
179 'MailFrom' => $options{'from'},
184 =item send_fax OPTION => VALUE ...
188 I<dialstring> - (required) 10-digit phone number w/ area code
190 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
194 I<docfile> - (required) Filename of PostScript TIFF Class F document
196 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
205 die 'HylaFAX support has not been configured.'
206 unless $conf->exists('hylafax');
209 require Fax::Hylafax::Client;
213 if ($@ =~ /^Can't locate Fax.*/) {
214 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
220 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
222 die 'Called send_fax without a \'dialstring\'.'
223 unless exists($options{'dialstring'});
225 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
226 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
227 my $fh = new File::Temp(
228 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
231 ) or die "can't open temp file: $!\n";
233 $options{docfile} = $fh->filename;
235 print $fh @{$options{'docdata'}};
238 delete $options{'docdata'};
241 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
242 unless exists($options{'docfile'});
244 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
247 $options{'dialstring'} =~ s/[^\d\+]//g;
248 if ($options{'dialstring'} =~ /^\d{10}$/) {
249 $options{dialstring} = '+1' . $options{'dialstring'};
251 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
254 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
256 if ($faxjob->success) {
257 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
262 return 'Error while sending FAX: ' . $faxjob->trace;
267 package Mail::Internet;
272 sub Mail::Internet::mysmtpsend {
275 my $host = $opt{Host};
276 my $envelope = $opt{MailFrom};
279 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
281 push(@hello, 'Port', $opt{'Port'})
282 if exists $opt{'Port'};
284 push(@hello, 'Debug', $opt{'Debug'})
285 if exists $opt{'Debug'};
287 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
292 #local $SIG{__DIE__};
293 #$smtp = eval { Net::SMTP->new($host, @hello) };
294 $smtp = new Net::SMTP $host, @hello;
297 unless ( defined($smtp) ) {
299 $err =~ s/Invalid argument/Unknown host/;
300 return "can't connect to $host: $err"
303 my $hdr = $src->head->dup;
309 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
310 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
312 my @addr = map($_->address, Mail::Address->parse(@rcpt));
314 return 'No valid destination addresses found!'
317 $hdr->delete('Bcc'); # Remove blind Cc's
321 #warn "Headers: \n" . join('',@{$hdr->header});
322 #warn "Body: \n" . join('',@{$src->body});
324 my $ok = $smtp->mail( $envelope ) &&
326 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
333 return $smtp->code. ' '. $smtp->message;
345 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
347 L<Fax::Hylafax::Client>