Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem getting sendmail script to work

Status
Not open for further replies.

mightychest

Technical User
Dec 6, 2009
4
0
0
GB
Hi,

I've spent a lot of time looking for a solution to this problem but have had no success (probably because I don't understand Perl well enough to make other people's code work for me).

I got some code from It was the best explained code that I could find so I stuck with it. I am making a website and on one page need the info entered into a form to be sent off to my email address. I also want another email to be sent to the sender with a copy of what they entered into the form.

The problem is the script just does not send any emails! Oddly, the form does not throw up any errors and it successfully redirects to the "message sent successfully" page. I am at a loss as to what the problem is.

My web host (also my ISP) has this page giving details on some of the ways to overcome problems with their frankly inadequate and frustrating system: It gives details on how to get Perl to work with them.

Any help would be greatly appreciated! This problem is making my hair fall out and my grades suffer.

Code below:


#!/usr/bin/perl
# location of Perl (above) - check your doc or ask admin

#############################################################
#
# Script Source: BrainCode.com
# Source URL: # Script Title: Alpha E-Mailer w/ Autoresponder
# Script Name: AlphaMail.pl
# Copyright: 1998,1999,2000 by OEC Corp & BrainCode.com
# Version: 09.01.1999.C (latest)
# Status: Fully tested, release version 7, Y2K ready
#
# This script may not be redistributed without this header
#
# There are no restrictions on this script - anyone may use
# it, though we appreciate credit where due. Responsibility
# for use of the script is entirely with the user. Please
# note that BrainCode.com does not support Perl scripts!
#
# Instructions:
#
# - Use WordPad.exe or any plain text editor to edit
# - Set your path to Perl as needed (first line at top)
# - Set ALL variables appropriately (below, see notes)
# - Take special care with the path to your sendmail (below)
# - Send the script to the server in ascii
# - Set script permissions - check your doc or ask admin
# - See a_faq.html for HTML set-ups in alphamail.zip file
# - Required html pages are also provided in alphamail.zip
#
#############################################################



##### SETTABLE VARIABLES ####################################

# URL to go to if there is a problem with form input
$ErrorPage = "
# URL to go to when form has been successfully submitted
$ThankPage = "
# URL to go to if a 'foreign' referer calls the script
$EvilReferer = "
# E-mail address to send intake form to (your address)
# If not using PERL 5, escape the @ thus: \@ instead of @
$YourEmail = '***@***.com';

# Script works only on your server(s) - ('URL1','URL2')
@referers = ('
# Location of mail program - check your doc or ask admin
$MailProgram = "/usr/lib/sendmail -f '$email' '***@***.com'";

# Subject of the e-mail autoreply to the submitter
$Subject = "***.com Autoreply";

# Header line in the auto-reply message
$Header = "Thank you for your enquiry. If you have requested any information from us we will do our best to revert to you as soon as possible.";

# Brief tail message for body of e-mail autoreply
$TailMessage = "Thank you for your contact.";

# Your signature lines the end of the autoreply e-mail
$Signature1 = "*** ***";
$Signature2 = "
##### END OF SETTABLE VARIABLES ############################



##### MAIN PROGRAM #########################################

&CheckReferingURL;
&ReadParse;
$title = $in{'title'};
$name = $in{'name'};
$lawfirmcompany = $in{'lawfirmcompany'};
$email = $in{'email'};
$telephone = $in{'telephone'};
$website = $in{'website'};
$country = $in{'country'};
$message = $in{'message'};
&CheckEmailAddressFormat;
&CheckFields;
&GetDate;
&SendSubmission;
&SendAutoReply;
print "Location: $ThankPage\n\n";
exit;

# _________________________________________________________

sub SendSubmission {
open (MAIL,"$MailProgram") || die "Content-Type: text/plain\n\nCan't send mail, sorry!\n";
print MAIL "To: $YourEmail\n";
print MAIL "From: $email\n";
print MAIL "Subject: $Subject\n";
print MAIL "$Date\n\n";
print MAIL "From: $title $name\n";
print MAIL "Law firm / company name: $lawfirmcompany\n";
print MAIL "Email: $email\n";
print MAIL "Telephone number: $telephone\n";
print MAIL "Website URL: $website\n";
print MAIL "Country: $country\n\n";
print MAIL "Message:\n\n";
print MAIL "$message\n\n";

print MAIL "-HOST:\t$ENV{'REMOTE_HOST'}\n";
print MAIL "-PAGE:\t$ENV{'HTTP_REFERER'}\n";

close (MAIL);
}

# _________________________________________________________

sub SendAutoReply {
open (MAIL,"$MailProgram") || die "Content-Type: text/plain\n\nCan't send mail, sorry!\n";
print MAIL "To: $email\n";
print MAIL "From: $YourEmail\n";
print MAIL "Subject: $Subject\n";
print MAIL "$Header\n";
print MAIL "$Date\n\n";
print MAIL "$Subject\n\n";
print MAIL "You sent the following:\n\n";
print MAIL "==============================\n\n";
print MAIL "$Date\n\n";
print MAIL "Title: $title\n";
print MAIL "Name: $name\n";
print MAIL "Law firm / company name: $lawfirmcompany\n";
print MAIL "Email: $email\n";
print MAIL "Telephone number: $telephone\n";
print MAIL "Website URL: $website\n";
print MAIL "Country: $country\n\n";
print MAIL "Message:\n\n";
print MAIL "$message\n\n";
print MAIL "==============================\n\n";
print MAIL "$TailMessage\n\n";
print MAIL "Best regards,\n\n\n";
print MAIL "$Signature1\n";
print MAIL "$Signature2\n\n";

print MAIL "-HOST:\t$ENV{'REMOTE_HOST'}\n";
print MAIL "-PAGE:\t$ENV{'HTTP_REFERER'}\n";

close (MAIL);
}

# _________________________________________________________

sub GetDate {
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('01','02','03','04','05','06','07','08','09','10','11','12');
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year+1900;
$Date = "$days[$wday] $months[$mon]/$mday/$year";
}

# _________________________________________________________

sub ReadParse { local (*in) = @_ if @_;
local ($i, $key, $val); if ( $ENV{'REQUEST_METHOD'} eq "GET" )
{$in = $ENV{'QUERY_STRING'};}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});}
else {
$in = ( grep( !/^-/, @ARGV )) [0];
$in =~ s/\\&/&/g; } @in = split(/&/,$in);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2);
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val; } return length($in); }

# _________________________________________________________

sub CheckEmailAddressFormat {
if (index($email, "@") < 1) {&DoEmailError;}
#if (index($email, ".") < 1) {&DoEmailError;}
if (index($email, " ") > -1) {&DoEmailError;}
}
sub CheckFields {
if (!$name || $name eq ' ') {&DoEmailError;}
if (!$email || $email eq ' ') {&DoEmailError;}
if (!$message || $message eq ' ') {&DoEmailError;}
}
sub DoEmailError {
print "Location: $ErrorPage\n\n";
exit;
}

# _________________________________________________________

sub CheckReferingURL {
if ($ENV{'HTTP_REFERER'}) {
foreach $referer (@referers) {
if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
$check_referer = '1';
last;
}}}
else {$check_referer = '1';}
if ($check_referer != 1) {
print "Location: $EvilReferer\n\n";
exit;
}}

# _________________________________________________________

exit;

##### End of Script ########################################
 
This script is way too long for testing, especially if you are new to perl.

You need to start with a script that only sends to you.
Once that works, add the reply section, sending to you also or open a free email acct somewhere else and use that for testing for replies.

1. You need to make sure this ISP uses sendmail.
2. You need to make sure that sendmail for this ISP is actually at /usr/lib/sendmail .
3. You need to make sure that perl is at /usr/bin/perl
4. Don't send anything but To: From: Subject: and body
Skip info like date. Make sure you have double \n\n after to,from,subject before starting body.

Read up on CGI.pm, it offers much better security.
You can probably find much better scripts searching for form mailers that use CGI.pm.
These scripts will have something like
Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI;

etc
near top.

This is pretty simple stuff, but still is pretty confusing
 
Hi MrCBofBCinTX,

Thank you for your reply. I have a lot of clarifying to do with my ISP especially if I'm going to use CGI.pm.

Simple AND confusing. Isn't that always the worst way.
 
I can't imagine that they don't offer CGI.pm.
But if they don't, you can install a copy locally in your cgi area.

This sort of problem is very common, so you won't have much trouble finding examples of how to install modules in your own directories.

I started off a few years ago using ISP's shared servers. A good way to learn the ropes. It forces you to learn some important details. Now I have my own server, which is wonderful when you learn enough to run it later on.

I get to decide what is and isn't available! :)

If you decide that they are too much trouble for what you want to do, just switch. Just make sure you understand what is lacking (if anything) before hopping around

Good Luck, Keep at it, perl is a really wonderful tool for all kinds of tasks, not just web stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top