FAQ 9.20 How do I send mail?

Discussion in 'Perl Misc' started by PerlFAQ Server, Apr 5, 2011.

  1. This is an excerpt from the latest version perlfaq9.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    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";
    From: User Originating Mail <me\@host>
    To: Final Destination <you\@otherhost>
    Subject: A relevant subject line

    Body of the message goes here after the blank line
    in as many lines as you like.
    EOF
    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;
    $mailer->close();

    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.



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
    PerlFAQ Server, Apr 5, 2011
    #1
    1. Advertising

  2. On 05 Apr 2011 PerlFAQ Server <> wrote in
    news:DhMmp.31372$:


    > 9.20: How do I send mail?
    >
    > Use the "sendmail" program directly:


    <snip>

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

    <snip>


    Is there a reason that this prefers the use of sendmail over the various
    CPAN modules that allow use of SMTP directly? As someone who writes a lot
    fo Perl for Windows, scritps / modules that use calls to sendmail are a
    real pain, those using SMTP directly are much more portable.

    --
    Graham Drabble
    http://www.drabble.me.uk/
    Graham Drabble, Apr 7, 2011
    #2
    1. Advertising

  3. On 2011-04-07 17:49, Graham Drabble <> wrote:
    > On 05 Apr 2011 PerlFAQ Server <> wrote in
    > news:DhMmp.31372$:
    >> 9.20: How do I send mail?
    >>
    >> Use the "sendmail" program directly:

    >
    ><snip>
    >
    >> Or you might be able use the CPAN module "Mail::Mailer":

    ><snip>
    >
    >
    > Is there a reason that this prefers the use of sendmail over the various
    > CPAN modules that allow use of SMTP directly? As someone who writes a lot
    > fo Perl for Windows, scritps / modules that use calls to sendmail are a
    > real pain, those using SMTP directly are much more portable.


    Actually you want to use SUBMISSION (RFC 4409), not SMTP (RFC 5321). But
    apart from that I agree that talking a well-defined network protocol is
    more portable than and generally preferrable to invoking a program.

    (Invoking /usr/sbin/sendmail is a lot simpler, though)

    hp
    Peter J. Holzer, Apr 7, 2011
    #3
  4. PerlFAQ Server

    brian d foy Guest

    [[ This message was both posted and mailed: see
    the "To," "Cc," and "Newsgroups" headers for details. ]]

    In article
    <>, Graham
    Drabble <> wrote:

    > On 05 Apr 2011 PerlFAQ Server <> wrote in
    > news:DhMmp.31372$:
    >
    >
    > > 9.20: How do I send mail?
    > >
    > > Use the "sendmail" program directly:

    >
    > <snip>
    >
    > > Or you might be able use the CPAN module "Mail::Mailer":

    > <snip>
    >
    >
    > Is there a reason that this prefers the use of sendmail over the various
    > CPAN modules that allow use of SMTP directly?


    Because no one has rewritten the answer with modern modules. You could
    be the one to do that. :)
    brian d foy, Apr 8, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    3
    Views:
    1,942
    Larry Lard
    Nov 23, 2005
  2. =?Utf-8?B?Unlhbg==?=
    Replies:
    2
    Views:
    3,888
    Steven Cheng[MSFT]
    Feb 28, 2006
  3. refv8
    Replies:
    2
    Views:
    27,064
    ittower
    Oct 13, 2006
  4. Mike P
    Replies:
    1
    Views:
    565
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 1, 2007
  5. Ramon
    Replies:
    2
    Views:
    1,005
    Ramon
    Oct 25, 2007
Loading...

Share This Page