4 use vars qw ( @ISA @EXPORT_OK $DEBUG );
10 @EXPORT_OK = qw( send_email send_fax );
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 =item send_fax OPTION => VALUE ...
192 I<dialstring> - (required) 10-digit phone number w/ area code
194 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
198 I<docfile> - (required) Filename of PostScript TIFF Class F document
200 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
209 die 'HylaFAX support has not been configured.'
210 unless $conf->exists('hylafax');
213 require Fax::Hylafax::Client;
217 if ($@ =~ /^Can't locate Fax.*/) {
218 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
224 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
226 die 'Called send_fax without a \'dialstring\'.'
227 unless exists($options{'dialstring'});
229 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
230 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
231 my $fh = new File::Temp(
232 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
235 ) or die "can't open temp file: $!\n";
237 $options{docfile} = $fh->filename;
239 print $fh @{$options{'docdata'}};
242 delete $options{'docdata'};
245 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
246 unless exists($options{'docfile'});
248 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
251 $options{'dialstring'} =~ s/[^\d\+]//g;
252 if ($options{'dialstring'} =~ /^\d{10}$/) {
253 $options{dialstring} = '+1' . $options{'dialstring'};
255 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
258 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
260 if ($faxjob->success) {
261 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
266 return 'Error while sending FAX: ' . $faxjob->trace;
271 package Mail::Internet;
276 sub Mail::Internet::mysmtpsend {
279 my $host = $opt{Host};
280 my $envelope = $opt{MailFrom};
283 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
285 push(@hello, 'Port', $opt{'Port'})
286 if exists $opt{'Port'};
288 push(@hello, 'Debug', $opt{'Debug'})
289 if exists $opt{'Debug'};
291 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
296 #local $SIG{__DIE__};
297 #$smtp = eval { Net::SMTP->new($host, @hello) };
298 $smtp = new Net::SMTP $host, @hello;
301 unless ( defined($smtp) ) {
303 $err =~ s/Invalid argument/Unknown host/;
304 return "can't connect to $host: $err"
307 my $hdr = $src->head->dup;
313 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
314 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
316 my @addr = map($_->address, Mail::Address->parse(@rcpt));
318 return 'No valid destination addresses found!'
321 $hdr->delete('Bcc'); # Remove blind Cc's
325 #warn "Headers: \n" . join('',@{$hdr->header});
326 #warn "Body: \n" . join('',@{$src->body});
328 my $ok = $smtp->mail( $envelope ) &&
330 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
337 return $smtp->code. ' '. $smtp->message;
351 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
353 L<Fax::Hylafax::Client>