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;
53 use Fax::Hylafax::Client;
56 FS::UID->install_callback( sub {
63 $ENV{MAILADDRESS} = $options{'from'};
64 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
66 my @mimeparts = (ref($options{'mimeparts'}) eq 'ARRAY')
67 ? @{$options{'mimeparts'}} : ();
68 my $mimetype = (scalar(@mimeparts)) ? 'multipart/mixed' : 'text/plain';
71 if (scalar(@mimeparts)) {
73 'Type' => 'multipart/mixed',
78 'Data' => $options{'body'},
79 'Disposition' => 'inline',
80 'Type' => (($options{'content-type'} ne '')
81 ? $options{'content-type'} : 'text/plain'),
85 'Type' => (($options{'content-type'} ne '')
86 ? $options{'content-type'} : 'text/plain'),
87 'Data' => $options{'body'},
91 my $message = MIME::Entity->build(
92 'From' => $options{'from'},
94 'Sender' => $options{'from'},
95 'Reply-To' => $options{'from'},
96 'Date' => time2str("%a, %d %b %Y %X %z", time),
97 'Subject' => $options{'subject'},
101 foreach my $part (@mimeparts) {
102 next unless ref($part) eq 'HASH'; #warn?
103 $message->attach(%$part);
106 my $smtpmachine = $conf->config('smtpmachine');
109 $message->mysmtpsend( 'Host' => $smtpmachine,
110 'MailFrom' => $options{'from'},
115 =item send_fax OPTION => VALUE ...
119 I<dialstring> - (required) 10-digit phone number w/ area code
121 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
125 I<docfile> - (required) Filename of PostScript TIFF Class F document
127 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
136 die 'HylaFAX support has not been configured.'
137 unless $conf->exists('hylafax');
139 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
141 die 'Called send_fax without a \'dialstring\'.'
142 unless exists($options{'dialstring'});
144 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
145 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
146 my $fh = new File::Temp(
147 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
150 ) or die "can't open temp file: $!\n";
152 $options{docfile} = $fh->filename;
154 print $fh @{$options{'docdata'}};
157 delete $options{'docdata'};
160 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
161 unless exists($options{'docfile'});
163 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
166 $options{'dialstring'} =~ s/[^\d\+]//g;
167 if ($options{'dialstring'} =~ /^\d{10}$/) {
168 $options{dialstring} = '+1' . $options{'dialstring'};
170 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
173 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
175 if ($faxjob->success) {
176 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
179 return 'Error while sending FAX: ' . $faxjob->trace;
186 package Mail::Internet;
191 sub Mail::Internet::mysmtpsend {
194 my $host = $opt{Host};
195 my $envelope = $opt{MailFrom};
198 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
200 push(@hello, 'Port', $opt{'Port'})
201 if exists $opt{'Port'};
203 push(@hello, 'Debug', $opt{'Debug'})
204 if exists $opt{'Debug'};
206 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
211 #local $SIG{__DIE__};
212 #$smtp = eval { Net::SMTP->new($host, @hello) };
213 $smtp = new Net::SMTP $host, @hello;
216 unless ( defined($smtp) ) {
218 $err =~ s/Invalid argument/Unknown host/;
219 return "can't connect to $host: $err"
222 my $hdr = $src->head->dup;
228 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
229 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
231 my @addr = map($_->address, Mail::Address->parse(@rcpt));
233 return 'No valid destination addresses found!'
236 $hdr->delete('Bcc'); # Remove blind Cc's
240 #warn "Headers: \n" . join('',@{$hdr->header});
241 #warn "Body: \n" . join('',@{$src->body});
243 my $ok = $smtp->mail( $envelope ) &&
245 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
252 return $smtp->code. ' '. $smtp->message;
264 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
266 L<Fax::Hylafax::Client>