Help with tell a friend script - email sending
06-18-2008, 07:47 PM
|
Help with tell a friend script - email sending
|
Posts: 182
|
I'd like to use this asp101 tell-a-friend script, but don't know how to set up the emailing part of it. can you help me?
thanks.
Code:
<%
'*******************************************************
'* ASP 101 Sample Code - http://www.asp101.com/ *
'* *
'* This code is made available as a service to our *
'* visitors and is provided strictly for the *
'* purpose of illustration. *
'* *
'* http://www.asp101.com/samples/license.asp *
'* *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
%>
<H3>Tell a Friend:</H3>
<%
Dim objCDO ' Email object
Dim strFromName ' From persons' real name
Dim strFromEmail, strToEmail ' Email addresses
Dim strSubject, strBody ' Message
Dim strThisPage ' This page's URL
Dim strReferringPage ' The referring page's URL
Dim bValidInput ' A boolean indicating valid parameters
' Retrieve this page name and referring page name
strThisPage = Request.ServerVariables("SCRIPT_NAME")
strReferringPage = Request.ServerVariables("HTTP_REFERER")
' Debugging lines:
'Response.Write strThisPage & "<BR>" & vbCrLf
'Response.Write strReferringPage & "<BR>" & vbCrLf
' Read in and set the initial values of our message parameters
strFromName = Trim(Request.Form("txtFromName"))
strFromEmail = Trim(Request.Form("txtFromEmail"))
strToEmail = Trim(Request.Form("txtToEmail"))
strSubject = "Check out ASP 101!"
strBody = Trim(Request.Form("txtMessage"))
' I set the body message to a message that referenced the page the
' user arrived from. This makes it great if you place a link to it
' from your different articles, but can be weird if people link in
' from other web sites.
If strBody = "" Then
If strReferringPage = "" Or InStr(1, strReferringPage, "www.asp101.com", 1) = 0 Then
strBody = ""
strBody = strBody & "I found a site I thought you'd like to see:" & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & " http://www.asp101.com" & vbCrLf
Else
strBody = ""
strBody = strBody & "I found an article I thought you'd like to see:" & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & " " & strReferringPage & vbCrLf
End If
End If
' Quick validation just to make sure our parameters are somewhat valid
bValidInput = True
bValidInput = bValidInput And strFromName <> ""
bValidInput = bValidInput And IsValidEmail(strFromEmail)
bValidInput = bValidInput And IsValidEmail(strToEmail)
' If valid send email and show thanks, o/w show form
If bValidInput Then
' Set up our email object and send the message
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.From = strFromName & " <" & strFromEmail & ">"
objCDO.To = strToEmail
objCDO.Subject = strSubject
objCDO.TextBody = strBody
objCDO.Send
Set objCDO = Nothing
' Show our thank you message
ShowThanksMsg
Else
If "http://" & Request.ServerVariables("HTTP_HOST") & strThisPage = strReferringPage Then
Response.Write "There's been an error. Please check your entries:" & "<BR>" & vbCrLf
End If
' Show our information retrieval form
ShowReferralForm strThisPage, strFromName, strFromEmail, strToEmail, strBody
End If
' End of page logic... subs and functions follow!
%>
<%
' Subroutines and Functions that encapsulate some functionality
' and make the above code easier to write... and read.
' A quick email syntax checker. It's not perfect,
' but it's quick and easy and will catch most of
' the bad addresses than people type in.
Function IsValidEmail(strEmail)
Dim bIsValid
bIsValid = True
If Len(strEmail) < 5 Then
bIsValid = False
Else
If Instr(1, strEmail, " ") <> 0 Then
bIsValid = False
Else
If InStr(1, strEmail, "@", 1) < 2 Then
bIsValid = False
Else
If InStrRev(strEmail, ".") < InStr(1, strEmail, "@", 1) + 2 Then
bIsValid = False
End If
End If
End If
End If
IsValidEmail = bIsValid
End Function
' I made this a function just to get it out of the
' logic and make it easier to read. It just shows the
' form that asks for the input
Sub ShowReferralForm(strPageName, strFromName, strFromEmail, strToEmail, strBody)
' I use script_name so users can rename this script witout having to change the code.
%>
<FORM ACTION="<%= strPageName %>" METHOD="post" name=frmReferral>
<TABLE BORDER="0">
<TR>
<TD VALIGN="top" ALIGN="right"><STRONG>Your Name:</STRONG></TD>
<TD><INPUT TYPE="text" NAME="txtFromName" VALUE="<%= strFromName %>" SIZE="30"></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="right"><STRONG>Your E-mail:</STRONG></TD>
<TD><INPUT TYPE="text" NAME="txtFromEmail" VALUE="<%= strFromEmail %>" SIZE="30"></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="right"><STRONG>Friend's E-mail:</STRONG></TD>
<TD><INPUT TYPE="text" NAME="txtToEmail" VALUE="<%= strToEmail %>" SIZE="30"></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="right"><STRONG>Message:</STRONG></TD>
<TD><TEXTAREA NAME="txtMessage" COLS="50" ROWS="5" WRAP="virtual" READONLY><%= strBody %></TEXTAREA>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE="reset" VALUE="Reset Form" name=rstReferral> <INPUT TYPE="submit" VALUE="Send E-mail" name=subReferral></TD>
</TR>
</TABLE>
</FORM>
<%
'<P>The Message to be sent:</P>
'<P><B>Subject:</B> < %= strSubject % ></P>
'<P><B>Body:</B> < %= strBody % ></P>
End Sub
' This just shows our thank you message... probably didn't need to
' be a function, but since I made the form one I figured I'd do this
' for consistency.
Sub ShowThanksMsg()
%>
<P>Your message has been sent. Thanks for helping us spread the word about ASP 101!</P>
<%
End Sub
%>
|
|
|
|
06-19-2008, 04:18 AM
|
Re: Help with tell a friend script - email sending
|
Posts: 22,257
Location: Blackpool. UK
|
|
|
|
|
06-19-2008, 11:28 PM
|
Re: Help with tell a friend script - email sending
|
Posts: 182
|
Thanks for your reply. I would greatly appreciate a little more clarification, please. I followed your link and read it. Does this mean I should just define these:
strMailServer = "mail.yourdomain.tld"
strSMTPUser = "your.mailserver.username"
strSMTPPassword = "your_password"
strDefMailName = "SenderName"
strDefMailTo = "Recipient"
in the existing ASP101 - Tell a Friend script?
Or should I define those and add the following code to the Tell a friend script? Thanks. I look forward to being enlightened.
Code:
<%
const strMailServer = "mail.yourdomain.tld"
const strSMTPUser = "your.mailserver.username"
const strSMTPPassword = "your_password"
const strDefMailName = "SenderName"
const strDefMailTo = "Recipient"
function CDOSYSMail(strMailFrom,strMailTo,strMailMessage,strCCList,strBccList)
if strMailFrom = "" then strMailFrom = strDefMailName
if strMailTo = "" then strMailTo = strDefMailTo
dim iConf
dim Flds
dim objNewMail
const schema = "http://schemas.microsoft.com/cdo/con...tion/sendusing"
' on error resume next '## Ignore Errors
Set iConf = Server.CreateObject ("CDO.Configuration")
Set Flds = iConf.Fields
'Set and update fields properties
Flds(schema) = cdoSendUsingPort
Flds(schema) = strMailServer
Flds(schema) = cdoBasic
Flds(schema) = strSMTPUser
Flds(schema) = strSMTPPassword
Flds.Update
Set objNewMail = Server.CreateObject("CDO.Message")
Set objNewMail.Configuration = iConf
'Format and send message
Err.Clear
objNewMail.To = strMailTo
objNewMail.From = strMailFrom
if strCCList <> "" then
objNewMail.cc = strCCList
end if
if strBccList <> "" then
objNewMail.bcc = strBccList
end if
objNewMail.Subject = strMailSubject
objNewMail.TextBody = strMailMessage
On Error Resume Next
if bDebug then
response.write strMailTo & "<br>" & strBccList & "<br>" & strccList
else
objNewMail.Send
end if
If Err <> 0 Then
' add in some error messages.
CDOSYSMail = Err
else
CDOSYSMail = true
end if
set Flds = nothing
set iConf = nothing
Set objNewMail = nothing
end function
function CDOSYSHTMLMail(strMailFrom, strMailTo, strMailMessage, strCCList, strBccList)
if strMailFrom = "" then strMailFrom = strDefMailName
if strMailTo = "" then strMailTo = strDefMailTo
dim iConf
dim Flds
dim objNewMail
const schema = "http://schemas.microsoft.com/cdo/con...tion/sendusing"
Set iConf = Server.CreateObject ("CDO.Configuration")
Set Flds = iConf.Fields
Flds(schema) = cdoSendUsingPort
Flds(schema) = strMailServer
Flds(schema) = cdoBasic
Flds(schema) = strSMTPUser
Flds(schema) = strSMTPPassword
Flds.Update
Flds.Update
Set objNewMail = Server.CreateObject("CDO.Message")
Set objNewMail.Configuration = iConf
Err.Clear
objNewMail.To = strMailTo
if strCCList <> "" then
objNewMail.cc = strCCList
end if
if strBccList <> "" then
objNewMail.bcc = strBccList
end if
objNewMail.From = strMailFrom
objNewMail.Subject = strMailSubject
objNewMail.TextBody = strMailMessage
objNewMail.HTMLBody = LinkURLS(toHTML(strMailMessage))
On Error Resume Next
if bDebug then
response.write strMailTo & "<br>" & strBccList & "<br>" & strccList
else
objNewMail.Send
end if
If Err <> 0 Then
' add in some error messages.
CDOSYSMail = Err
else
CDOSYSMail = true
end if
set Flds = nothing
set iConf = nothing
Set objNewMail = nothing
end function
Function LinkURLs(strInput)
Dim iCurrentLocation
Dim iLinkStart
Dim iLinkEnd
Dim strLinkText
Dim strOutput
dim temp
iCurrentLocation = 1
Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)
iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
temp = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
if instr(temp,vbcrlf) > 0 then iLinkEnd = InStr(iLinkStart, strInput, vbCRLf, 1)
if instr(temp,vbCR) > 0 then iLinkEnd = InStr(iLinkStart, strInput, vbCR, 1)
if instr(temp,vbLf) > 0 then iLinkEnd = InStr(iLinkStart, strInput, vbLf, 1)
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case "<br>","/",".", "!", "?",chr(10),chr(13),vbCrLf
iLinkEnd = iLinkEnd - 1
End Select
strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
strOutput = strOutput & "<a href='" & strLinkText & "' title='" & strLinkText & "'>" & strLinkText & "</a>"
iCurrentLocation = iLinkEnd
Loop
strOutput = strOutput & Mid(strInput, iCurrentLocation)
LinkURLs = strOutput
End Function 'LinkURLs
%>
|
|
|
|
06-20-2008, 04:34 AM
|
Re: Help with tell a friend script - email sending
|
Posts: 22,257
Location: Blackpool. UK
|
You don't need to add ALL the code, but the ASP101 version does need the configuration fields adding to it.
Just so the component knows which mail to send the message through and what the authentication user and password is.
If not, it will try and use the "default SMTP server" that is enabled through IIS. Which, if the hosting company are even slightly clued up should be turned off.
|
|
|
|
07-04-2008, 06:26 PM
|
Re: Help with tell a friend script - email sending
|
Posts: 182
|
Thank you
|
|
|
|
02-14-2009, 05:10 PM
|
Re: Help with tell a friend script - email sending
|
Posts: 66
Name: Michael
|
Thanks for referring me to this thread, I have a few questions though, pretty similar to the original post. I have uploaded my .aspx file to my server, but it looks like I need to update my web.config file, which I don't know how to do...
I tried to inlcude the ASP 101 code into my .aspx page, but it looks like it's not working correctly, because I need to configure some web.config for the page to work correctly.
Does the web.config file have to be uploaded separately? Or is it all done in the same .aspx file? Sorry for asking the same questions, but I am a little confused about how it all works and want to make sure I am doing things correctly. Can someone please offer me a little guidance?
Here is the code I am using for my .aspx file, and here is the URL:
Code:
<script type="text/javascript">
function eventsignup() {
var theForm = document.signup;
var temp = theForm.firstname.value;
if ((temp == '') || (temp.charAt(0) == ' ')){
alert("Please enter first name.");
theForm.firstname.focus();
return false;
}
var temp = theForm.lastname.value;
if ((temp == '') || (temp.charAt(0) == ' ')){
alert("Please enter last name.");
theForm.lastname.focus();
return false;
}
var temp = theForm.email.value;
if ((temp == '') || (temp.charAt(0) == ' ')){
alert("Please enter email.");
theForm.email.focus();
return false;
}
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
if(((temp.search(exclude) != -1)||(temp.search(check)) == -1)||(temp.search(checkend) == -1)) {
alert("Invalid email address.");
theForm.email.focus();
return false;
}
var temp = theForm.email.value;
var temp2 = theForm.email2.value;
if (temp != temp2) {
alert("Please retype email address.");
theForm.email2.focus();
return false;
}
}
</script>
<br />
<script type="text/javascript">
function validateform() {
if ((document.resume.sender.value == '') || (document.resume.sender.value.charAt(0) == ' ')) {
alert("Please enter your name.");
document.resume.sender.focus();
return false;
}
if ((document.resume.collName.value == '') || (document.resume.collName.value.charAt(0) == ' ')) {
alert("Please enter your colleague's name.");
document.resume.collName.focus();
return false;
}
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
if(((document.resume.senderemail.value.search(exclude) != -1)||(document.resume.senderemail.value.search(check)) == -1)||(document.resume.senderemail.value.search(checkend) == -1)) {
alert("Please enter a valid email address for yourself!");
document.resume.senderemail.focus();
return false;
}
if(((document.resume.colleagueemail.value.search(exclude) != -1)||(document.resume.colleagueemail.value.search(check)) == -1)||(document.resume.colleagueemail.value.search(checkend) == -1)) {
alert("Please enter a valid email address for your colleague!");
document.resume.colleagueemail.focus();
return false;
}
return true;
}
</script>
<form name="resume" action="http://www.iongeo.com/sites/virtuals/innovation_test_pages_dev" method="post" onsubmit="return validateform();">
<table width="477" border="0">
<tr>
<td width="76" align="right" valign="top"><strong>Your Name:</strong></td>
<td width="391">
<asp:TextBox id="txtFromName" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validNameRequired" ControlToValidate="txtFromName"
errormessage="Please enter your name."
display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Your E-mail:</strong></td>
<td>
<asp:TextBox id="txtFromEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validFromEmailRequired" ControlToValidate="txtFromEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validFromEmailRegExp" ControlToValidate="txtFromEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Friend's E-mail:</strong></td>
<td>
<asp:TextBox id="txtToEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validToEmailRequired" ControlToValidate="txtToEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validToEmailRegExp" ControlToValidate="txtToEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Message:</strong></td>
<td>
<asp:TextBox id="txtMessage" Cols="60" TextMode="MultiLine" Rows="5" ReadOnly="True" runat="server" />
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button id="btnSend" Text="Send Message" OnClick="btnSendMsg_OnClick" runat="server" /></td>
</tr>
</table>
</form>
Any help would be greatly appreciated. Thanks a lot!
Last edited by Mikeface; 02-14-2009 at 05:41 PM..
|
|
|
|
|
« Reply to Help with tell a friend script - email sending
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|