How to make a PERL script - please review

  • dreamer7
  • Student
  • Student
  • dreamer7
  • Posts: 95
  • Loc: UK

Post 3+ Months Ago

Hey all can you tell me what else i can add to this tutorial i made please.

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;
  1. #!/usr/bin/perl
  2. use CGI::Carp qw(fatalsToBrowser);
  3. use CGI;
  4. $q = new CGI;
  5. $name = $q->param('name');
  6. $subject = $q->param('subject');
  7. $age = $q->param('age');
  8. $email = $q->param('email');
  9. $ok_url = $q->param('ok_url');
  10. open (MAIL, "| /usr/sbin/sendmail -oi -n -t");
  11. print MAIL <<MAIL_MESSAGE;
  12. To:$email
  13. From:mailer\@OnSpRiNg.co.uk
  14. Subject:Form Sent to you by $name
  15. The following information has been sent from your test form:
  16. This person has wrote the following:
  17. Name: $name
  18. Subject: $subject
  19. Message sent:
  20. $message
  21. Have a nice day!
  22. MAIL_MESSAGE
  23. close MAIL;
  24. print "location: $ok_url\n\n";
  25. 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;
  1. use CGI::Carp qw(fatalsToBrowser);
  2. 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');
  1. $q = new CGI;
  2. $name = $q->param('name');
  3. $age = $q->param('age');
  4. $message = $q->param('message');
  5. $email = $q->param('email');
  6. $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
  1. print MAIL <<MAIL_MESSAGE;
  2. To:$email
  3. From:mailer\@yoursite.com
  4. Subject:Form Sent to you by $name
  5. This person has wrote the following:
  6. Name: $name
  7. Subject: $subject
  8. Message sent:
  9. $message
  10. Have a nice day!
  11. 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;
  1. close MAIL;
  2. print "location: $ok_url\n\n";
  3. 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>
  1. <html>
  2. <head>
  3. <title>titleofyourchoice</title>
  4. </head>
  5. <body>
  6. <form action="/pathtoyourscript">
  7. Email Address: <input type="text" name="email"><p>
  8. Message: <input type="text" name="message"><p>
  9. Name: <input type="text" name="name"><p>
  10. Age: <input type="text" name="age"><p>
  11. <input type="hidden" name="ok_url" value="http:\\www.yoursite.com\thankyoupage.htm">
  12. <input type="submit" value="Submit"><input type="reset">
  13. </body>
  14. </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
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

I'll go through it after I play some unreal, dont want to reboot to mandrake right now :)

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 73 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.