Archived

This topic is now archived and is closed to further replies.

Evil Bill

CGI: Sending someone an automated E-Mail

Recommended Posts

Hi all, i''m writing a CGI program (as you can probably tell by my past 4 or 5 questions), which is a message board, suspiciously similar to this one . I''d like to have the ability to e-mail me when someone posts, and also to e-mail a user when they recieve a reply (just like here). I had a look into "sendmail", and i couldn''t understand any of it. Is this what i should use? And if so, how complicated is it to use? I''m using Windows 98SE, but i''ll be porting to Linux over the weekend if it matters. Cheers, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
This is a script I use on one of my sites, take a look. Hopefully it might help...

  
#! /usr/local/bin/perl

# leifform.cgi
#
# Written by Leif M. Wright
# leif@conservatives.net
#
#************************************************************************
# I got really ticked when the form response programs I wanted to use
# all wanted to send me an e-mail. I don't want an email.
# So I wrote this VERY simply program to post the data to a logfile and
# redirect the user to a "form successful" page. That's all it does. Period.
# If you want a fancy form handler, go somewheres else, but I just
# wanted a simple script to post form data to a logfile
# Your form should have action=post
# For every input name on your form, you'll have to add it to the
# script below, and decide whether to make it required or not.
# Enjoy
# I'm not guilty or responsible if this screws up your system. It works
# fine on mine.
##################################################################
#

# this tells the program where to place a link for the user
# once they are done.
$link ="http://www.some.net/";


# this is where the info will be written to - you need to specify a real directory
$file ="/path/to/file"; #must be read/writable

#
##################################################################

# Default recipient (where submitted data will be sent)
$recipient = 'name@some.com';


# Default email topic (subject line of email) set below


# Path to sendmail (check with your server, but try this one first... should work)
$sendmail = "/usr/sbin/sendmail";

##################################################################

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$contents{$name} = $value;

}
}

chop($date = `date`);

# Now with the program
###########################################################
# Has to output a Content-type
print "Content-type: text/html\n\n ";

# Check to see if all required information was entered
# If you want a field to be required, add it here.
&no_cigar unless $contents{'name'};



sub no_cigar
{
print <<"HTML";
<HTML><HEAD><TITLE>Form Incomplete</TITLE></HEAD>
<BODY>
<H1>Form Incomplete</H1>
I'm sorry, the form was not filled completely.<br>
Please Return to the form and fill it out completely.<p>
Thank you.
<HR>
<a href=\"$contents{'url'}\">Return to the $contents{'formname'} page</a>
</body></html>
HTML
exit;
}



# They go here if the form was submitted
# successfully. Now this page will send them
# off to where ever specify in the "link" field above.

print <<"HTML";
<HTML><HEAD><TITLE>Entry successful</TITLE></HEAD>
<BODY>
<H1>Entry successful!</H1>
<p>
<hr noshade>
<p>
<H2>$contents{'name'}, your information has been received!</H2>
Your information will be processed immediately,
[b]$contents{'name'}[/b]. Thank you.

<p>
<HR noshade>
<A HREF=\"$link\">Back to the home page</A>.

</body>
</html>
HTML

$subject = "Subject";

open(MAIL,"|$sendmail -t");
print MAIL "To: $recipient\n";
print MAIL "From: $recipient\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Date: $date\n";
print MAIL "FORM NAME: $contents{'formname'}\n";
print MAIL "NAME: $contents{'name'}\n";
print MAIL "PHONE: $contents{'phone'}\n";
print MAIL "EMAIL: $contents{'email'}\n";
print MAIL "STREET: $contents{'street'}\n";
print MAIL "CITY: $contents{'city'}\n";
print MAIL "STATE: $contents{'state'}\n";
print MAIL "ZIP CODE: $contents{'zip'}\n";
close(MAIL);
exit;


[edited by - ElCrazon on October 31, 2002 7:25:51 PM]

Share this post


Link to post
Share on other sites
Ok, i get hundreds of errors if i try to compile sendmail (missing include files). Does anyone know of somewhere i can dowanload a binary for windows 9x?
Or does anyone have any other suggestions (Preferably in C code, not Perl, but i can understand it).

I''m running an Apache Web server at the mo, and it doesn''t have sendmail with it.

Cheers,
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites