Solved

Newbie with Sendmail ?

Posted on 2008-10-27
3
496 Views
Last Modified: 2013-12-25
I have a script that needs to have an email acknowledgment sent to a cusmter through email. I have everything ready but this. Can someone help me with the sendmail function?
Thanks~
#!/usr/bin/perl
#inter.cgi - saves form data to a file, and creates
#three different dynamic Web pages
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
 
#prevent Perl from creating undeclared variables
use strict;
 
#declare variables
my ($name, $email, $comments, $data_ok, $msg);
 
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	($name, $email, $comments) = get_input();
	($name, $email, $comments) = format_input();
	$data_ok, $msg = validate_input();
	if ($data_ok eq "Y") {
		save_to_file();
		create_acknowledgment_page();
	}
	else {
		create_error_page();
	}
}
else {
	create_comments_page();
}
exit;
 
#*****user-defined functions*****
sub get_input {
	return param('Name'), param('Email'), param('Comments');
} #end get_input
 
sub format_input {
	#declare and assign values to temporary variables
	my ($n, $e, $c);
	($n, $e, $c) = ($name, $email, $comments);
	#remove leading and trailing spaces from name
	$n =~ s/^ +//;
	$n =~ s/ +$//;
	#remove leading and trailing spaces from e-mail address
	$e =~ s/^ +//;
	$e =~ s/ +$//;
	#remove leading and trailing whitespace characters
	#from comments
	$c =~ s/^\s+//;
	$c =~ s/\s+$//;
	#replace return and newline combination within comments
	#with a space
	$c =~ tr/\r\n/ /;
	#remove extra spaces from within comments
	$c =~ tr/ //s;
	return $n, $e, $c;
} #end format_input
 
sub validate_input {
	my $valid = "Y";
	my $errormsg;
	if ($name eq "" or $email eq "" or $comments eq "") {
		$valid = "N";
		$errormsg = "complete all items";
	}
	elsif ($email !~ m/ [\w\-]+\@[\w\-]+\[\w\-]+/) {
		$valid = "N";
		$errormsg = "enter a valid e-mail address";
	}
	return $valid, $errormsg;
} #end validate_input
 
sub save_to_file {
	open(OUTFILE, ">>", "comments.txt")
		or die "Error opening comments.txt for save. $!, stopped";
	print OUTFILE "$name|$email|$comments\n";
	close(OUTFILE);
} #end save_to_file
 
sub create_acknowledgment_page {
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>$name, thank you for the following \n";
	print "comments:<BR><BR>$comments\n";
	print "</H2></BODY></HTML>\n";
} #end create_acknowledgment_page
 
sub create_error_page {
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>Please return to the form and \n";
	print "$msg.</H2>\n";
	print "</BODY></HTML>\n";
} #end create_error_page
 
sub create_comments_page {
	my (@records, $name_field, $email_field, $com_field);
 
	open(INFILE, "<", "comments.txt")
		or die "Error opening comments.txt. $!, stopped";
 
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>What other coffee lovers say \n";
	print "about our coffees:</H2>\n";
	@records = <INFILE>;
	close(INFILE);
	foreach my $rec (@records) {
		chomp($rec);
		($name_field, $email_field, $com_field) = split(/\|/, $rec);
		print "<B>Name:</B> $name_field<BR>\n";
		print "<B>Comments:</B> $com_field<BR>\n";
		print "<HR>";
	}
	print "</BODY></HTML>\n";
} #end create_comments_page

Open in new window

0
Comment
Question by:reno02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 26

Expert Comment

by:jar3817
ID: 22821194
Check out this page to see how you can send email using perl and sendmail:

http://www.perlfect.com/articles/sendmail.shtml
0
 

Author Comment

by:reno02
ID: 22826186
I looked at it and tried to do this but it still isn't working. What am I doing wrong?
Thanks for any help!
#!/usr/bin/perl
#inter.cgi - saves form data to a file, and creates
#three different dynamic Web pages
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use Mail::Sendmail;
 
#prevent Perl from creating undeclared variables
use strict;
 
#declare variables
my ($name, $email, $comments, $data_ok, $msg, %mail);
 
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	($name, $email, $comments) = get_input();
	($name, $email, $comments) = format_input();
	($data_ok, $msg) = validate_input();
	if ($data_ok eq "Y") {
		save_to_file();
		create_acknowledgment_page();
		create_email_aknowledgment();
	}
	else {
		create_error_page();
	}
}
else {
	create_comments_page();
}
exit;
 
#*****user-defined functions*****
sub get_input {
	return param('Name'), param('Email'), param('Comments');
} #end get_input
 
sub format_input {
	#declare and assign values to temporary variables
	my ($n, $e, $c);
	($n, $e, $c) = ($name, $email, $comments);
	#remove leading and trailing spaces from name
	$n =~ s/^ +//;
	$n =~ s/ +$//;
	#remove leading and trailing spaces from e-mail address
	$e =~ s/^ +//;
	$e =~ s/ +$//;
	#remove leading and trailing whitespace characters
	#from comments
	$c =~ s/^\s+//;
	$c =~ s/\s+$//;
	#replace return and newline combination within comments
	#with a space
	$c =~ tr/\r\n/ /;
	#remove extra spaces from within comments
	$c =~ tr/ //s;
	return $n, $e, $c;
} #end format_input
 
sub validate_input {
	my $valid = "Y";
	my $errormsg;
	if ($name eq "" or $email eq "" or $comments eq "") {
		$valid = "N";
		$errormsg = "complete all items";
	}
	elsif ($email !~ m/[\w\-]+\@[\w\-]+\.[\w\-]+/) {
		$valid = "N";
		$errormsg = "enter a valid e-mail address";
	}
	return $valid, $errormsg;
} #end validate_input
 
sub save_to_file {
	open(OUTFILE, ">> comments.txt")
		or die "Error opening comments.txt for save. $!, stopped";
	print OUTFILE "$name|$email|$comments\n";
	close(OUTFILE);
} #end save_to_file
 
sub create_acknowledgment_page {
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>$name, thank you for the following \n";
	print "comments:<BR><BR>$comments\n";
	print "</H2></BODY></HTML>\n";
} #end create_acknowledgment_page
 
sub create_email_acknowledgment {
	my (%mail);
	$mail{"To"} = $email;
	$mail{"From"} = 'mknigh01@baker.edu';
	$mail{"Subject"} = "International Coffee Comments";
	$mail{"Smtp"} = "crux.baker.edu";
	$mail{"Message"} = "Thank you, $name for submitting your comments: $comments.";
	sendmail(%mail); 
} #end create_email_acknowledgment
 
sub create_error_page {
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>Please return to the form and \n";
	print "$msg.</H2>\n";
	print "</BODY></HTML>\n";
} #end create_error_page
 
sub create_comments_page {
	my (@records, $name_field, $email_field, $com_field);
	open(INFILE, "< comments.txt")
		or die "Error opening comments.txt. $!, stopped";
 
	print "<HTML>\n";
	print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
	print "<BODY>\n";
	print "<H2>What other coffee lovers say \n";
	print "about our coffees:</H2>\n";
	@records = <INFILE>;
	close(INFILE);
	foreach my $rec (@records) {
		chomp($rec);
		($name_field, $email_field, $com_field) = split(/\|/, $rec);
		print "<B>Name:</B> $name_field<BR>\n";
		print "<B>Comments:</B> $com_field<BR>\n";
		print "<HR>";
	}
	print "</BODY></HTML>\n";
} #end create_comments_page

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 200 total points
ID: 22879689
Change your line 96 to this:
    sendmail(%mail) or die $Mail::Sendmail::error;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question