4 use vars qw ( @ISA @EXPORT_OK );
8 @EXPORT_OK = qw( send_email send_fax );
12 FS::Misc - Miscellaneous subroutines
16 use FS::Misc qw(send_email);
22 Miscellaneous subroutines. This module contains miscellaneous subroutines
23 called from multiple other modules. These are not OO or necessarily related,
24 but are collected here to elimiate code duplication.
30 =item send_email OPTION => VALUE ...
36 I<to> - (required) comma-separated scalar or arrayref of recipients
38 I<subject> - (required)
40 I<content-type> - (optional) MIME type
42 I<body> - (required) arrayref of body text lines
44 I<mimeparts> - (optional) arrayref of MIME::Entity->build PARAMHASH refs, not MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach().
51 use Mail::Internet 1.44;
55 FS::UID->install_callback( sub {
62 $ENV{MAILADDRESS} = $options{'from'};
63 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
65 my @mimeparts = (ref($options{'mimeparts'}) eq 'ARRAY')
66 ? @{$options{'mimeparts'}} : ();
67 my $mimetype = (scalar(@mimeparts)) ? 'multipart/mixed' : 'text/plain';
70 if (scalar(@mimeparts)) {
72 'Type' => 'multipart/mixed',
77 'Data' => $options{'body'},
78 'Disposition' => 'inline',
79 'Type' => (($options{'content-type'} ne '')
80 ? $options{'content-type'} : 'text/plain'),
84 'Type' => (($options{'content-type'} ne '')
85 ? $options{'content-type'} : 'text/plain'),
86 'Data' => $options{'body'},
90 my $message = MIME::Entity->build(
91 'From' => $options{'from'},
93 'Sender' => $options{'from'},
94 'Reply-To' => $options{'from'},
95 'Date' => time2str("%a, %d %b %Y %X %z", time),
96 'Subject' => $options{'subject'},
100 foreach my $part (@mimeparts) {
101 next unless ref($part) eq 'HASH'; #warn?
102 $message->attach(%$part);
105 my $smtpmachine = $conf->config('smtpmachine');
108 $message->mysmtpsend( 'Host' => $smtpmachine,
109 'MailFrom' => $options{'from'},
114 =item send_fax OPTION => VALUE ...
118 I<dialstring> - (required) 10-digit phone number w/ area code
120 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
124 I<docfile> - (required) Filename of PostScript TIFF Class F document
126 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
135 die 'HylaFAX support has not been configured.'
136 unless $conf->exists('hylafax');
139 require Fax::Hylafax::Client;
143 if ($@ =~ /^Can't locate Fax.*/) {
144 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
150 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
152 die 'Called send_fax without a \'dialstring\'.'
153 unless exists($options{'dialstring'});
155 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
156 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
157 my $fh = new File::Temp(
158 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
161 ) or die "can't open temp file: $!\n";
163 $options{docfile} = $fh->filename;
165 print $fh @{$options{'docdata'}};
168 delete $options{'docdata'};
171 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
172 unless exists($options{'docfile'});
174 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
177 $options{'dialstring'} =~ s/[^\d\+]//g;
178 if ($options{'dialstring'} =~ /^\d{10}$/) {
179 $options{dialstring} = '+1' . $options{'dialstring'};
181 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
184 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
186 if ($faxjob->success) {
187 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
190 return 'Error while sending FAX: ' . $faxjob->trace;
197 package Mail::Internet;
202 sub Mail::Internet::mysmtpsend {
205 my $host = $opt{Host};
206 my $envelope = $opt{MailFrom};
209 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
211 push(@hello, 'Port', $opt{'Port'})
212 if exists $opt{'Port'};
214 push(@hello, 'Debug', $opt{'Debug'})
215 if exists $opt{'Debug'};
217 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
222 #local $SIG{__DIE__};
223 #$smtp = eval { Net::SMTP->new($host, @hello) };
224 $smtp = new Net::SMTP $host, @hello;
227 unless ( defined($smtp) ) {
229 $err =~ s/Invalid argument/Unknown host/;
230 return "can't connect to $host: $err"
233 my $hdr = $src->head->dup;
239 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
240 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
242 my @addr = map($_->address, Mail::Address->parse(@rcpt));
244 return 'No valid destination addresses found!'
247 $hdr->delete('Bcc'); # Remove blind Cc's
251 #warn "Headers: \n" . join('',@{$hdr->header});
252 #warn "Body: \n" . join('',@{$src->body});
254 my $ok = $smtp->mail( $envelope ) &&
256 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
263 return $smtp->code. ' '. $smtp->message;
275 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
277 L<Fax::Hylafax::Client>