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 {
73 $ENV{MAILADDRESS} = $options{'from'};
74 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
78 if ( $options{'nobody'} ) {
80 croak "'mimeparts' option required when 'nobody' option given\n"
81 unless $options{'mimeparts'};
83 @mimeparts = @{$options{'mimeparts'}};
86 'Type' => ( $options{'content-type'} || 'multipart/mixed' ),
87 'Encoding' => ( $options{'content-encoding'} || '7bit' ),
92 @mimeparts = @{$options{'mimeparts'}}
93 if ref($options{'mimeparts'}) eq 'ARRAY';
95 if (scalar(@mimeparts)) {
98 'Type' => 'multipart/mixed',
102 unshift @mimeparts, {
103 'Type' => ( $options{'content-type'} || 'text/plain' ),
104 'Data' => $options{'body'},
105 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
106 'Disposition' => 'inline',
112 'Type' => ( $options{'content-type'} || 'text/plain' ),
113 'Data' => $options{'body'},
114 'Encoding' => ( $options{'content-type'} ? '-SUGGEST' : '7bit' ),
122 if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
125 $domain = 'example.com';
127 my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
129 my $message = MIME::Entity->build(
130 'From' => $options{'from'},
132 'Sender' => $options{'from'},
133 'Reply-To' => $options{'from'},
134 'Date' => time2str("%a, %d %b %Y %X %z", time),
135 'Subject' => $options{'subject'},
136 'Message-ID' => "<$message_id>",
140 if ( $options{'type'} ) {
141 #false laziness w/cust_bill::generate_email
142 $message->head->replace('Content-type',
144 '; boundary="'. $message->head->multipart_boundary. '"'.
145 '; type='. $options{'type'}
149 foreach my $part (@mimeparts) {
151 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
153 warn "attaching MIME part from MIME::Entity object\n"
155 $message->add_part($part);
157 } elsif ( ref($part) eq 'HASH' ) {
159 warn "attaching MIME part from hashref:\n".
160 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
162 $message->attach(%$part);
165 croak "mimepart $part isn't a hashref or MIME::Entity object!";
170 my $smtpmachine = $conf->config('smtpmachine');
173 $message->mysmtpsend( 'Host' => $smtpmachine,
174 'MailFrom' => $options{'from'},
179 =item send_fax OPTION => VALUE ...
183 I<dialstring> - (required) 10-digit phone number w/ area code
185 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
189 I<docfile> - (required) Filename of PostScript TIFF Class F document
191 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
200 die 'HylaFAX support has not been configured.'
201 unless $conf->exists('hylafax');
204 require Fax::Hylafax::Client;
208 if ($@ =~ /^Can't locate Fax.*/) {
209 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
215 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
217 die 'Called send_fax without a \'dialstring\'.'
218 unless exists($options{'dialstring'});
220 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
221 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
222 my $fh = new File::Temp(
223 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
226 ) or die "can't open temp file: $!\n";
228 $options{docfile} = $fh->filename;
230 print $fh @{$options{'docdata'}};
233 delete $options{'docdata'};
236 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
237 unless exists($options{'docfile'});
239 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
242 $options{'dialstring'} =~ s/[^\d\+]//g;
243 if ($options{'dialstring'} =~ /^\d{10}$/) {
244 $options{dialstring} = '+1' . $options{'dialstring'};
246 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
249 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
251 if ($faxjob->success) {
252 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
257 return 'Error while sending FAX: ' . $faxjob->trace;
262 package Mail::Internet;
267 sub Mail::Internet::mysmtpsend {
270 my $host = $opt{Host};
271 my $envelope = $opt{MailFrom};
274 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
276 push(@hello, 'Port', $opt{'Port'})
277 if exists $opt{'Port'};
279 push(@hello, 'Debug', $opt{'Debug'})
280 if exists $opt{'Debug'};
282 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
287 #local $SIG{__DIE__};
288 #$smtp = eval { Net::SMTP->new($host, @hello) };
289 $smtp = new Net::SMTP $host, @hello;
292 unless ( defined($smtp) ) {
294 $err =~ s/Invalid argument/Unknown host/;
295 return "can't connect to $host: $err"
298 my $hdr = $src->head->dup;
304 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
305 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
307 my @addr = map($_->address, Mail::Address->parse(@rcpt));
309 return 'No valid destination addresses found!'
312 $hdr->delete('Bcc'); # Remove blind Cc's
316 #warn "Headers: \n" . join('',@{$hdr->header});
317 #warn "Body: \n" . join('',@{$src->body});
319 my $ok = $smtp->mail( $envelope ) &&
321 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
328 return $smtp->code. ' '. $smtp->message;
340 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
342 L<Fax::Hylafax::Client>