#!/usr/bin/perl -w use Socket; use strict; sub sendmail() { my (%body) = @_; my $line = ""; # Substitute your mail server here. my($mailServer) = 'smtp.puyallup.k12.wa.us'; #These next fields come in from the form, your form should have at least the following # fields: recipient, senderemail, sendername, subject my($mailTo) = "$body{recipient}"; my($mailFrom) = "$body{senderemail}"; my($realName) = "$body{sendername}"; my($subject) = "$body{subject}"; # These constants shouldn't need changing. my($packFormat) = 'S n a4 x8'; #Internet address format (AF_INET:portNumber:serverAddress:eightNulls) my($proto) = getprotobyname("tcp") || 6; my($port) = getservbyname("SMTP", "tcp") || 25; my($name,$aliases,$addrtype,$len,@addrs) = gethostbyname($mailServer); my($serverAddr) = $addrs[0]; # If this script dies at this line, it is because you don't have an entry in the SYS:\etc\hosts # file for your mail server. Manually add an entry for the mail server if not already there. if (! defined($len)) { die('gethostbyname failed.'); } socket(SMTP, AF_INET(), SOCK_STREAM(), $proto) or die("socket: $!"); connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr)) or die("connect: $!"); select(SMTP); $| = 1; select(STDOUT); # use unbuffered i/o. { my($inpBuf) = ''; recv(SMTP, $inpBuf, 200, 0); } sendSMTP(1, "HELO\n"); sendSMTP(1, "MAIL FROM: <$mailFrom>\n"); sendSMTP(1, "RCPT TO: <$mailTo>\n"); sendSMTP(1, "VRFY\n"); sendSMTP(1, "DATA\n"); sendSMTPnoresponse(1, "From: $realName <$mailFrom>\n"); sendSMTPnoresponse(1, "Subject: $subject\n"); sendSMTPnoresponse(1, "\n"); # Print each of the name/value pairs coming from the form IF they have been filled in for $line (keys %body) { if($line !~ /link/ && $line !~ /subject/) # We don't need to print out the hidden form fields { sendSMTPnoresponse(1, "$line = $body{$line}\n") if($body{$line}); } } # Since we have put the name/value pairs into a hash which stores randomly, using the above # for loop will print the name/value pairs randomly in your email message. # To print them in a specific sequence, you can explicitly print each one with the following # format (INSTEAD OF using the for loop). # sendSMTPnoresponse(1, "Sender\'s Name = $body{sendername}\n") if($body{sendername}); # sendSMTPnoresponse(1, "Sender\'s Email = $body{senderemail}\n") if($body{senderemail}); sendSMTPnoresponse(1, "\n"); sendSMTP(1, ".\n"); sendSMTP(1, "QUIT\n"); close(SMTP); } sub closeSocket { # close smtp socket on error close(SMTP); die("SMTP socket closed due to SIGINT\n"); } sub send_to_smtp { my($debug) = shift; my($response) = shift; my($buffer) = @_; # Uncomment the following line for debugging only # print STDERR ("> $buffer") if $debug; send(SMTP, $buffer, 0); if ($response) { recv(SMTP, $buffer, 200, 0); # Uncomment the following line for debugging only # print STDERR ("< $buffer") if $debug; return( (split(/ /, $buffer))[0] ); } } sub sendSMTP { my ($debug) = shift; send_to_smtp($debug, 1, @_); } sub sendSMTPnoresponse { my ($debug) = shift; send_to_smtp($debug, 0, @_); } 1; #return true