Hello, I am building a website using a standard form and perl script that we commonly use, but this one has a special request and I don't know how to handle it because I have very little knowledge of Perl, other than what is in the standard script I have reused for many projects.

I am pasting my code below as it stands now- it currently contains a command for sending the information to me in an email, but I also need two additional emails to be generated once the user fills out the form and presses the submit button. The first should be a confirmation email that will go back to the student confirming receipt of the form, and the should be second a pre-populated email to a professor (using the Instructor Name and Instructor Email filled in on the form by the student).

I'm not sure how to add the instruction to send out these two additional emails, and how to populate with the correct Instructor Name and Student Name. If anyone can shed any light on this I would be really grateful for any pointers.

#!/usr/bin/perl -w

# ***********************
# *** submit.pl ***
# ***********************
# This Perl script handles submissions of registration forms for
# Pearson Arts & Sciences hardside marketing sites 
# Updated July 2011
# - - - - - - - - - - 
# The basic flow of the script is as follows:
# * Get hidden "company" parameter and define e-mail coordinator and log file.
# * Check user input, send error page if name or email missing
# * Send an email with the form response
# * Display thank you page

print "Content-Type: text/html\n\n";

# Global Variables 
# * no need to edit this
$mail_prog = "/usr/lib/sendmail";

# Get library and parse client input 
# * no need to edit this
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$request = new CGI;


# Read and check user input
# * edit to include all inputs named in HTML form
# * format: $CHANGETHIS = $request->param('CHANGETHIS');
# * email field required in HTML form (and below) to send form responses from
@option = $request->param("option");
$optionList = join( ', @option );
$studentname = $request->param('studentname');
$instructorname = $request->param('instructorname');
$instructoremail = $request->param('instructoremail');
$affil = $request->param('affil');
$addr = $request->param('addr');
$city = $request->param('city');
$state = $request->param('state');
$zip = $request->param('zip');
$phone = $request->param('phone');
$email = $request->param('email');
$rules = $request->param('rules');

# Define e-mails
# * postEmail - who should receive the form email responses
# * ccEmail - who should be copied on the form email responses
$postEmail = 'emily.wieja@pearson.com';
$ccEmail = 'webmaster@aw.com';

#Put all the required fields in this if statement 
# * format: if ($RequiredField1 eq "" || $RequiredField2 eq "")
# * make sure there's no || before the closing parentheses
if (@option eq "" || $optionList eq "" || $studentname eq "" || $instructorname eq "" || $instructoremail eq "" || $affil eq "" || $addr eq "" || $city eq "" || $state eq "" || $zip eq ""|| $phone eq "" || $email eq "" || $rules eq "") 

{
	 			
# Missing data--send error page
# * need to create HTML error page as part of the site

print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/error.html";
		</script>
!!EOX!!

}



else {

# Send e-mail form response
# * no need to change first 4 lines
# * Subject: as specified in form copy
# * Following lines are how the emailed response appears
# * no need to change bottom "EOM" line or below
	
open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
print MAIL << "!!EOM!!";
To: $postEmail
CC: $ccEmail
From: $email
Subject: Student Application Allied Health Award 2012

Option 2 link: $optionList
Student's Name: $studentsname
Instructor's Name: $instructorname
Instructor's Email: $instructoremail
Email: $email
College/University: $affil
Student's Address: $addr
City: $city
State: $state
Zip: $zip
Phone: $phone
Student's Email: $email
Have read and downloaded rules: $rules

Submitted from http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/index.html
!!EOM!!

close(MAIL);


# Put your thank you page here
# * need to create HTML thank you page as part of the site
print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/thankyou.html";
		</script>
!!EOX!!

}

The way i see it the code that sends out the mail is already there (line 81-105). You also have all the values since they are read in the beginning (line 33-45). So you could write a function that contains the mail code and then just pass the parameters to it. That way you can easily send multiple mails without having to copy all that send code.

The way i see it the code that sends out the mail is already there (line 81-105). You also have all the values since they are read in the beginning (line 33-45). So you could write a function that contains the mail code and then just pass the parameters to it. That way you can easily send multiple mails without having to copy all that send code.

Thank you- I'm not sure I understand how to write a function without copying all the send code?

Well you have to move the code that is sending the mail to a function. This enables you to call the Function with different parameters instead of having several blocks of the same code.

&send_mail(mail to student);
&send_mail(mail to professor);
&send_mail(mail to etc);

sub send_mail
{
    # mail code goes here
}

Well you have to move the code that is sending the mail to a function. This enables you to call the Function with different parameters instead of having several blocks of the same code.

&send_mail(mail to student);
&send_mail(mail to professor);
&send_mail(mail to etc);

sub send_mail
{
    # mail code goes here
}

So I would move the code from lines 81-105 to a new document, then call that code inside my current document? Would the code you put in above then go into my current document? If so I don't quite understand what would go into the "Mail code goes here" spot.

I am not familiar with sending mails like this so i am not sure if you can just cram the mail code into a function. If it does you can just call the function whenever you need it. Make sure you change your values beforehand.
Changes: Line 75-84 and 96-end.

#!/usr/bin/perl -w

# ***********************
# *** submit.pl ***
# ***********************
# This Perl script handles submissions of registration forms for
# Pearson Arts & Sciences hardside marketing sites 
# Updated July 2011
# - - - - - - - - - - 
# The basic flow of the script is as follows:
# * Get hidden "company" parameter and define e-mail coordinator and log file.
# * Check user input, send error page if name or email missing
# * Send an email with the form response
# * Display thank you page

print "Content-Type: text/html\n\n";

# Global Variables 
# * no need to edit this
$mail_prog = "/usr/lib/sendmail";

# Get library and parse client input 
# * no need to edit this
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$request = new CGI;


# Read and check user input
# * edit to include all inputs named in HTML form
# * format: $CHANGETHIS = $request->param('CHANGETHIS');
# * email field required in HTML form (and below) to send form responses from
@option = $request->param("option");
$optionList = join( ', @option );
$studentname = $request->param('studentname');
$instructorname = $request->param('instructorname');
$instructoremail = $request->param('instructoremail');
$affil = $request->param('affil');
$addr = $request->param('addr');
$city = $request->param('city');
$state = $request->param('state');
$zip = $request->param('zip');
$phone = $request->param('phone');
$email = $request->param('email');
$rules = $request->param('rules');

# Define e-mails
# * postEmail - who should receive the form email responses
# * ccEmail - who should be copied on the form email responses
$postEmail = 'emily.wieja@pearson.com';
$ccEmail = 'webmaster@aw.com';

#Put all the required fields in this if statement 
# * format: if ($RequiredField1 eq "" || $RequiredField2 eq "")
# * make sure there's no || before the closing parentheses
if (@option eq "" || $optionList eq "" || $studentname eq "" || $instructorname eq "" || $instructoremail eq "" || $affil eq "" || $addr eq "" || $city eq "" || $state eq "" || $zip eq ""|| $phone eq "" || $email eq "" || $rules eq "") 

{
	 			
# Missing data--send error page
# * need to create HTML error page as part of the site

print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/error.html";
		</script>
!!EOX!!

}



else {

# mail code used to be here

# send your first email as usual
&send_mail();
# change mail info
$postEmail = "new recipient";
# change additional variables here if necessary
# send more mails
&send_mail();
# repeat above steps if necessary

# Put your thank you page here
# * need to create HTML thank you page as part of the site
print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/thankyou.html";
		</script>
!!EOX!!

}

# mail code is now in a function
sub send_mail
{
	# Send e-mail form response
	# * no need to change first 4 lines
	# * Subject: as specified in form copy
	# * Following lines are how the emailed response appears
	# * no need to change bottom "EOM" line or below
		
	open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
	print MAIL << "!!EOM!!";
	To: $postEmail
	CC: $ccEmail
	From: $email
	Subject: Student Application Allied Health Award 2012

	Option 2 link: $optionList
	Student's Name: $studentsname
	Instructor's Name: $instructorname
	Instructor's Email: $instructoremail
	Email: $email
	College/University: $affil
	Student's Address: $addr
	City: $city
	State: $state
	Zip: $zip
	Phone: $phone
	Student's Email: $email
	Have read and downloaded rules: $rules

	Submitted from http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/index.html
	# !!EOM!!

	close(MAIL);
}

I am not familiar with sending mails like this so i am not sure if you can just cram the mail code into a function. If it does you can just call the function whenever you need it. Make sure you change your values beforehand.
Changes: Line 75-84 and 96-end.

#!/usr/bin/perl -w

# ***********************
# *** submit.pl ***
# ***********************
# This Perl script handles submissions of registration forms for
# Pearson Arts & Sciences hardside marketing sites 
# Updated July 2011
# - - - - - - - - - - 
# The basic flow of the script is as follows:
# * Get hidden "company" parameter and define e-mail coordinator and log file.
# * Check user input, send error page if name or email missing
# * Send an email with the form response
# * Display thank you page

print "Content-Type: text/html\n\n";

# Global Variables 
# * no need to edit this
$mail_prog = "/usr/lib/sendmail";

# Get library and parse client input 
# * no need to edit this
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$request = new CGI;


# Read and check user input
# * edit to include all inputs named in HTML form
# * format: $CHANGETHIS = $request->param('CHANGETHIS');
# * email field required in HTML form (and below) to send form responses from
@option = $request->param("option");
$optionList = join( ', @option );
$studentname = $request->param('studentname');
$instructorname = $request->param('instructorname');
$instructoremail = $request->param('instructoremail');
$affil = $request->param('affil');
$addr = $request->param('addr');
$city = $request->param('city');
$state = $request->param('state');
$zip = $request->param('zip');
$phone = $request->param('phone');
$email = $request->param('email');
$rules = $request->param('rules');

# Define e-mails
# * postEmail - who should receive the form email responses
# * ccEmail - who should be copied on the form email responses
$postEmail = 'emily.wieja@pearson.com';
$ccEmail = 'webmaster@aw.com';

#Put all the required fields in this if statement 
# * format: if ($RequiredField1 eq "" || $RequiredField2 eq "")
# * make sure there's no || before the closing parentheses
if (@option eq "" || $optionList eq "" || $studentname eq "" || $instructorname eq "" || $instructoremail eq "" || $affil eq "" || $addr eq "" || $city eq "" || $state eq "" || $zip eq ""|| $phone eq "" || $email eq "" || $rules eq "") 

{
	 			
# Missing data--send error page
# * need to create HTML error page as part of the site

print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/error.html";
		</script>
!!EOX!!

}



else {

# mail code used to be here

# send your first email as usual
&send_mail();
# change mail info
$postEmail = "new recipient";
# change additional variables here if necessary
# send more mails
&send_mail();
# repeat above steps if necessary

# Put your thank you page here
# * need to create HTML thank you page as part of the site
print <<"!!EOX!!";
   		<script type="text/javascript" language="JavaScript" >
		document.location = "http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/thankyou.html";
		</script>
!!EOX!!

}

# mail code is now in a function
sub send_mail
{
	# Send e-mail form response
	# * no need to change first 4 lines
	# * Subject: as specified in form copy
	# * Following lines are how the emailed response appears
	# * no need to change bottom "EOM" line or below
		
	open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
	print MAIL << "!!EOM!!";
	To: $postEmail
	CC: $ccEmail
	From: $email
	Subject: Student Application Allied Health Award 2012

	Option 2 link: $optionList
	Student's Name: $studentsname
	Instructor's Name: $instructorname
	Instructor's Email: $instructoremail
	Email: $email
	College/University: $affil
	Student's Address: $addr
	City: $city
	State: $state
	Zip: $zip
	Phone: $phone
	Student's Email: $email
	Have read and downloaded rules: $rules

	Submitted from http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/index.html
	# !!EOM!!

	close(MAIL);
}

Thank you! I have got that working now. I am having another problem as I build the emails to be sent, I hope it is ok to continue the discussion with a related problem- I have put in a url in one of the emails, set to auto-fill two fields on the linked form:

open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
print MAIL << "!!EOM!!";
To: $instructoremail
CC: $ccEmail
From: $postEmail
Subject: Allied Health Award from Pearson-Your Approval Needed

Dear $instructorname,

Your student, $studentname, has applied for the 2012 Allied Health Award from Pearson. This award recognizes three students for their outstanding suggestion for a type of media tool that would help them learn more effectively in their A&P or microbiology course.

For your student to be considered for our Award, we need verification from you that they have attended your class in the last two years. Please click on the link below and complete the form that will be sent directly to us.

[B]http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/confirmation/index.html?studentname=$studentname&instructorname=$instructorname[/B]

Submitted from http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/index.html
!!EOM!!

close(MAIL);

However, if the $studentname and $instructorname are two separate first and last names with a space in between them, which is how most people would fill out the form, the URL obviously breaks once the email is sent. Is there any way around this?

If i remember correctly %20 is used to encode whitespaces within a url. Try the following for everything that might include whitespaces:
$studentname =~ s/\s/%20/g;

Thank you- would I add that line underneath the line

$studentname = $request->param('studentname');

as a separate line?

Yes, add it after you obtained the value from the form and before you add it to the url.

That worked pertfectly- thank you so much!

Thanx alot its really work very good.

sorry- one more question rel;ated to that last bit of code. Is there some way I can place the

$studentname =~ s/\s/%20/g;

so that only the $studentname in the URL below is affected, and not the "Your student, $studentname" etc.? When I test it now, the URK works great but I am getting the %20 in the body of the email.

open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
print MAIL << "!!EOM!!";
To: $instructoremail
CC: $ccEmail
From: $postEmail
Subject: Allied Health Award from Pearson-Your Approval Needed
Dear $instructorname,
Your student, $studentname, has applied for the 2012 Allied Health Award from Pearson. This award recognizes three students for their outstanding suggestion for a type of media tool that would help them learn more effectively in their A&P or microbiology course.
For your student to be considered for our Award, we need verification from you that they have attended your class in the last two years. Please click on the link below and complete the form that will be sent directly to us.
**http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/confirmation/index.html?studentname=$studentname&instructorname=$instructorname**
Submitted from http://vigstage.pearsonhighered.com/dev/awards/alliedhealth/index.html
!!EOM!!
close(MAIL);

The easiest way to solve this would probably be something like this:

$studentname_url = $studentname;
$studentname_url =~ s/\s/%20/g;

Just create another variable for the url.

That works perfectly- thanks so much.

Many thanks for all your help, I've learned a lot in this thread.

Be a part of the DaniWeb community

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