Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp mail

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i am trying to use the script below to retrieve info put into an html form into an email that gets sent to me....the script below is working and i do get the email when the user inputs the data but i want to add more fields like home address, age, etc.......not just the ones specified in the script ex.fromAddress, subject, bodytext....any suggestions?
<%
Set Mailer = Server.CreateObject (&quot;SMTPsvg.Mailer&quot;)

Mailer.FromName = Request.Form (&quot;FromName&quot;)
Mailer.FromAddress = Request.Form(&quot;FromAddress&quot;)
Mailer.Subject = Request.Form (&quot;Subject&quot;)
Mailer.BodyText = Request.Form (&quot;Bodytext&quot;)
Mailer.RemoteHost = &quot;mail-fwd.verio-web.com&quot;

Mailer.AddRecipient Request.Form(&quot;ToName&quot;), Request.Form(&quot;ToAddress&quot;)

if Mailer.SendMail then
' Message sent sucessfully
response.write (&quot;Your message was sent&quot;)
else
' Message send failure
response.write (&quot;Your message was not sent. &quot;)
response.write (&quot;The error was: &quot; & Mailer.Response)
end if
%>
 
try this and put it into the mailer.bodytext, that is where all form fields can be added
strMsgHeader = &quot;Form information follows&quot; & vbCrLf
for each frmItem in Request.Form
strMsgInfo = strMsgInfo & frmItem & &quot; - &quot; & request.Form(frmItem) & vbCrLf
next
strMsgFooter = vbCrLf & &quot;End of form information&quot;
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter

You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
You would append it in the bodytext. For example, instaed of just

Code:
Mailer.BodyText = Request.Form (&quot;Bodytext&quot;)

you could do

Code:
Dim strBodyText

strBodyText =               &quot;Age: &quot; & Request.Form(&quot;Age&quot;) & chr(13)
strBodyText = strBodyText & &quot;Address: &quot; & chr(13)
strBodyText = strBodyText & Request.Form(&quot;Address1&quot;) & chr13)
strBodyText = strBodyText & Request.Form(&quot;Address2&quot;) & chr13)
strBodyText = strBodyText & Request.Form(&quot;Address3&quot;) & chr13)


Mailer.BodyText = strBodyText

 
why not do a loop thru all the fields. Seems like a lot more work to add them all seperatly as that. You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
Very true and thus your way would be good if he wants all fields displayed. The way I mention is more work yes, but gives more control over titles if you want them to be different than the name of the fields as well as overall display control. For example, most sites use separate lines for the number, address, street, city... however for display purposes you may want to format the text as it would appear in a paper mailing with only one main tile &quot;Address&quot;. Both ways will accomplish the same thing in getting the information across, it all depends on how the developer wants the end product to look like.

Mike

 
To add to that. As you said some have seperate lines for address etc. Seeing as the form is passed with the name value all you would really need to do is have your naming convention like
<input type=&quot;text&quot; name=&quot;home address&quot;>
<input type=&quot;text&quot; name=&quot;business address&quot;>
so the results would be
home address=1111-11
business address=2222-22

If you want to get into exctensive naming then the long way is deffinetly the way to go to avoid errors
You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
regarding the asp mail, i hv a question here..

i'm using CDONTS to send emails, i wonder if is it possible to auto-generate the email to recipients without clicking any buttons or submit any forms?

wat i mean is, can i make the system automatically send an email to notify a user ..lets say his email account has expired or nitify him 3 days b4 the expiry date?
 
Sure you can. There are different ways to do it. First you have to decide if you want to run the script to do this manualy or atomatically at a scheduled time. By the sounds of your request I would suggest automatically at a sceduled time and you would likely use a .vbs file for this. You can use the NT 'at' scheduler to execute your script. Now for the script itself, I am guessing you store the expiry date in a database, so all you would need to do is have logic in the script to determine if it is three days before that date and fire off an email to that user.

I actually have a script that I wrote that sends an email reminder to a group of users if they have not completed a task at a set date and time. Here is a trimmed down version of a vbs script that you could adapt for your purposes:

Code:
'--------------------------------------------------------------------
'
' Mailout using CDONTS.NewMail
'
'--------------------------------------------------------------------

' declare all variables
Option Explicit

Dim strFrom, strBody, strSubject
Dim EmailSuccess, EmailCC, EmailFailure, EmailDebug
Dim oCon  
Dim WshNetwork
Dim strServerName
Dim blnCon
Dim blnSendMail
Dim ThisDate
Dim DayOfWeek
Dim DayOfMonth
Dim WeekStartDate
Dim WeekEndDate
Dim MonthStartDate
Dim MonthEndDate
Dim blnDebugOn

blnDebugOn = false	' if blnDebugOn is true, email will be sent to developer with more detailed error description

' mail constants (some are for reference)
Const CdoBodyFormatHTML = 0 ' Body property is HTML
Const CdoBodyFormatText = 1 ' Body property is plain text (default)
Const CdoMailFormatMime = 0 ' NewMail object is in MIME format
Const CdoMailFormatText = 1 ' NewMail object is plain text (default)
Const CdoLow    = 0         ' Low importance
Const CdoNormal = 1         ' Normal importance (default)
Const CdoHigh   = 2         ' High importance

'ADO constants
Const adCmdText = &H0001
Const adCmdTable = &H0002
Const adCmdStoredProc = &H0004
Const adInteger = 3
Const adParamInput = &H0001

'Server constants
Const DevServer  = &quot;DevServerName&quot;
Const TestServer = &quot;TestServerName&quot;
Const ProdServer = &quot;ProdServerName&quot;

'ConnectionString
Const strDBConnection = &quot;dsn=<<DSN_name>>;uid=<<username>>;pwd=<<password>>&quot;

' email subject and sender
strFrom  = &quot;yourname@domain.com&quot; 

' determine environment and set email recipients as appropriate
Set WshNetwork = WScript.CreateObject(&quot;WScript.Network&quot;)
strServerName = Trim(CStr(WshNetwork.ComputerName))

Select Case UCase(strServerName)
	Case DevServer
		strSubject   = &quot;Time Entry Reminder - Dev &quot;  
		EmailSuccess = &quot;developer@company.com&quot;
		EmailFailure = &quot;developer@company.com&quot;	
	Case TestServer  
		strSubject   = &quot;Time Entry Reminder - Test &quot; 
		EmailSuccess = &quot;developer@company.com&quot;
		EmailFailure = &quot;developer@company.com&quot;		
	Case ProdServer   
		strSubject   = &quot;Time Entry Reminder&quot; 
		EmailSuccess = &quot;appadmin@company.com&quot; 
		EmailCC      = &quot;developer@company.com&quot;
		EmailFailure = &quot;appadmin@company.com&quot;		
End Select
If blnDebugOn Then
	EmailSuccess = &quot;developer@company.com&quot;
	EmailFailure = &quot;developer@company.com&quot;
	EmailDebug   = &quot;developer@company.com&quot;	
End If

'Determine if it is the end of the work week or end of month
ThisDate = Date()
DayOfWeek = WeekDay(ThisDate)
DayOfMonth = Day(ThisDate)
WeekStartDate = DateAdd(&quot;y&quot;, -(DayOfWeek), ThisDate)
WeekEndDate = DateAdd(&quot;y&quot;, 6, CDate(WeekStartDate))
MonthStartDate = CDate(Year(ThisDate) &&quot;/&quot;& Month(ThisDate) &&quot;/01&quot;)
MonthEndDate = DateAdd(&quot;y&quot;, -DayOfMonth, DateAdd(&quot;m&quot;, 1, ThisDate))

' if today is the end of the work week or end of month then send the emails
If ThisDate = WeekEndDate Or (ThisDate = MonthEndDate And (WeekDay(ThisDate) >= 2 And WeekDay(ThisDate) <= 6)) Then
	blnSendMail = true
Else
	blnSendMail = false
End If

blnCon=OpenConnection()

If blnCon And blnSendMail Then
	Call MailoutReminders()
	Call CloseConnection()
ElseIf blnCon And Not blnSendMail Then
	'End Script
Else
	' failure notification
	strBody = &quot;<font face=Tahoma color=red size=3pt>Unable to send time entry reminder emails because of database connection error!</font>&quot;
	Call Send_Email(EmailFailure, strBody, &quot;&quot;)
End IF		
		

'=====================================================
'	Functions and Subroutines
'=====================================================

Sub MailoutReminders()
	' ADO for database
	Dim oRs 
	Dim oCmd
	Dim emailCount
	Dim userCount
	Dim strTo
	Dim strCC
	Dim strEmailSentToOnFail

	emailCount = 0	
	userCount = 0
	
	On Error Resume Next

	Set oCmd=CreateObject(&quot;ADODB.Command&quot;)
	Set oRs=CreateObject(&quot;ADODB.Recordset&quot;)
	
	' failure notification
	If Err.number<>0 Then
		Err.Clear
		strBody = &quot;<font face=Tahoma color=red size=3pt>Unable to send time entry reminder emails -- Creating ADO objects failed!</font>&quot;
		Call Send_Email(EmailFailure, strBody, &quot;&quot;)
		Exit Sub
	End If	

	oCmd.ActiveConnection = oCon
	oCmd.CommandType = adCmdStoredProc
	oCmd.CommandText = &quot;p_time_entry_email_reminder&quot;
	Set oRs=oCmd.Execute

	' failure notification
	If Err.number<>0 Then
		Err.Clear
		strBody = &quot;<font face=Tahoma color=red size=3pt>Unable to send time entry reminder emails -- Executing stored procedure p_time_entry_email_reminder failed!</font>&quot;
		Call Send_Email(EmailFailure, strBody, &quot;&quot;)
		Exit Sub
	End If	
	
	' storedproc execution successful
	If Not oRs.EOF Then
		While Not oRs.EOF
			userCount = userCount + 1	

			If blnDebugOn Then
				strTo = EmailDebug
			Else
				strTo = oRs(&quot;Email&quot;)
				strCC = &quot;usermanager@company.com&quot;
			End If
		
			strBody = Email_Template(oRs(&quot;FirstName&quot;),oRs(&quot;LastName&quot;))
			
			If Err.number=0 Then
				Call Send_Email(strTo, strBody, strCC)
				If Err.number=0 Then
					emailCount = emailCount + 1
					If Err.number<>0 Then
						strEmailSentToOnFail = strEmailSentToOnFail & &quot;/&quot; & oRs(&quot;Userid&quot;)
						Err.Clear
					End If
				Else
					Err.Clear
				End If
			Else
				Err.Clear
			End If
						
			oRs.MoveNext
		Wend
	End If
 
	oRs.Close 
	
	' successful email
	strBody = emailCount & &quot; of &quot; & userCount & &quot; time entry reminding email(s) have been sent on &quot; & date() & &quot; at &quot; & time() & &quot;.<br>&quot; 
	Call Send_Email(EmailSuccess, strBody, EmailCC)
		
	' failure notification
	If (emailCount <> userCount) or (strEmailSentToOnFail<>&quot;&quot;) Then
		strBody = &quot;<table border=&quot;&quot;0&quot;&quot; width=&quot;&quot;500&quot;&quot;><tr><td><font face=Tahoma color=red size=3pt>&quot;
		strBody = strBody & &quot;Total emails should be sent: &quot; & userCount & &quot;<br>&quot;
		strBody = strBody & &quot;Number of emails actually sent: &quot; & emailCount & &quot;<br><br>&quot;
		If strEmailSentToOnFail<>&quot;&quot; Then
			strBody = strBody & &quot;Reminding emails have been sent to the following users:<br>&quot;
 			strBody = strBody & strEmailSentToOnFail & &quot;<br><br>&quot;
		End If
		strBody = strBody & &quot;</font></td></tr></table>&quot;
		Call Send_Email(EmailFailure, strBody, &quot;&quot;)
	End If
End Sub

'function to open db connection
Function OpenConnection()
	' assume failure
	OpenConnection = false

	On Error Resume Next

	Set oCon=CreateObject(&quot;ADODB.Connection&quot;)
	If Err.number <> 0 Then
		Err.Clear
		Exit Function
	End If

	oCon.ConnectionString = strDBConnection
	oCon.Open
	If Err.number <> 0 Then
		Err.Clear
		Exit Function
	End If
	OpenConnection = true
End Function

'sub to close db connection
Sub CloseConnection()
	oCon.Close
	Set oCon = nothing
End Sub

'function to invoke cdonts
Sub Send_Email(sendTo, contents, ccTo)
	Dim objSendMail	

	' create and initialize mail object       
	Set objSendMail = CreateObject(&quot;CDONTS.NewMail&quot;)
	objSendMail.From    = strFrom
	objSendMail.To = sendTo
	If ccTo <> &quot;&quot; Then
		objSendMail.CC = ccTo
	End If
	objSendMail.Subject = strSubject 
	objSendMail.Body = contents
	
	objSendMail.BodyFormat = CdoBodyFormatHTML
	objSendMail.MailFormat = CdoMailFormatMime
	objSendMail.Importance = CdoHigh

	objSendMail.send
	Set objSendMail = Nothing
End Sub


'=======================================================================================
' email template
'=======================================================================================
Function Email_Template(FirstName,LastName)	
	Email_Template = &quot;<font face=&quot;&quot;Arial, Helvetica, sans-serif&quot;&quot; size=&quot;&quot;3&quot;&quot;>&quot;
	Email_Template = Email_Template & &quot;<table border=0 cellpadding=1 cellspacing=1>&quot;
	Email_Template = Email_Template & &quot;<tr><td>&quot;
	Email_Template = Email_Template & &quot;&quot; & FirstName & &quot; &quot; & LastName & &quot;<br><br>&quot;
	Email_Template = Email_Template & &quot;blah blah blah blah.<br><br>&quot;
	Email_Template = Email_Template & &quot;</td></tr>&quot;
	Email_Template = Email_Template & &quot;</table>&quot;
	Email_Template = Email_Template & &quot;</font>&quot;	
End Function

Okay I think my example just got excessive. Oh well, hope it helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top