4 use vars qw ( @ISA @EXPORT_OK );
8 @EXPORT_OK = qw( send_email );
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
49 use Mail::Internet 1.44;
52 FS::UID->install_callback( sub {
59 $ENV{MAILADDRESS} = $options{'from'};
60 my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
62 'From: '. $options{'from'},
64 'Sender: '. $options{'from'},
65 'Reply-To: '. $options{'from'},
66 'Date: '. time2str("%a, %d %b %Y %X %z", time),
67 'Subject: '. $options{'subject'},
69 push @header, 'Content-Type: '. $options{'content-type'}
70 if exists($options{'content-type'});
71 my $header = new Mail::Header ( \@header );
73 my $message = new Mail::Internet (
75 'Body' => $options{'body'},
78 my $smtpmachine = $conf->config('smtpmachine');
81 $message->mysmtpsend( 'Host' => $smtpmachine,
82 'MailFrom' => $options{'from'},
87 package Mail::Internet;
92 sub Mail::Internet::mysmtpsend {
95 my $host = $opt{Host};
96 my $envelope = $opt{MailFrom};
99 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
101 push(@hello, 'Port', $opt{'Port'})
102 if exists $opt{'Port'};
104 push(@hello, 'Debug', $opt{'Debug'})
105 if exists $opt{'Debug'};
107 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
112 #local $SIG{__DIE__};
113 #$smtp = eval { Net::SMTP->new($host, @hello) };
114 $smtp = new Net::SMTP $host, @hello;
117 unless ( defined($smtp) ) {
119 $err =~ s/Invalid argument/Unknown host/;
120 return "can't connect to $host: $err"
123 my $hdr = $src->head->dup;
129 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
130 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
132 my @addr = map($_->address, Mail::Address->parse(@rcpt));
134 return 'No valid destination addresses found!'
137 $hdr->delete('Bcc'); # Remove blind Cc's
141 my $ok = $smtp->mail( $envelope ) &&
143 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
150 return $smtp->code. ' '. $smtp->message;
162 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.