Hi Friends

I have a requirement to where my cgi program has to send an email when the "Submit" button is pressed.

Here is my cgi program, which has a text field to enter the username and a submit button.


use strict;
use CGI qw(:standard);
use CGI;
use check;
my $q = new CGI;

print $q->header("text/html");
my $action = param("action");
print button();

sub button {
return <<EOF;


<body bgcolor="#95CBE9">


<TABLE BORDER=0 align="center" cellpadding="20">
  <TD>User Name</TD>
<INPUT type=text size="40" name="username" value=""/>

<input type="submit" value="Submit" onClick=?????>


What I need to do is when the user hits the submit button an email has to be sent from Windows platform. I did a bit of googling and found out the perl script to send the mail as follows. This uses Net::SMTP

use Net::SMTP;
my $smtpserver = 'xxx.xxx.xxx.xxx';
my $smtpuser = 'username';
my $fromemail = 'someone@somewhere.com';

    my $smtp = Net::SMTP-> new($smtpserver, Timeout => 10);
    $smtp-> mail($smtpuser);
    $smtp-> to('someoneelse@somewhere.cz');
    $smtp-> data();
    $smtp-> datasend("To: blblb\@blblbl.cz\n");
    $smtp-> datasend("From: psmejkal\@tesco.cz\n");
    $smtp-> datasend("\n");
    $smtp-> datasend("test\n");
    $smtp-> dataend();
    $smtp-> quit;

But I have no idea on how to integrate it with the cgi script.

Any help would be really appreciated.



I am not sure whether you have the solution by now. Cos, you have posted few days back. Please ignore if you already have a solution...

Alright, as per your research you already have entire solution. All you require is integration.

The <form> tag in html has attributes which will do it for you. All you need to do is write a script which sends a email(as you have given a code snippet at the bottom) and glue the script in the form tag.


<form name="name_of_the_form" action="[page containing the script which triggers the mail]" method="[get or post]">

action attribute takes the page name which needs to be invoked when the user submit the form(i.e. when the user hits the 'submit'[or whatever name you give to the submit button] button.

In your case, you need to call the page which sends out the mail.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.