Forum Moderators: open

Message Too Old, No Replies

Could anyone help me with an ASP script

Email script needs tweaking

         

u4eas

10:51 am on Oct 9, 2003 (gmt 0)

10+ Year Member



Ok here is my problem....

I have a third party site that sends targeted traffic to a form on my website. This third party adds an PID "personal unique ID" to the url as it passes the traffic to my site. When that traffic gets to me its pointed to the basic html form that is handled by an asp script for validation and emailing. The script then emails me the form.

I need to add something to that script that pulls out the PID # and generates a SECOND basic email with a predesignated subject and body, but it also needs to add the unique PID to the second email and then send it to the third party for tracking reasons.

Any help would be greatly appreciated.

I talked with the third party to try and get some help, but they only code in Cold Fusion. ;(

Thanks in advance!

U4EA

IanTurner

11:14 am on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You could make the HTML form an ASP page and write the PID to a hidden field on the form so it is passed to the ASP emailing script.

u4eas

12:11 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



My problem is I am not a programer.... I dont know much about ASP, I just hack and slash. Could someone write the code I need and let me use it on the form, or even an example that I may be able to copy.

IanTurner

12:32 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Something like this in your ASP page will pass the PID to the email script.

<input type="hidden" name="PID" value="<%=request.querystring("PID")%>">

In the email script just copy what is being done for the first email and add in another write to write in the PID.

User Mailer.CC to add an extra address (or it will be something similar - you will need to read the documentation for the mailer object to work out exactly how it should work.

u4eas

6:39 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



Thanks so much for the PID answer...

I was able to parse the PID number and have it sent in the one email, but I have been unsuccessful in having the seperate email sent.

The first Email should contain the form data.

The second email should contain just the PID a specific Subject and A Specific message...

What I am getting is the form data with the PID in one message.

Maybe I cant have two seperate forms sent at the same time off of the same page?

Cheers-

Matt

IanTurner

6:55 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You are right you can't have two forms sent seperately off the one page at the same time.

However if you are using an ASP mailer object you can send two emails from the receiving page. I would look at CDONTS or ASPEmail as fairly simple to use ASP mailer objects.

If you are using a standard frontpage mailer then I don't know enough to say whether two mails can be sent.

u4eas

7:13 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



Ian:

I am using Formmail


<% '***************************************************************************
'* ASP FormMail *
'* *
'* Do not remove this notice. *
'* *
'* Copyright 1999, 2000 by Mike Hall. *
'* Please see [brainjar.com...] for documentation and terms of use. *
'***************************************************************************

'- Customization of these values is required, see documentation. -----------

'referers = Array("www.ZZZZZZZ.com", "wwww.ZZZZZZ.com", "ZZZZZZ.com", "ZZZZZZZ.com","http://www.ZZZZZZ.com","http://www.ZZZZZ.com")
mailComp = "ASPMail"
smtpServer = "mail.ZZZZZZZ.com"
fromAddr = "webmaster@ZZZZZZZ.com"

'- End required customization section. -------------------------------------

Response.Buffer = true
errorMsgs = Array()

'Check for form data.

if Request.ServerVariables("Content_Length") = 0 then
call AddErrorMsg("No form data submitted.")
end if

'Check if referer is allowed.

' validReferer = false
' referer = GetHost(Request.ServerVariables("HTTP_REFERER"))
' for each host in referers
' if host = referer then
' validReferer = true
' end if
' next
' if not validReferer then
' if referer = "" then
' call AddErrorMsg("No referer.")
' else
' call AddErrorMsg("Invalid referer: '" & referer & "'.")
' end if
' end if

'Check for the recipients field.

if Request.Form("_recipients") = "" then
call AddErrorMsg("Missing email recipient.")
end if

'Check all recipient email addresses.

recipients = Split(Request.Form("_recipients"), ",")
for each name in recipients
name = Trim(name)
if not IsValidEmail(name) then
call AddErrorMsg("Invalid email address in recipient list: " & name & ".")
end if
next
recipients = Join(recipients, ",")

'Get replyTo email address from specified field, if given, and check it.

name = Trim(Request.Form("_replyToField"))
if name <> "" then
replyTo = Request.Form(name)
else
replyTo = Request.Form("_replyTo")
end if
if replyTo <> "" then
if not IsValidEmail(replyTo) then
call AddErrorMsg("Invalid email address in reply-to field: " & replyTo & ".")
end if
end if

'Get subject text.

subject = Request.Form("_subject")

'If required fields are specified, check for them.

if Request.Form("_requiredFields") <> "" then
required = Split(Request.Form("_requiredFields"), ",")
for each name in required
name = Trim(name)
if Left(name, 1) <> "_" and Request.Form(name) = "" then
call AddErrorMsg("Missing value for " & name)
end if
next
end if

'If a field order was given, use it. Otherwise use the order the fields were
'received in.

str = ""
if Request.Form("_fieldOrder") <> "" then
fieldOrder = Split(Request.Form("_fieldOrder"), ",")
for each name in fieldOrder
if str <> "" then
str = str & ","
end if
str = str & Trim(name)
next
fieldOrder = Split(str, ",")
else
fieldOrder = FormFieldList()
end if

'If there were no errors, build the email note and send it.

if UBound(errorMsgs) < 0 then

'Build table of form fields and values.

body = "<table border=1 cellpadding=2 cellspacing=0>" & vbCrLf
for each name in fieldOrder
body = body _
& "<tr valign=top>" _
& "<td><b>" _
& name _
& ":&nbsp;</b></font></td>" _
& "<td>" _
& Request.Form(name) _
& "</td>" _
& "</tr>" & vbCrLf

next
body = body & "</table>" & vbCrLf

'Add a table with any environmental variables.

if Request.Form("_envars") <> "" then
body = body _
& "<p>" _
& "<table border=1 cellpadding=2 cellspacing=0>" & vbCrLf
envars = Split(Request.Form("_envars"), ",")
for each name in envars
name = Trim(name)
body = body _
& "<tr valign=top>" _
& "<td><b>" _
& name _
& ":&nbsp;</b></td>" _
& "<td>" _
& Request.ServerVariables(name) _
& "</td>" _
& "</tr>" & vbCrLf
next
body = body & "</table>" & vbCrLf
end if

'Send it.

str = SendMail()
if str <> "" then
AddErrorMsg(str)
end if

'Redirect if a URL was given.

if Request.Form("_redirect") <> "" then
Response.Redirect(Request.Form("_redirect"))
end if

end if %>

<% '---------------------------------------------------------------------------
' Subroutines and functions.
'---------------------------------------------------------------------------

sub AddErrorMsg(msg)

dim n

'Add an error message to the list.

n = UBound(errorMsgs)
Redim Preserve errorMsgs(n + 1)
errorMsgs(n + 1) = msg

end sub

function GetHost(url)

dim i, s

GetHost = ""

'Strip down to host or IP address and port number, if any.

if Left(url, 7) = "http://" then
s = Mid(url, 8)
elseif Left(url, 8) = "https://" then
s = Mid(url, 9)
end if
i = InStr(s, "/")
if i > 1 then
s = Mid(s, 1, i - 1)
end if

getHost = s

end function

function IsValidEmail(email)

dim names, name, i, c

'Check for valid syntax in an email address.

IsValidEmail = true
names = Split(email, "@")
if UBound(names) <> 1 then
IsValidEmail = false
exit function
end if
for each name in names
if Len(name) <= 0 then
IsValidEmail = false
exit function
end if
for i = 1 to Len(name)
c = Lcase(Mid(name, i, 1))
if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
IsValidEmail = false
exit function
end if
next
if Left(name, 1) = "." or Right(name, 1) = "." then
IsValidEmail = false
exit function
end if
next
if InStr(names(1), ".") <= 0 then
IsValidEmail = false
exit function
end if
i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
IsValidEmail = false
exit function
end if
if InStr(email, "..") > 0 then
IsValidEmail = false
end if

end function

function FormFieldList()

dim str, i, name

'Build an array of form field names ordered as they were received.

str = ""
for i = 1 to Request.Form.Count
for each name in Request.Form
if Left(name, 1) <> "_" and Request.Form(name) is Request.Form(i) then
if str <> "" then
str = str & ","
end if
str = str & name
exit for
end if
next
next
FormFieldList = Split(str, ",")

end function

function SendMail()

dim mailObj
dim addrList

'Send email based on mail component. Uses global variables for parameters
'because there are so many.

SendMail = ""

'Send email (CDONTS version), doesn't support reply-to address and has
'no error checking.

if mailComp = "CDONTS" then
set mailObj = Server.CreateObject("CDONTS.NewMail")
mailObj.BodyFormat = 0
mailObj.MailFormat = 0
mailObj.From = fromAddr
mailObj.To = recipients
mailObj.Subject = subject
mailObj.Body = body
mailObj.Send
end if

'Send email (JMail version).

if mailComp = "JMail" then
set mailObj = Server.CreateObject("JMail.SMTPMail")
mailObj.Silent = true
mailObj.ServerAddress = smtpServer
mailObj.Sender = fromAddr
mailObj.ReplyTo = replyTo
mailObj.Subject = subject
addrList = Split(recipients, ",")
for each addr in addrList
mailObj.AddRecipient Trim(addr)
next
mailObj.ContentType = "text/html"
mailObj.Body = body
if not mailObj.Execute then
SendMail = "Email send failed: " & mailObj.ErrorMessage & "."
end if
end if

'Send email (ASPMail version).

if mailComp = "ASPMail" then
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
for each addr in Split(recipients, ",")
mailObj.AddRecipient "", Trim(addr)
next
mailObj.Subject = subject
mailObj.ContentType = "text/html"
mailObj.BodyText = body
if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."
end if
end if

end function %>

u4eas

8:21 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



Ian would I add something like this to the script above?


<%
pid = request.QueryString("PID")

Set objMail = Server.CreateObject("SMTPsvg.Mailer")
objMail.From = "me@me.com"
objMail.To = "somebody@someone.com"
objMail.Subject = "This is my subject blah blah blah"

objMail.Body = "This is the body I need to have auto generated" & vbcrlf & _
"line2 " & vbcrlf & _
"pid = " & pid

objMail.Send
set objMail = nothing

'response.write("Message was sent successfully") (and Ill comment out the response since I dont need that)
%>

If so where should I put it, and is there anything else I would need?

Thanks so much!

Matt

IanTurner

8:37 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If you copy the contents of the "if mailComp = "ASPMail" then " block and paste it before the end if of the block, you will send two emails - then it is just a case of editing the parameters in the second block to the values you need

u4eas

9:01 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



I did what you said and it errored out.

I fixed the errors the way I thought they should be fixed...

I got it to look like this, its not erroring now but its also not sending the second email

:(

if mailComp = "ASPMail" then
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
for each addr in Split(recipients, ",")
mailObj.AddRecipient "", Trim(addr)
next
mailObj.Subject = subject
mailObj.ContentType = "text/html"
mailObj.BodyText = body
if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."

if mailComp = "ASPMail" then
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = "webmaster@mydomain.com"
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
mailObj.AddRecipient "matt@mydomain.com"
mailObj.Subject = "Test Email"
mailObj.ContentType = "text/html"
mailObj.BodyText = "This is the Body"
mailObj.SendMail
SendMail = "Email send failed: " & mailObj.Response & "."

end if
end if

end if

end function %>

Any ideas?

It also kept erroring untill I added the third end if statement.

IanTurner

9:18 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Move the first 'end if' to above the third 'if'

u4eas

11:55 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



I am so close I can taste it...
Too bad I dont know asp any better :(
Anyway here is what I have come up with:


'Send email (ASPMail version).

if mailComp = "ASPMail" then
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
for each addr in Split(recipients, ",")
mailObj.AddRecipient "", Trim(addr)
next
mailObj.Subject = subject
mailObj.ContentType = "text/html"
mailObj.BodyText = body
if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."

if mailComp = "ASPMail" then
PID = request.QueryString("PID")
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.Recipient = "me@mydomain.com"
mailObj.Subject = "Test Email"
mailObj.ContentType = "text/html"
mailObj.BodyText = "The following prospect has completed the form " & vbcrlf & _
"line2 " & vbcrlf & _
"PID = " & Request.Form("PID")
mailObj.SendMail
SendMail = "Email send failed: " & mailObj.Response & "."

end if
end if
end if

end function %>

What seems to be happening is that the first ASPMailer block sends the correct form to me then stops. It doesnt continue to the second aspmailer block...

If I comment the first aspmalier out then the second email is sent like its supposed to but I dont get my form...

So I know both work correctly, however there must be something that is telling it to stop when it processes the first email. As it doesnt even get to the second email...

***GRRR***

Any ideas?

defanjos

2:42 am on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not 100% sure, but I think this will work (partial code only):


'Send email (ASPMail version).

if mailComp = "ASPMail" then

set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
for each addr in Split(recipients, ",")
mailObj.AddRecipient "", Trim(addr)
next
mailObj.Subject = subject
mailObj.ContentType = "text/html"
mailObj.BodyText = body

if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."
end if

set mailObj=nothing

PID = request.QueryString("PID")
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.Recipient = "me@mydomain.com"
mailObj.Subject = "Test Email"
mailObj.ContentType = "text/html"
mailObj.BodyText = "The following prospect has completed the form " & vbcrlf & _
"line2 " & vbcrlf & _
"PID = " & Request.Form("PID")

if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."
end if

set mailObj=nothing

end if

u4eas

10:31 am on Oct 10, 2003 (gmt 0)

10+ Year Member



Thank you so very much the corrections you made worked.
Both emails are being sent as they should be!

Thanks to both you and Ian for taking some time to help me, I really appreciate it!

:) :)

Matt