Link to home
Start Free TrialLog in
Avatar of eckrothmusic
eckrothmusic

asked on

htlm or java script code to send email

I would like to put embedded code in a web page so that if someone visited that page on our website it would send an email to a given address.  

I don't want to have any submit buttons or anything visible to the web page visitor.  I want it to happen behind the scene.
Avatar of dimmergeek
dimmergeek
Flag of United States of America image

You cannot do this purely with HTML and/or JavaScript.
You will need a scripting language such as ASP.NET or PHP.
Here is some PHP code that you can run each time the page is called...

put this at the VERY TOP of your web page and re-name it as .php extension.

<?php
	// send e-mail on page load
	$page = $_SERVER['REQUEST_URI'];
	$fromadd = "mailfrom@domain.com";
	$to = "mailto@domain.com";
	$subject = "The web page " . $page . " has been visited";
	$mailText = "The web page " . $page . " has been visited";
	$mymail = "<html><body bgcolor='#FFFFFF'>" . $maillText . "</body></html>";
	$mymail = str_replace("\n.", "\n..", $mymail);
	$mymail = wordwrap($mymail, 70, "\r\n");

	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	// Additional headers
	$headers .= "To: tomail@domain.com <tomail@domain.com>\r\n";
	$headers .= "From: frommail@domain.com <frommail@domain.com>\r\n";
	mail( $to, $subject, $mymail, $headers);
%>

Open in new window

If you are loking to track visitors, there are better methods.  THe code above will do what you asked, but if someone visits the same page multiple times, you'll get multiple e-mails.
You're better off setting a cookie value of the vistor's IP address and writing the visit to a database.  Then on re-visits, you can check for the presence of the cookie value and determine whether to write to the db or not.
Maybe you don't want to counrt visits back to the same page by the same user within 1/2 hour or something like that.
Avatar of eckrothmusic
eckrothmusic

ASKER

ok.  this looks good!  

I am actually putting this on an error page so that I am notified if a user gets this page.

Question:  in your code where do I change the frommail and tomail to the actually email addresses?
ASKER CERTIFIED SOLUTION
Avatar of dimmergeek
dimmergeek
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks!
You're welcome.
Everywhere you see to@domain.com, substitute with your desired e-mail address.
Everywhere you see from@domain.com, substitute with your desired e-mail address.
Does the error page accept input from the user from another page?
Are their variables you're expecting to be POSTed?
YOu can include those in the e-mail...