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
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 package Mail::Internet;
119 sub Mail::Internet::mysmtpsend {
122 my $host = $opt{Host};
123 my $envelope = $opt{MailFrom};
126 my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
128 push(@hello, 'Port', $opt{'Port'})
129 if exists $opt{'Port'};
131 push(@hello, 'Debug', $opt{'Debug'})
132 if exists $opt{'Debug'};
134 if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
139 #local $SIG{__DIE__};
140 #$smtp = eval { Net::SMTP->new($host, @hello) };
141 $smtp = new Net::SMTP $host, @hello;
144 unless ( defined($smtp) ) {
146 $err =~ s/Invalid argument/Unknown host/;
147 return "can't connect to $host: $err"
150 my $hdr = $src->head->dup;
156 my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
157 @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
159 my @addr = map($_->address, Mail::Address->parse(@rcpt));
161 return 'No valid destination addresses found!'
164 $hdr->delete('Bcc'); # Remove blind Cc's
168 #warn "Headers: \n" . join('',@{$hdr->header});
169 #warn "Body: \n" . join('',@{$src->body});
171 my $ok = $smtp->mail( $envelope ) &&
173 $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
180 return $smtp->code. ' '. $smtp->message;
192 L<FS::UID>, L<FS::CGI>, L<FS::Record>, the base documentation.