How to make a PERL script - please review
- dreamer7
- Student


- Joined: Jan 20, 2003
- Posts: 96
- Loc: UK
- Status: Offline
Hey all can you tell me what else i can add to this tutorial i made please.
Heres what the script looks like..
You might be a bit scared by now if you have had no experience with perl at the moment but i assure it is fairly simple.
But lets go through it line by line:
This is the 'shebang' line as its called but its just telling the script where perl is on the server.
These two lines are important as is the shebang line but these two lines call perl modules the first is if any errors occur the module will display the errors on your web browser the second opens the CGI module which is used for various different tasks but the task we are going to use it for is for mailing.
The first line is just making $q a CGI object anyone who knows what object oriented programming is should see the point in it doing this as the next 5 lines take the values from the following fields in your web form which you should name by putting in "name=" after input type=... i'll show you the web form later so you don't have to remember that one yet.
This part of the code opens the perl sendmail program (NOTE: "/usr/sbin/sendmail" isn't always the path to sendmail please check in case it isn't) it opens the sendmail and it gives it a file handle called MAIL this is used to reference it.
This is the main email part of your script it tells the preprocessor that its text your printing and not perl code. To,From, Subject is all self explanatory. Name,subject,message are the variables we have taken from the web form. This part of the script can be changed to suit your needs.This script i made and so i customised it so that it would say who was sending it on the subject and "Have a nice day!" at the end of the email as a nice touch lol. Also with most languages if you start something you have to finish it so it is finished with MAIL_MESSAGE as an end tag.
This closes sendmail and then redirects the browser to a new page which you could put in your web form as a hidden field and name it "ok_url" or put in the URL and take out $ok_url but i'm pretty sure you need to keep the 2 new line characters (\n\n).
Then the end of the script with exit; .
Now onto the web form (bear in mind that you will need to change how it looks because im only typing out what needs to be put in it):
The path to your script should look something like "http:\\www.yoursite.com\cgi-bin\script.cgi" . Well thats about it if you need any help post it on here. Hope it helps to all you people who need scripts for mailing.
I would put the perl code into a text editor like notepad and save it as either .cgi or .pl and another thing is that when you upload the script to your cgi-bin directory (if you don't have one then your server doesn't have perl and so you can't use this script) you must change the file permissions to 0755 or readable to all executable by all but writeable only by you. A good ftp program will have the options to do that for you but also your web hosts program for uploading could have it too.
Heres what the script looks like..
Code: [ Select ]
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$q = new CGI;
$name = $q->param('name');
$subject = $q->param('subject');
$age = $q->param('age');
$email = $q->param('email');
$ok_url = $q->param('ok_url');
open (MAIL, "| /usr/sbin/sendmail -oi -n -t");
print MAIL <<MAIL_MESSAGE;
To:$email
From:mailer\@OnSpRiNg.co.uk
Subject:Form Sent to you by $name
The following information has been sent from your test form:
This person has wrote the following:
Name: $name
Subject: $subject
Message sent:
$message
Have a nice day!
MAIL_MESSAGE
close MAIL;
print "location: $ok_url\n\n";
exit;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$q = new CGI;
$name = $q->param('name');
$subject = $q->param('subject');
$age = $q->param('age');
$email = $q->param('email');
$ok_url = $q->param('ok_url');
open (MAIL, "| /usr/sbin/sendmail -oi -n -t");
print MAIL <<MAIL_MESSAGE;
To:$email
From:mailer\@OnSpRiNg.co.uk
Subject:Form Sent to you by $name
The following information has been sent from your test form:
This person has wrote the following:
Name: $name
Subject: $subject
Message sent:
$message
Have a nice day!
MAIL_MESSAGE
close MAIL;
print "location: $ok_url\n\n";
exit;
- #!/usr/bin/perl
- use CGI::Carp qw(fatalsToBrowser);
- use CGI;
- $q = new CGI;
- $name = $q->param('name');
- $subject = $q->param('subject');
- $age = $q->param('age');
- $email = $q->param('email');
- $ok_url = $q->param('ok_url');
- open (MAIL, "| /usr/sbin/sendmail -oi -n -t");
- print MAIL <<MAIL_MESSAGE;
- To:$email
- From:mailer\@OnSpRiNg.co.uk
- Subject:Form Sent to you by $name
- The following information has been sent from your test form:
- This person has wrote the following:
- Name: $name
- Subject: $subject
- Message sent:
- $message
- Have a nice day!
- MAIL_MESSAGE
- close MAIL;
- print "location: $ok_url\n\n";
- exit;
You might be a bit scared by now if you have had no experience with perl at the moment but i assure it is fairly simple.
But lets go through it line by line:
Code: [ Select ]
#!/usr/bin/perl
This is the 'shebang' line as its called but its just telling the script where perl is on the server.
Code: [ Select ]
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use CGI;
- use CGI::Carp qw(fatalsToBrowser);
- use CGI;
These two lines are important as is the shebang line but these two lines call perl modules the first is if any errors occur the module will display the errors on your web browser the second opens the CGI module which is used for various different tasks but the task we are going to use it for is for mailing.
Code: [ Select ]
$q = new CGI;
$name = $q->param('name');
$age = $q->param('age');
$message = $q->param('message');
$email = $q->param('email');
$ok_url = $q->param('ok_url');
$name = $q->param('name');
$age = $q->param('age');
$message = $q->param('message');
$email = $q->param('email');
$ok_url = $q->param('ok_url');
- $q = new CGI;
- $name = $q->param('name');
- $age = $q->param('age');
- $message = $q->param('message');
- $email = $q->param('email');
- $ok_url = $q->param('ok_url');
The first line is just making $q a CGI object anyone who knows what object oriented programming is should see the point in it doing this as the next 5 lines take the values from the following fields in your web form which you should name by putting in "name=" after input type=... i'll show you the web form later so you don't have to remember that one yet.
Code: [ Select ]
open (MAIL, "| /usr/sbin/sendmail -oi -n -t");
This part of the code opens the perl sendmail program (NOTE: "/usr/sbin/sendmail" isn't always the path to sendmail please check in case it isn't) it opens the sendmail and it gives it a file handle called MAIL this is used to reference it.
Code: [ Select ]
print MAIL <<MAIL_MESSAGE;
To:$email
From:mailer\@yoursite.com
Subject:Form Sent to you by $name
This person has wrote the following:
Name: $name
Subject: $subject
Message sent:
$message
Have a nice day!
MAIL_MESSAGE
To:$email
From:mailer\@yoursite.com
Subject:Form Sent to you by $name
This person has wrote the following:
Name: $name
Subject: $subject
Message sent:
$message
Have a nice day!
MAIL_MESSAGE
- print MAIL <<MAIL_MESSAGE;
- To:$email
- From:mailer\@yoursite.com
- Subject:Form Sent to you by $name
- This person has wrote the following:
- Name: $name
- Subject: $subject
- Message sent:
- $message
- Have a nice day!
- MAIL_MESSAGE
This is the main email part of your script it tells the preprocessor that its text your printing and not perl code. To,From, Subject is all self explanatory. Name,subject,message are the variables we have taken from the web form. This part of the script can be changed to suit your needs.This script i made and so i customised it so that it would say who was sending it on the subject and "Have a nice day!" at the end of the email as a nice touch lol. Also with most languages if you start something you have to finish it so it is finished with MAIL_MESSAGE as an end tag.
Code: [ Select ]
close MAIL;
print "location: $ok_url\n\n";
exit;
print "location: $ok_url\n\n";
exit;
- close MAIL;
- print "location: $ok_url\n\n";
- exit;
This closes sendmail and then redirects the browser to a new page which you could put in your web form as a hidden field and name it "ok_url" or put in the URL and take out $ok_url but i'm pretty sure you need to keep the 2 new line characters (\n\n).
Then the end of the script with exit; .
Now onto the web form (bear in mind that you will need to change how it looks because im only typing out what needs to be put in it):
Code: [ Select ]
<html>
<head>
<title>titleofyourchoice</title>
</head>
<body>
<form action="/pathtoyourscript">
Email Address: <input type="text" name="email"><p>
Message: <input type="text" name="message"><p>
Name: <input type="text" name="name"><p>
Age: <input type="text" name="age"><p>
<input type="hidden" name="ok_url" value="http:\\www.yoursite.com\thankyoupage.htm">
<input type="submit" value="Submit"><input type="reset">
</body>
</html>
<head>
<title>titleofyourchoice</title>
</head>
<body>
<form action="/pathtoyourscript">
Email Address: <input type="text" name="email"><p>
Message: <input type="text" name="message"><p>
Name: <input type="text" name="name"><p>
Age: <input type="text" name="age"><p>
<input type="hidden" name="ok_url" value="http:\\www.yoursite.com\thankyoupage.htm">
<input type="submit" value="Submit"><input type="reset">
</body>
</html>
- <html>
- <head>
- <title>titleofyourchoice</title>
- </head>
- <body>
- <form action="/pathtoyourscript">
- Email Address: <input type="text" name="email"><p>
- Message: <input type="text" name="message"><p>
- Name: <input type="text" name="name"><p>
- Age: <input type="text" name="age"><p>
- <input type="hidden" name="ok_url" value="http:\\www.yoursite.com\thankyoupage.htm">
- <input type="submit" value="Submit"><input type="reset">
- </body>
- </html>
The path to your script should look something like "http:\\www.yoursite.com\cgi-bin\script.cgi" . Well thats about it if you need any help post it on here. Hope it helps to all you people who need scripts for mailing.
I would put the perl code into a text editor like notepad and save it as either .cgi or .pl and another thing is that when you upload the script to your cgi-bin directory (if you don't have one then your server doesn't have perl and so you can't use this script) you must change the file permissions to 0755 or readable to all executable by all but writeable only by you. A good ftp program will have the options to do that for you but also your web hosts program for uploading could have it too.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
June 23rd, 2003, 10:22 am
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 2 posts
- Users browsing this forum: No registered users and 216 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum

