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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an Email Function In ASP

Status
Not open for further replies.

JDOne

Programmer
Apr 9, 2003
40
US
Greetings,

I'm fairly new to asp programming and would like a little help regarding sending email in asp. I have the following code:

<%

Set oMailMessage = Server.CreateObject(&quot;CDONTS.NewMail&quot;)

'Settings for NewMail object and send'
oMailMessage.from = &quot;messagecenter@def.com&quot;
oMailMessage.to = &quot;abc@def.com&quot;
oMailMessage.Importance = 2
oMailMessage.bodyFormat = 1
oMailMessage.MailFormat = 0
oMailMessage.subject = &quot;Firm Notification: Draft Message&quot;
sMessage = &quot;This is a Draft Message. &quot; & vbcrlf & vbcrlf
sMessage = sMessage & &quot;Name: &quot; & vbcrlf & vbcrlf
sMessage = sMessage & &quot;Email: &quot; & vbcrlf & vbcrlf
sMessage = sMessage & &quot;Thank you&quot;
oMailMessage.body = sMessage
oMailMessage.Send()
Set oMailMessage = Nothing

%>

My question is, how do I incorporate this code into a function which can be called by an HTML button object something like this:

<input type=&quot;button&quot; value=&quot;Send Email&quot; onclick=&quot;sendMail()&quot;>

Your help is appreciated.

Sincerely,

JD
 
Generally, we would put the Mail Code into a separate page, which is launched when the submit button is clicked.

Your Mail-Code would then operate using the variables submitted by the form, and, if successful, you would re-direct (or transfer) back to your email page... or, if an error occurred, report the error to the user.

Another way to get his result without actually &quot;switching&quot; pages, is to place a hidden IFRAME on your page, and submit the form to that page...

Here's some pseudo-code to illustrate the idea...
Code:
<html>
<head>
<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
function setTxtMsg(typ, msg){
  var fColor = (typ == 'err') ? 'red':'blue';
  var divObj = document.getElementById('txtMsg');
  divObj.style.fontColor = fColor;
  divObj.innerHTML = msg;
}
</script>
</head>
<body>
<div id=&quot;txtMsg&quot; style=&quot;text-style:bold;&quot;></div>
<form target=&quot;ifrm&quot; action=&quot;procMail.asp&quot;>
...your input fields go here...
<input type=&quot;submit&quot; value=&quot;send mail&quot;>
</form>
<iframe id=&quot;ifrm&quot; style=&quot;display:none;&quot; src=&quot;procMail.asp?do=nothing&quot;></iframe>
</body>
</html>
[code]
...then your &quot;procMail.asp&quot; page, which actually submits the emails and reports the messages looks something like:
[code]
option explicit
dim doWhat
doWhat = request.queryString(&quot;do&quot;)
if doWhat = &quot;nothing&quot; then  ' get outta here
  response.write(&quot;<html><body></body></html>&quot;)
  response.end
end if
%>

...your MAIL-CODE goes in here, using the elements submitted by the form...

<%
if successful then
  txtMsg = &quot;Your EMail has been Sent&quot;
  typ = &quot;OK&quot;
else
  txtMsg = &quot;Your EMail Failed!&quot;
  typ = &quot;err&quot;
end if
%>
<html onload=&quot;top.document.setTxtMsg('<%=typ%>','<%=txtMsg%>');&quot;>
  <body>
    <%=typ%>
  </body>
</html>
 
Thanks for the responses. Let me provide a little more information.

I actually need to have an email button placed on a results page of an executed query providing the user with the ability to send a canned email.

I first tried placing the email code in a .inc file but all that did was cause the email to be sent upon loading up the page. What the users want is either a button or a link which, when upon clicking it, the email would be sent to that person's account - in this case, Outlook.

I'm familiar with using JavaScript and calling the &quot;onClick&quot; method to set off events, but its different with ASP and the email code described in my first post. Is there code in vb script that would be appropriate in doing this type of a function?

Thanks again,

JD
 
The Javascript onclick event runs on the CLIENT side whereas the code you have in the ASP runs on the SERVER side, you would need to submit your form to the ASP to have it processed and sent out, simply turn it into a sub and then call it as you would any other sub.

The only way to do this client side is to use the MAILTO tag, but this is not a good option and can fail if you are not using 'standard' - i.e. M$ - mail and web clients.
 
Heres an alternative using XML.
Where it says svalues just add the extra information you use building it up like a querystring, this examples uses asp variables inserted into it. Then on the button click it calls the routine which executes emailpage.asp which is where you have your email code.

<script language=&quot;JavaScript&quot;>
function SendEmail(){
sValues = &quot;email=&quot; + <%=email%> + &quot;&body=&quot; + <%=body%>;
var oXMLHttp = new ActiveXObject(&quot;MSXML2.XMLHttp&quot;);
oXMLHttp.Open(&quot;POST&quot;,&quot; false);
oXMLHttp.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x- oXMLHttp.Send(sValues);}
</script>

<input type=button value=&quot;Email&quot; name=&quot;Email&quot; onclick=&quot;SendEmail()&quot;>

 
Thanks all, for your help. Now that I got the send mail part working, the users want additional enhancements (what else is new???) Anyway, this is what they want: The query results returns two fields, a Draft ID # and a Client Name. Beside each result they want a button which when clicked will send email to the individual whose email was entered initially. (The results are fetched based upon the email address entered).

I placed the code for the send mail button within the while loop (while not rstResult.EOF) so that next to each result, the send mail button appears. So far so good! I also tested the sendMail method and hard coded my own email address to make sure it worked and it works. What they want done is when the user clicks on the sendMail button, a canned email message is sent to the user containing a link to that person's draft based upon the Draft ID # that is a part of the result set. Placing the link to the server in the canned email I had no problem with, the issue now is how do I extract the DraftID # that appears in each line of my result set and incorporate it into the link that's a part of the canned email address? This is where I'm stuck at this moment. Once I get this piece functional, the task is done.

If anyone can offer any insight, it would be greatly appreciated.

Thanks.

Sincerely,

JD
 
Why not have a hidden form and change the button so that it looks something like :

<form name=&quot;myForm&quot; action=&quot;mail.asp&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;draftid&quot;>
<input type=&quot;hidden&quot; name=&quot;emailto&quot;>
...

<input type=&quot;button&quot; value=&quot;One&quot; onclick=&quot;go(1,'gg@gg.org,two@me.org')&quot;>
<input type=&quot;button&quot; value=&quot;Two&quot; onclick=&quot;go(2,'justme@home.com')&quot;>
</form>

<script language=&quot;javascript&quot;>
function go(draftID,emailTo)
{
document.myForm.draftid=draftID;
document.myForm.emailto=emailTo;
document.myForm.submit();
}
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top