FAQ 9.20 How do I send mail?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


9.20: How do I send mail?

    Use the "sendmail" program directly:

        open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
                            or die "Can't fork for sendmail: $!\n";
        print SENDMAIL <<"EOF";
        Subject: A relevant subject line

        Body of the message goes here after the blank line
        in as many lines as you like.
        close(SENDMAIL)     or warn "sendmail didn't close nicely";

    The -oi option prevents sendmail from interpreting a line consisting of
    a single dot as "end of message". The -t option says to use the headers
    to decide who to send the message to, and -odq says to put the message
    into the queue. This last option means your message won't be immediately
    delivered, so leave it out if you want immediate delivery.

    Alternate, less convenient approaches include calling mail (sometimes
    called mailx) directly or simply opening up port 25 have having an
    intimate conversation between just you and the remote SMTP daemon,
    probably sendmail.

    Or you might be able use the CPAN module Mail::Mailer:

        use Mail::Mailer;

        $mailer = Mail::Mailer->new();
        $mailer->open({ From    => $from_address,
                        To      => $to_address,
                        Subject => $subject,
            or die "Can't open: $!\n";
        print $mailer $body;

    The Mail::Internet module uses Net::SMTP which is less Unix-centric than
    Mail::Mailer, but less reliable. Avoid raw SMTP commands. There are many
    reasons to use a mail transport agent like sendmail. These include
    queuing, MX records, and security.


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline