C and S Design.
Search Friendly Programming and Design

Go to -> C and S Design -> Articles -> Code -> ASP -> Send Email -> CDOSYS -> Show Code

Click here to view or download the code for this example



ASP Code Articles

Sending email with CDOSYS

CDOSYS
The Form

CODE:
Get this Code: Click to download the ASP file.
Do not copy and paste the displayed code. The display function adds line breaks and numbers, so what you see is definitely not you would get. The continuation marker " _ " is used to indicate the break.
01: <%
02: dim strSendURL
03: dim strMailMessage
04: dim strMessage
05: dim bSendOk
06: dim bMsgSent
07: dim strFriendName
08: dim strSenderName
09: dim strFriendEmail
10: dim strSenderEmail
11: dim strMailFrom
12: dim strMailTo
13: dim strSenderIP
14: dim intSentMsgID
15: dim frmAction

16: ' strMailSubject is global variable defined in inc_vars_config.asp
17: strMailSubject = strCompanyName & " Tell a Friend"

18: strFriendName = Proper(request.form("to_name"))
19: strSenderName = Proper(request.form("from_name"))
20: strFriendEmail = lcase(request.form("to_email"))
21: strSenderEmail = lcase(request.form("from_email"))
22: frmAction = lcase(left(request.form("send"),4))

23: ' testing values
24: '	strFriendName = "Barney Rubble"
25: '	strSenderName = "Fred Flintstone"
26: '	strFriendEmail = "barney@candsdesign.co.uk"
27: '	strSenderEmail = "fred.flintstone@blooberi.com"

28: strMessage = ToHTML(request.form("message"))

29: strMailFrom = strSenderName  & " <" & strSenderEmail & _ 
	">"
30: strMailTo = strFriendName  & " <" & strFriendEmail & _ 
	">"

31: strSenderIP = request.servervariables("REMOTE_HOST")

32: if frmAction <> "" then
33: strSendUrl = lcase(request.form("send_page"))
34: else
35: strSendURL = request.servervariables("HTTP_REFERER")
36: end if

37: if strSendURL <> "" then
38: 	if instr(strSendURL,domain()) > 0 then
39: 		bSendOk = true
40: 	end if
41: else
42: bSendOk = false
43: end if


44: if bSendOk then
45: strMailMessage = "This message was sent from <a href="
46: strMailMessage = strMailMessage & chr(34)	
47: strMailMessage = strMailMessage & domain()
48: strMailMessage = strMailMessage & chr(34)	
49: strMailMessage = strMailMessage & ">"
50: strMailMessage = strMailMessage & "C and S Design"
51: strMailMessage = strMailMessage & "</a>"
52: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
53: strMailMessage = strMailMessage & "Hello "
54: strMailMessage = strMailMessage & strFriendName
55: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
56: 'strMailMessage = strMailMessage &
57: strMailMessage = strMailMessage & strSenderName
58: strMailMessage = strMailMessage & " visited our site and thought _ 
	you might be interested in "
59: strMailMessage = strMailMessage & "<a href="
60: strMailMessage = strMailMessage & chr(34)	
61: strMailMessage = strMailMessage & strSendURL
62: strMailMessage = strMailMessage & chr(34)	
63: strMailMessage = strMailMessage & ">"
64: strMailMessage = strMailMessage & "this page"
65: strMailMessage = strMailMessage & "</a>"
66: strMailMessage = strMailMessage & " from the website."
67: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
68: if len(strMessage) > 1 then
69: 	strMailMessage = strMailMessage & "And added this message "
70: 	strMailMessage = strMailMessage & "<br>" & vbCrLf
71: 	strMailMessage = strMailMessage & strMessage
72: end if
73: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
74: strMailMessage = strMailMessage & "Please note this message has _ 
	been sent by someone who has your email address, and not directly by C _ 
	and S Design."
75: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
76: strMailMessage = strMailMessage & "You will not receive any _ 
	emails from us as a result of your email address be used for this _ 
	message"
77: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
78: strMailMessage = strMailMessage & "<strong>Thanks and _ 
	Regards</strong>"
79: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
80: strMailMessage = strMailMessage & "From "
81: strMailMessage = strMailMessage & " <a href="
82: strMailMessage = strMailMessage & chr(34)	
83: strMailMessage = strMailMessage & domain()
84: strMailMessage = strMailMessage & chr(34)	
85: strMailMessage = strMailMessage & ">"
86: strMailMessage = strMailMessage & "C and S Design"
87: strMailMessage = strMailMessage & "</a>"
88: strMailMessage = strMailMessage & "<br><br>" & _ 
	vbCrLf
89: 'strMailMessage = strMailMessage &
90: 'strMailMessage = strMailMessage &
91: 'strMailMessage = strMailMessage &
92: 'strMailMessage = strMailMessage &
93: 'strMailMessage = strMailMessage &
94: 'strMailMessage = strMailMessage &

95: if frmAction <> "" then
96: 	if CDOSYSHTMLMail(strMailFrom,strMailTo,strMailMessage) > 0 then
97: '		show error messages go in here
98: 		bMsgSent = false
99: 	else
100: 		bMsgSent = true
101: 	end if
102: end if '	frmAction
103: end if '	bSendOk

104: if bMsgSent then
105: dim strTellSQL
106: strTellSQL = "INSERT INTO " & TellAFriendTable & " _ 
	(from_name,to_name,from_email,to_email,sent_date,sent_time,sender_ip) _ 
	VALUES ('" & strSenderName & "','" & strFriendName & _ 
	"','" & strSenderEmail & "','" & strFriendEmail & _ 
	"','" & date & "','" & time & "','" & _ 
	strSenderIP & "');"
107: InsertTellAFriend(strTellSQL)
108: end if


109: %>


110: <html>
111: <head>
112: <title>C and S Design - Tell a Friend Page</title>
113: <meta name="description" content="" >
114: <meta name="keywords" content="" >

115: </head>

116: <body>
117: <div class="main">
118: 
119: <h2>Tell a Friend</h2>
120: <%
121: if bMsgSent then
122: %>
123: Thank You.
124: <br>
125: Your message to <%=strFriendName%> has been delivered.
126: <br>
127: Please note that your IP <%=strSenderIP%> and the email _ 
	address used has been stored in case of any complaints of UCE _ 
	(Unsolicited Commercial Email). This email address will not be used _ 
	for any emails from <%=strCompanyName%>. In case of _ 
	complaints these details, along with the date and time will be sent on _ 
	to the ISP concerned.
128: <br>
129: We do take SPAM complaints seriously and we will take action in all _ 
	cases.
130: <br>
131: Thanks from <%=strCompanyName%>
132: <%
133: end if

134: if bSendOk = true then
135: %>
136: <form name="tell-a-friend" method="post" _ 
	action="">
137: <div class="row">
138: Send a friend a link to this page <%=strSendURL%>
139: </div> <!-- row -->
140: <iframe style="float:left;width:500px;height:300px;" _ 
	src="<%=strSendURL%>"></iframe>
141: <input type="hidden" name="send_page" _ 
	value="<%=strSendURL%>">
142: <div style="clear:left;"></div>
143: <div class="row">
144: <strong>To:</strong>
145: </div> <!-- row -->
146: <div class="row">
147: 	<div class="cell_lab">
148: 	Name:
149: 	</div> <!-- cell_lab -->
150: 	<div class="cell_inp">
151: 	<input type="text" name="to_name" _ 
	size="33" _ 
	value="<%=strFriendName%>">
152: 	</div> <!-- cell_inp -->
153: </div> <!-- row -->
154: <div class="row">
155: 	<div class="cell_lab">
156: 	Email:
157: 	</div> <!-- cell_lab -->
158: 	<div class="cell_inp">
159: 	<input type="text" name="to_email" _ 
	size="33" _ 
	value="<%=strFriendEmail%>">
160: 	</div> <!-- cell_inp -->
161: </div> <!-- row -->
162: <div class="row">
163: <strong>From:</strong>
164: </div> <!-- row -->
165: <div class="row">
166: 	<div class="cell_lab">
167: 	Name:
168: 	</div> <!-- cell_lab -->
169: 	<div class="cell_inp">
170: 	<input type="text" name="from_name" _ 
	size="33" _ 
	value="<%=strSenderName%>">
171: 	</div> <!-- cell_inp -->
172: </div> <!-- row -->
173: <div class="row">
174: 	<div class="cell_lab">
175: 	Email:
176: 	</div> <!-- cell_lab -->
177: 	<div class="cell_inp">
178: 	<input type="text" name="from_email" _ 
	size="33" _ 
	value="<%=strSenderEmail%>">
179: 	</div> <!-- cell_inp -->
180: </div> <!-- row -->
181: <div class="row">
182: 	<div class="cell_lab">
183: 	Message:
184: 	</div> <!-- cell_lab -->
185: 	<div class="cell_inp">
186: 	<textarea name="message" rows="3" _ 
	cols="25"></textarea>
187: 	</div> <!-- cell_inp -->
188: </div> <!-- row -->
189: <div class="row">
190: <input type="submit" name="send" _ 
	value="Send Mail">
191: </div> <!-- row -->

192: </form>
193: <div class="spacer"></div>
194: <%
195: '	response.write strMailMessage
196: else
197: %>
198: Sorry, No site page address selected to send.
199: <br><br>
200: This may be due to arriving on this page from a search engine or your _ 
	computer is set not to allow a referering page to be sent.
201: <br>

202: <%
203: end if
204: %>
URL Linker




Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design