-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection




 Lesson 9: Send e-mails from your script!
Lesson 9: Send e-mails from your script!

Author: Alexander Haneng
Difficulty: Medium
Requires: ASP
Demo: n/a
Download: Lesson9.zip
Summary:
Learn to use JMail to send emails from your ASP scripts.

Intro:
To send e-mail from your scripts are an extremely useful feature. I use this for five purposes:
1) Send the content of a submitted form to my email address. A good idea if you don't have a db to store the info in.
2) Send mail to a user when he registers for something, this can help me verify that the email address is correct, and I can welcome him as a new user etc.
3) If I got a password protected site, I can make a button that sends the password to the user if he has forgotten it.
4) Let the user mail others. E.g. a "Tell a friend" script.
5) The fifth and incredibly cool purpose is error reporting. Let's say that you sell a product, and you got a order form for it. But something's wrong, so all the users get an error message. The users are angry that they can't make the order, and will probably not bother to send you a mail telling you about it. The result is that you get no sales. I have made a function that if a critical page generates an error message, this message is mailed directly to me. Then I can fix the error, and my users are happy.

What do I need?
You need two things. A mail component and a mail server. Let's start with the mail component. I recommend JMail because it's free, and it's simply the best free email component out there. (It won a test on 15Seconds). You have to fill out a short form to download it, but that's a cheap price to pay for this great software.
So download it now: Jmail download site
If you got hosting on Softcom, you got JMail preinstalled!
You can use any kind of mail server that you have access to.


Let's start on the code:
Page 1, the form: form.asp
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="jmail.asp">
Your email address: <INPUT TYPE="TEXT" NAME="MailTo"><BR>
The subject: <INPUT TYPE="TEXT" NAME="subject"><BR>
Your message: <INPUT TYPE="TEXT" NAME="body"><BR>
<INPUT TYPE="SUBMIT" VALUE="Send mail">
</FORM>
</BODY>
</HTML>

Page 2, the mail script and confirmation page: jmail.asp
<HTML>
<HEAD>
<TITLE>Confirmation </TITLE>
<BODY>
<%
Set JMail = Server.CreateObject("JMail.SMTPMail")
' This is my local SMTP server
JMail.ServerAddress = "mail.yourdomain.com:25"

' This is me....
JMail.Sender = "myemail@mydomain.net"
JMail.Subject = Request.Form("Subject")

' Get the recipients mailbox from a form (note the lack of an equal sign).
JMail.AddRecipient Request.Form("MailTo")

' The body property is both read and write.
' If you want to append text to the body you can
' use JMail.Body = JMail.Body & "Hello world!"
' or you can use JMail.AppendText "Hello World!"
' which in many cases is easier to use.
JMail.Body = Request.Form("body")

' 1 - highest priority (Urgent)
' 3 - normal
' 5 - lowest

JMail.Priority = 3

'JMail.AddHeader "Originating-IP",
'Request.ServerVariables("REMOTE_ADDR")

' Must make sure that IUSR_???? has access to the following files.
' I've disabled the attachments and footer in this demo
'JMail.AppendBodyFromFile "e:\mail\standard_footer.txt"
'JMail.AddAttachment "e:\products\MyProduct.exe"

JMail.Execute

%>

<CENTER>
An e-mail has been sent to your mailbox (<%=request.form("MailTo")%>).
</CENTER>
</BODY>
</HTML>

Download the code: lesson9.zip

There are a lot of email components out there and most of them work roughly the same way as JMail. You usually find a sample ASP script using the component on the component creator's website.

Hope this could help you get starting! Good luck!

Where to go next:
Take a look at our lessons page to get an overview of all our lessons.


8: Bake a cookie!
10: Master your strings!
| Info |
© Copyright 1997-2012 Alexander Haneng