Sending email

Posted on 2004-11-08
Last Modified: 2013-12-25
First of all, I have never used CGI before so please bare with me...

I want to create an online form that users can fill out and have the info emailed back to me.  My hosting company tells me that I can use CGI and Perl on my site.  Please tell me what I need to accomplish this.  I have done this before using ASP but never CGI or Perl so I'm not sure how different it is.  Also please let me know if there is any specific information I need to get from my ISP.

Question by:mgcIT
    LVL 10

    Expert Comment


    I wouldn't write your own, I would use Formmail available at

    easy to use so why reinvent the wheel


    LVL 18

    Author Comment

    ok, I downloaded the file ( but I still don't really know how to use it.  What do I need to do to customize for my needs?  What I need to do is:

    1. Format the email message that is going to be generated
    2. Specify the email address to send it to
    3. Where to put this file on my site (I have a /CGI-bin directory already set up so I'm guessing in that folder???)
    4. Get the data from the form elements on my survey page to the Perl script

    LVL 48

    Expert Comment


    Please *don't* use FormMail from

    It is very poorly written and has had so many security flaws.

    You want FormMail from
    LVL 18

    Author Comment

    ok Tintin, since you are the highest rated expert in this section I'll take your word for it.... but please walk me through this.  What (if anything) do I need to change in this new Perl file, and how do I get it up & running?  Again, I am a total novice with CGI and Perl so I'm looking for a REALLY easy example to follow.  I gave 500 points for this so I wouldn't have to do a lot of reading and research.

    LVL 48

    Accepted Solution

    Let's start from the begining then.

    1.  You need to find out (if you don't already know), where you can run Perl/CGI scripts.  On most webservers, you have a cgi-bin directory to put the scripts in, however in some cases, you can put the script where ever you like so long as it has a recognised extension like .cgi or .pl  (for these purposes, I'll assume you have a cgi-bin).

    2.  Copy into your cgi-bin directory.  As you are a windows server, you may need to rename it FormMail.cgi

    3.  At this point, see it the webserver recognises it by entering into your browser.

    4.  Assuming you don't get any errors, you can now configure the options you need.  The most important ones are $mailprog (set to your SMTP server name), and possibly @allow_mail_to and @recipients

    5.  Then create your HTML form and point it to /cgi-bin/  See the README and Examples for more details.

    LVL 18

    Author Comment

    ok, when I upload that file, it appears to be working but when I change the @recipients to have my email address, I get an error.  Here is that section of the file:

      $DEBUGGING         = 1;
      $emulate_matts_code= 0;
      $secure            = 1;
      $allow_empty_ref   = 1;
      $max_recipients    = 5;
      $mailprog          = '/usr/lib/sendmail -oi -t';
      $postmaster        = '';
      @referers          = qw( localhost);
      @allow_mail_to     = qw( localhost);   ****These are the only 2 lines I changed
      @recipients        = (;                        ****
      %recipient_alias   = ();
      $locale            = '';
      $charset           = 'iso-8859-1';
      $date_fmt          = '%A, %B %d, %Y at %H:%M:%S';
      $style             = '/css/nms.css';
      $no_content        = 0;
      $double_spacing    = 1;
      $wrap_text         = 0;
      $wrap_style        = 1;
      $address_style     = 0;
      $send_confirmation_mail = 0;
      $confirmation_text = <<'END_OF_CONFIRMATION';
    Subject: form submission

    Thank you for your form submission.

    LVL 18

    Author Comment

    ahh think I may have figured it out...

    for @recipients I put:

     @recipients        = qw(;    

    I didn't have the qw before so that was probably it.  I'll play around with this for a little while longer and let you know if I have any more trouble before finalizing this Q...

    Thanks for all the help.
    LVL 18

    Author Comment

    ok I ran into a few more snags.  The email is working correctly now, but I am having trouble customizing the email message and the Form submission page.

    For the email message I need the following things:

    1. How to set From address
    2. How to set Subject line
    3. How to edit body of email message (The default is rather vague and I'd like to type my own info there for the heading)

    Secondly, after you submit the form it redirects to another page (I think) and I'd like to change that page to something else.  Either change the content on that page or use my own page but I have no idea how.

    Thanks again...

    LVL 48

    Expert Comment

    1.   Are you wanting a user to specify their email address or have a fixed from address?

    2.   You set that in the form, eg:

       <input type="hidden" name="subject" value="From the feedback form" />

    3.  The email body is not so flexible (without editing the Perl code).  The main choices you have is:

    $double_spacing     - If this is set to 1 (as it is by default) then a blank
                          line is printed after each form value in the e-mail.
                          Change this value to 0 if you want the e-mail to be
                          more compact.

    $wrap_text          - If this is set to 1 then the content of any long text
                          fields will be wrapped at around 72 columns in the
                          e-mail which is sent.  The way that this is done is
                          controlled by the variable $wrap_style

    $wrap_style         - If $wrap_text is set to 1  then
                          the text will be wrapped in such a way that the left
                          margin of the text is lined up with the beginning of the
                          text after the description of the field - that is to
                          say it is indented by the length of the field name
                          plus 2.  If it is set to 2 then the subsequent lines
                          of the text will not be indented at all and will be
                          flush with the start of the lines.  The choice of style
                          is really a matter of taste although you might find
                          that style 1 does not work particularly well if your
                          e-mail client uses a proportional font where the spaces
                          of the indent might be smaller than the characters in
                          the field name.
    LVL 18

    Author Comment

    I just wanted to have a fixed From address, but I figured out how to do that using a hidden form element.  Having a different redirect page was the same process as well... just use a hidden element called "redirect".

    Thanks for all of the help. It's working perfectly now.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now