CGIScriptDepot.com Your source for CGI Scripts, Tutorials, Remotely Hosted Scripts and more... |
|
Sending Email in a CGI Script
In a previous article, we talked about the form processing using CGI-perl. We also saw that how data from a form can be passed on to a CGI script either using the GET method or using the POST method, the later one being more secure and is usually recommended.
Now the question is what can be done with this data.
Now that we have the data from the form, we can use this data in any useful manner according our needs. Sending an email using the information from the form is one of the most basic needs that every developer generally has.
For Example: We can put together useful information from a form, say a feedback form, and email the data a user enters to the webmaster. At the same time, we can send an email to the user thanking him for the feedback.
Let’s get into the details of how exactly we can do this.
The most popular way of sending email through CGI is to use the sendmail program. sendmail is available on almost all UNIX systems.
To begin with we shall define a variable called $mail which will store the location of the sendmail program. (The following example is almost always correct. Check with your system administrator or web host to make sure you have the correct path for sendmail.)
The $mail variable now defines the path to the sendmail program. We can also use variables to store parts of the email we want to send, such as the reply-to address, subject, etc. For example:
Now we would use the following code to actually use sendmail to create and send an email. We assume that $to is a variable containing the To address for the email, and $content is a variable containing the message content:
This part of the code opens the sendmail and then uses variables defined elsewhere in the script like reply_to, subject, content to construct and send the message. Once we close the file access to sendmail sendmail will deliver our message.
The line print SENDMAIL "Content-type: text/plain\n\n"; which is in BOLD above tells the server that the content to be followed is plain text. We can also use html instead to text to send the mails. This broadens possibilities for us. Using html means we can even link up some images in our content which shall be displayed when the user opens the mail.
In that case the $content variable can be something like this.
In this manner we can generate very useful emails using the sendmail and our CGI Scripts!
Learn More
We maintain a handy list of Perl and CGI Scripting Tutorials that can help get you started creating your own CGI Scripts. Even ifyou are already writing your own CGI Scripts- you might learn a thing or two in our more advanced tutorials.
|
©2003-2005 Assorted Internet Ventures LLC |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |