How to send emails in JavaScript
using ASP/PHP
Have you ever wanted to send emails directly in
Javascript?
This tutorial will teach you how to send emails in Javascript in a seamless way
and also to be compatible with Internet Explorer 4+ and Netscape 4+ !!!
You will find attached a ZIP file the HTML page
of this tutorial, the sendemail .js, .asp, and .php files, and also an email
sender demo where you can find all the source code to make a simple email sender
based on Javascript and ASP/PHP.
If you want any information added or you have any questions, post feedback.
To allow sending emails you must have ASP or PHP
enabled on your server, also your server must allow to send emails.
If you only have ASP on your server check if it has a mail sending component
named CDONTS, which is the most used by IIS servers, contact your webserver
admin to check that feature.
And now, how Javascript can send mails?
Well, really, client side Javascript doesn't has features to send mails, that's
why ASP and/or PHP is used.
What javascript does is to call a ASP/PHP page, then send some parameters to
that pages, and then that pages will send the emails, and now, to make the email
process fast and seamless, the ASP/PHP will be loaded as if they were a
javascript file, for example look at this code:
<script
language="JavaScript" id="sendemail" src></script>
<script>
document.scripts.sendemail.src="sendemail.php?address=user@email.com&message=Hello&subject=Hello+User!";
</scirpt>
What does this code does is to call a PHP send email page
and pass some parameters to it, as if it was a javascript file, then the PHP
page sends the email, and returns some javascript content like "var
EmailSended=true;", so you can know the email was sended.
Now, this email send interface may sound quite complex or
nonpractical to implement, that's way the code is already made so you can send
emails with ease, first, you will have to download the ZIP file attached and
copy the 3 files in there (sendemail.js, sendemail.asp, and sendemail.php) to
your web server, then include this line of code on the pages where you will be
sending emails:
<script
language="JavaScript" src="sendemail.js"></script>
Now, to send emails you only need to make some easy code
like this, the code was made so it as easy to use as possible and full of
features like including mail subject, message, reply-to address, and CC/BCC
addresses.
Note: Seamless email sending is only enabled on
Internet Explorer version 4 or greater, on other browsers like Netscape, Opera,
Mozilla, etc, a small popup will open the ASP/PHP page, and then closed after a
few seconds.
Now here are is the code to send a mail with all these
features, don't forget to include the 3 "sendemail" js, php, and asp
files in your server, and also to include the
<script
language="JavaScript" src="sendemail.js"></script>
line in your web page.
//First create the Mail()
object and set in on a variable
var easymail=new Mail();
//Set the server type, so the
script can know which page to call, the asp or php, if you are using a PHP
server
//replace the "ASP_SERVER" wit
// h "PHP_SERVER", by default the
//
server is set to PHP_SERVER
//NOTE: If you choose
ASP_SERVER you must have CDONTS component installed
//in your server in order to send the em
// ails
easymail.ServerType=ASP_SERVER;
//Now set the address to which
the email will be sended
easymail.To="username@email.com";
//Add the CC address
easymail.Cc="username2@email.com";
//Add the BCC address
easymail.Bcc="username3@email.com";
//Now set the address of the
one that sends the email
easymail.ReplyTo="me@email.com";
//Now set the subject of the
email
easymail.Subject="Here goes the subject of the email";
//Set the message of the email
easymail.Message="Here is the content of the email message, if you want to
split text into lines so it can be readable, you can use the '\r\n' characters,
or better";
//Send the email
easymail.Send();
Now, if you want to know if the ASP/PHP was
already loaded and/or if you want to know if the email was sended correctly, you
can use some special variables which are:
1. EmailSenderAccessed - If it is set to true then the ASP/PHP email page was
loaded
2. EmailSended - If it is set to true then the email was sended correctly
I have included some code so you can know if the
email page was loaded and if the email was sended correctly, add the following
code to the previous one.
function CheckEmail(){
//Show the message in the status bar
//
window.status="Sending email...";
//Check if the ASP/PHP email page was al
// ready loaded
if(EmailSenderAccessed){
//Now check if the email was sended or n
// ot and show the message
if(EmailSended) window.alert("The email was sended succesully!");
else window.alert("The email could not be sended");
}
//If ASP/PHP email page is not loaded ye
// t, then check it again in 0.5 seconds
(500 milliseconds)
else{
setTimeout("CheckEmail();", 500);
}
}
//Send the email
easymail.Send();
//Now call the function what will alert
// user when email was sended
CheckEmail();
Is as easy as it looks, if you liked this
tutorial please vote and/or post your comments/feedback of it, it would be great
:) |