<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2001, 2002 Benjamin Trott. This code cannot be redistributed without
# permission from www.movabletype.org.
#
# $Id: Mail.pm,v 1.12 2002/10/30 00:56:12 btrott Exp $

package MT::Mail;
use strict;

use MT;
use MT::ConfigMgr;
use MT::ErrorHandler;
@MT::Mail::ISA = qw( MT::ErrorHandler );

sub send {
    my $class = shift;
    my($hdrs, $body) = @_;
    $body .= &lt;&lt;SIG;

-- 
@{[ MT-&gt;version_slug ]}
SIG
    my $mgr = MT::ConfigMgr-&gt;instance;
    my $xfer = $mgr-&gt;MailTransfer;
    if ($xfer eq 'sendmail') {
        return $class-&gt;_send_mt_sendmail($hdrs, $body, $mgr);
    } elsif ($xfer eq 'smtp') {
        return $class-&gt;_send_mt_smtp($hdrs, $body, $mgr);
    } elsif ($xfer eq 'debug') {
        return $class-&gt;_send_mt_debug($hdrs, $body, $mgr);
    } else {
        return $class-&gt;error(MT-&gt;translate(
            "Unknown MailTransfer method '[_1]'", $xfer ));
    }
}

sub _send_mt_debug {
    my $class = shift;
    my($hdrs, $body, $mgr) = @_;
    for my $key (keys %$hdrs) {
        my @arr = ref($hdrs-&gt;{$key}) eq 'ARRAY' ?
            @{ $hdrs-&gt;{$key} } : ($hdrs-&gt;{$key});
        print STDERR map "$key: $_\n", @arr;
    }
    print STDERR "\n";
    print STDERR $body;
    1;
}

sub _send_mt_smtp {
    my $class = shift;
    my($hdrs, $body, $mgr) = @_;
    eval { require Mail::Sendmail; };
    return $class-&gt;error(MT-&gt;translate(
        "Sending mail via SMTP requires that your server " .
        "have Mail::Sendmail installed: [_1]", $@ )) if $@;
    my %hdrs = %$hdrs;
    $hdrs{Message} = $body;
    $hdrs{Smtp} = $mgr-&gt;SMTPServer;
    for my $h (qw( Cc Bcc )) {
        if ($hdrs{$h}) {
            $hdrs{$h} = join ', ', @{ $hdrs{$h} };
        }
    }
    my $ret = Mail::Sendmail::sendmail(%hdrs);
    $ret or return $class-&gt;error(MT-&gt;translate(
        "Error sending mail: [_1]", $Mail::Sendmail::error ));
    1;
}

my @Sendmail = qw( /usr/lib/sendmail /usr/sbin/sendmail /usr/ucblib/sendmail );
sub _send_mt_sendmail {
    my $class = shift;
    my($hdrs, $body, $mgr) = @_;
    my $sm_loc;
    for my $loc ($mgr-&gt;SendMailPath, @Sendmail) {
        next unless $loc;
        $sm_loc = $loc, last if -x $loc &amp;&amp; !-d $loc;
    }
    return $class-&gt;error(MT-&gt;translate(
        "You do not have a valid path to sendmail on your machine. " .
        "Perhaps you should try using SMTP?" ))
        unless $sm_loc;
    local $SIG{PIPE} = { };
    my $pid = open MAIL, '|-';
    local $SIG{ALRM} = sub { CORE::exit() };
    return unless defined $pid;
    if (!$pid) {
        exec $sm_loc, "-t" or
            return $class-&gt;error(MT-&gt;translate(
                "Exec of sendmail failed: [_1]", "$!" ));
    }
    for my $key (keys %$hdrs) {
        my @arr = ref($hdrs-&gt;{$key}) eq 'ARRAY' ?
            @{ $hdrs-&gt;{$key} } : ($hdrs-&gt;{$key});
        print MAIL map "$key: $_\n", @arr;
    }
    print MAIL "\n";
    print MAIL $body;
    close MAIL;
    1;
}

1;
__END__

=head1 NAME

MT::Mail - Movable Type mail sender

=head1 SYNOPSIS

    use MT::Mail;
    my %head = ( To =&gt; 'foo@bar.com', Subject =&gt; 'My Subject' );
    my $body = 'This is the body of the message.';
    MT::Mail-&gt;send(\%head, $body)
        or die MT::Mail-&gt;errstr;

=head1 DESCRIPTION

I&lt;MT::Mail&gt; is the Movable Type mail-sending interface. It can send mail
through I&lt;sendmail&gt; (in several different default locations), through SMTP,
or through a debugging interface that writes data to STDERR. You can set the
method of sending mail through the F&lt;mt.cfg&gt; file by changing the value for
the I&lt;MailTransfer&gt; setting to one of the following values: C&lt;sendmail&gt;,
C&lt;smtp&gt;, or C&lt;debug&gt;.

If you are using C&lt;sendmail&gt;, I&lt;MT::Mail::send&gt; looks for your I&lt;sendmail&gt;
program in any of the following locations: F&lt;/usr/lib/sendmail&gt;,
F&lt;/usr/sbin/sendmail&gt;, and F&lt;/usr/ucblib/sendmail&gt;. If your I&lt;sendmail&gt; is at
a different location, you can set it using the I&lt;SendMailPath&gt; directive.

If you are using C&lt;smtp&gt;, I&lt;MT::Mail::send&gt; will by default use C&lt;localhost&gt;
as the SMTP server. You can change this by setting the I&lt;SMTPServer&gt;
directive.

=head1 USAGE

=head2 MT::Mail-&gt;send(\%headers, $body)

Sends a mail message with the headers I&lt;\%headers&gt; and the message body
I&lt;$body&gt;.

The keys and values in I&lt;\%headers&gt; are passed directly in to the mail
program or server, so you can use any valid mail header names as keys. If
you need to supply a list of header values, specify the hash value as a
reference to a list of the header values. For example:

    %headers = ( Bcc =&gt; [ 'foo@bar.com', 'baz@quux.com' ] );

If you wish the lines in I&lt;$body&gt; to be wrapped, you should do this yourself;
it will not be done by I&lt;send&gt;.

On success, I&lt;send&gt; returns true; on failure, it returns C&lt;undef&gt;, and the
error message is in C&lt;MT::Mail-E&lt;gt&gt;errstr&gt;.

=head1 AUTHOR &amp; COPYRIGHT

Please see the I&lt;MT&gt; manpage for author, copyright, and license information.

=cut
</pre></body></html>