PowerBASIC Forums
  Source Code
  PBCC: CGI Form Mailer

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   PBCC: CGI Form Mailer
Dave Navarro
Member
posted September 17, 1999 03:35 PM     Click Here to See the Profile for Dave Navarro     Edit/Delete Message   Reply w/Quote
The following application will take all of the items from a Web
Form and send them to an email address.

You can specify the "to" address (and other fields) in the form
by adding a hidden objects (mailto, mailfrom, subject, etc.).

You can specify which fields are required by listing all field names
seperated by commas in a hidden "required" object.

It's not very robust, but it works.

--Dave

IP: Logged

Dave Navarro
Member
posted September 17, 1999 03:37 PM     Click Here to See the Profile for Dave Navarro     Edit/Delete Message   Reply w/Quote

#COMPILE EXE
#INCLUDE "PBCGI.INC"

$mailhost = "yourhost.com"
$mailto = "you@yourhost.com"
$mailfrom = "cgimail@yourhost.com"

FUNCTION PbMain() AS LONG

LOCAL hTcp AS LONG
LOCAL params AS LONG
LOCAL x AS LONG

LOCAL buffer AS STRING
LOCAL localhost AS STRING

LOCAL mailto AS STRING
LOCAL mailfrom AS STRING
LOCAL subject AS STRING
LOCAL redirect AS STRING
LOCAL email AS STRING
LOCAL required AS STRING

DIM param(1 TO 1) AS STRING

buffer = ReadCGI
params = ParseParams(buffer, param())

mailfrom = CgiParam(buffer, "mailfrom")
mailto = CgiParam(buffer, "mailto")
redirect = CgiParam(buffer, "redirect")
subject = CgiParam(buffer, "subject")
required = CgiParam(buffer, "required")

IF ISFALSE LEN(mailto) THEN
mailto = $mailto
END IF

IF ISFALSE LEN(mailfrom) THEN
mailfrom = $mailfrom
END IF

IF ISFALSE LEN(redirect) THEN
redirect = Http_Referer
END IF

IF ISFALSE LEN(subject) THEN
subject = "email from " & redirect
END IF

IF LEN(required) THEN
FOR x = 1 TO PARSECOUNT(required)
IF ISFALSE LEN(CgiParam(buffer,PARSE$(required,x))) THEN
WriteCGI "<FONT SIZE=""+2"">Form Error!"
WriteCGI "<P>Required field """ & PARSE$(required,x) & """ can not be empty.</FONT>"
WriteCGI "<P>Please press the BACK button on your browser and re-submit the form with all required items."
WriteCGI "<P>Thank you!"
EXIT FUNCTION
END IF
NEXT
END IF

' ** Get the local host name
HOST ADDR TO hTCP
HOST NAME hTCP TO localhost

' ** Connect to mail server
hTCP = FREEFILE
TCP OPEN "smtp" AT $mailhost AS hTCP
IF ERR THEN
Buffer = "Error connecting to mailhost"
GOTO SendError
ELSE
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "220" THEN
GOTO SendError
END IF
END IF

' ** Greet the mailhost
TCP PRINT hTCP, "HELO " + localhost
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "250" THEN
Buffer = "HELO error: " + Buffer
GOTO SendError
END IF

' ** Tell the mailhost who we are
TCP PRINT hTCP, "MAIL FROM: <gmail@basicguru.com>"
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "250" THEN
Buffer = "MAIL FROM error: " + Buffer
GOTO SendError
END IF

' ** Tell the mailhost who the message is for
TCP PRINT hTCP, "RCPT TO: <" + mailto + ">"
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "250" THEN
Buffer = "RCPT TO error: " + Buffer
GOTO SendError
END IF

' ** Send the message
TCP PRINT hTCP, "DATA"
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "354" THEN
Buffer = "DATA error: " + Buffer
GOTO SendError
END IF

TCP PRINT hTCP, "From: " & mailfrom
TCP PRINT hTCP, "To: " & mailto
TCP PRINT hTCP, "Subject: " & subject
TCP PRINT hTCP, "X-Mailer: cgimail.exe v1.0 (www.powerbasic.com)"
TCP PRINT hTCP, ""

FOR x = 1 TO params
buffer = DecodeCGI(Param(x))
IF ASC(buffer) = 46 THEN
buffer = "." & buffer
END IF
TCP PRINT hTCP, buffer
NEXT x

TCP PRINT hTCP, ""
TCP PRINT hTCP, "Referer: " & Http_Referer
TCP PRINT hTCP, "Remote_Addr: " & Remote_Addr
TCP PRINT hTCP, "Http_User_Agent: " & Http_User_Agent

TCP PRINT hTCP, "."
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "250" THEN
GOTO SendError
END IF

' ** Say goodbye
TCP PRINT hTCP, "QUIT"
TCP LINE hTCP, Buffer
IF LEFT$(Buffer, 3) <> "221" THEN
Buffer = "QUIT error: " + Buffer
GOTO SendError
END IF

TCP CLOSE hTCP

STDOUT "Location: " & redirect & $CRLF

FUNCTION = -1

Done:
EXIT FUNCTION

SendError:
PRINT buffer
TCP CLOSE hTCP
GOTO Done

END FUNCTION

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c