Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Sending email with Perl

by chuleto1 (Beadle)
on Jul 21, 2003 at 13:43 UTC ( #276271=perlquestion: print w/replies, xml ) Need Help??

chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a CGI page that is supposed to send an email with a form's content. Can anyone provide me with sample code?

Replies are listed 'Best First'.
Re: Sending email with Perl
by Chady (Priest) on Jul 21, 2003 at 14:11 UTC

    Or, you can use take a look at NMS, by none but our own davorg.

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady |
      Judging from the CVS updates, I think it should renamed Nick Cleaton's Perl Script Archive ;)

      The script to look at is FormMail. It will take a bit of configuring, but bear in mind it is a well supported script (via the NMS support list) so there are plenty of developers who can offer advice and help.

      Don't try and roll your own as you are likely to get burnt unless you've thought of everything. Spamming via the original FormMail (not the NMS one) is quite painful.

      Barbie | Birmingham Perl Mongers |

        Judging from the CVS updates, I think it should renamed Nick Cleaton's Perl Script Archive ;)

        /me agrees wholeheartedly


        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Sending email with Perl
by snadra (Scribe) on Jul 21, 2003 at 13:56 UTC

    I think it is the easiest to use the MIME::Lite Module for that.
    use MIME::Lite; # simple message, just text: $msg = MIME::Lite->new( From =>'', To =>'', Cc =>',', Subject =>'Hi!', Data =>"The content of the message\n..." );
    You can also send messages with attagements or HTML messages...

Re: Sending email with Perl
by blue_cowdawg (Monsignor) on Jul 21, 2003 at 13:58 UTC

    This ought to be a FAQ if it ain't already.

    MIME::Lite is your friend.

      #!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw/ fatalsToBrowser /; # but never in # production use MIME::Lite; my $q=new CGI; my @contents=(); foreach my $key($q->param) { # There is a problem with this code, I'll explain # later. push @contents,sprintf("%s = %s",$key,$q->param($key)); } my $mail=new MIME::Lite ( From => '', To => '', Subject => 'contents of my form', Type => "TEXT", Data => join("\n",@contents) ); MIME::Lite->send("smtp","",120); $mail -> send;

    For more information on what is going on there check out MIME::Lite's man page.


    First off I broke all of my rules with this example in that I do no error checking anywhere. Secondly, if your CGI parameters have multiple values then my code above will break. I leave the solution to that as an intellectual excersize for you to solve.

    Another potential problem with the code above is the use of CGI::Carp qw / fatalsToBrowser/. This is a potential information leak for hackers if you use that in production and they manage to make your code break.

    I dashed this off as I am about to be late for a meeting, but there is the nucleus of a solution for you.

    Peter L. BergholdBrewer of Belgian Ales
    Unix Professional
Re: Sending email with Perl
by hardburn (Abbot) on Jul 21, 2003 at 13:50 UTC

    Search through the Mail:: namespace on CPAN. Plenty of examples there.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

(jeffa) Re: Sending email with Perl
by jeffa (Bishop) on Jul 21, 2003 at 13:51 UTC
    Just look at the documentation for MIME::Lite. Say, you are using MIME::Lite to do this, aren't you? ;)


    (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://276271]
Approved by Mr_Person
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2023-03-27 13:37 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.