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' ),
121 $options{'from'} =~ /\@([\w\.\-]+)/ or $1 = 'example.com';
122 my $message_id = join('.', rand()*(2**32), $$, time). "\@$1";
124 my $message = MIME::Entity->build(
125 'From' => $options{'from'},
127 'Sender' => $options{'from'},
128 'Reply-To' => $options{'from'},
129 'Date' => time2str("%a, %d %b %Y %X %z", time),
130 'Subject' => $options{'subject'},
131 'Message-ID' => "<$message_id>",
135 if ( $options{'type'} ) {
136 #false laziness w/cust_bill::generate_email
137 $message->head->replace('Content-type',
139 '; boundary="'. $message->head->multipart_boundary. '"'.
140 '; type='. $options{'type'}
144 foreach my $part (@mimeparts) {
146 if ( UNIVERSAL::isa($part, 'MIME::Entity') ) {
148 warn "attaching MIME part from MIME::Entity object\n"
150 $message->add_part($part);
152 } elsif ( ref($part) eq 'HASH' ) {
154 warn "attaching MIME part from hashref:\n".
155 join("\n", map " $_: ".$part->{$_}, keys %$part ). "\n"
157 $message->attach(%$part);
160 croak "mimepart $part isn't a hashref or MIME::Entity object!";
165 my $smtpmachine = $conf->config('smtpmachine');
168 $message->mysmtpsend( 'Host' => $smtpmachine,
169 'MailFrom' => $options{'from'},
174 =item send_fax OPTION => VALUE ...
178 I<dialstring> - (required) 10-digit phone number w/ area code
180 I<docdata> - (required) Array ref containing PostScript or TIFF Class F document
184 I<docfile> - (required) Filename of PostScript TIFF Class F document
186 ...any other options will be passed to L<Fax::Hylafax::Client::sendfax>
195 die 'HylaFAX support has not been configured.'
196 unless $conf->exists('hylafax');
199 require Fax::Hylafax::Client;
203 if ($@ =~ /^Can't locate Fax.*/) {
204 die "You must have Fax::Hylafax::Client installed to use invoice faxing."
210 my %hylafax_opts = map { split /\s+/ } $conf->config('hylafax');
212 die 'Called send_fax without a \'dialstring\'.'
213 unless exists($options{'dialstring'});
215 if (exists($options{'docdata'}) and ref($options{'docdata'}) eq 'ARRAY') {
216 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
217 my $fh = new File::Temp(
218 TEMPLATE => 'faxdoc.'. $options{'dialstring'} . '.XXXXXXXX',
221 ) or die "can't open temp file: $!\n";
223 $options{docfile} = $fh->filename;
225 print $fh @{$options{'docdata'}};
228 delete $options{'docdata'};
231 die 'Called send_fax without a \'docfile\' or \'docdata\'.'
232 unless exists($options{'docfile'});
234 #FIXME: Need to send canonical dialstring to HylaFAX, but this only
237 $options{'dialstring'} =~ s/[^\d\+]//g;
238 if ($options{'dialstring'} =~ /^\d{10}$/) {
239 $options{dialstring} = '+1' . $options{'dialstring'};
241 return 'Invalid dialstring ' . $options{'dialstring'} . '.';
244 my $faxjob = &Fax::Hylafax::Client::sendfax(%options, %hylafax_opts);
246 if ($faxjob->success) {
247 warn "Successfully queued fax to '$options{dialstring}' with jobid " .
250 return 'Error while sending FAX: ' . $faxjob->trace;
257 package Mail::Internet;
262 sub Mail::Internet::mysmtpsend {
265 my $host = $opt{Host};
266 my $envelope = $opt{MailFrom};
269 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
271 push(@hello, 'Port', $opt{'Port'})
272 if exists $opt{'Port'};
274 push(@hello, 'Debug', $opt{'Debug'})
275 if exists $opt{'Debug'};
277 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
282 #local $SIG{__DIE__};
283 #$smtp = eval { Net::SMTP->new($host, @hello) };
284 $smtp = new Net::SMTP $host, @hello;
287 unless ( defined($smtp) ) {
289 $err =~ s/Invalid argument/Unknown host/;
290 return "can't connect to $host: $err"
293 my $hdr = $src->head->dup;
299 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
300 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
302 my @addr = map($_->address, Mail::Address->parse(@rcpt));
304 return 'No valid destination addresses found!'
307 $hdr->delete('Bcc'); # Remove blind Cc's
311 #warn "Headers: \n" . join('',@{$hdr->header});
312 #warn "Body: \n" . join('',@{$src->body});
314 my $ok = $smtp->mail( $envelope ) &&
316 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
323 return $smtp->code. ' '. $smtp->message;
335 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.
337 L<Fax::Hylafax::Client>