...

View Full Version : send an email when user clicks a link



~Kira
12-07-2010, 06:54 PM
I want to send a simple email with just email & subject only when the user clicks a link that opens a pdf. I'm thinking javascript is the best way to do this but I have no idea how. I have googled it a bunch but most information is about getting an email when someone clicks a link you send in an email. I don't want that. This is on a website. I'm not sure if php would be better to use. Any help would be most appreciated!

Old Pedant
12-07-2010, 07:47 PM
You can't send email automatically from JavaScript, alone. At best, you could ask the user's email client to send an email, but that only works with *SOME* email clients and, in any case, requires the user to allow the send.

So, yes, you should do this via PHP in the back end. You might want to *invoke* the PHP code from JS, possibly by making an AJAX request. But the actual email needs to be sent from the server, not from the browser.

~Kira
12-11-2010, 04:58 PM
Thank you Old Pendant. I combined info from 2 different forum posts and ended up getting it to work, however I had to redirect to the pdf (the original link the user clicks on) in php or else the button disappeared and the user stayed on the same page.

Here is the javascript in the head:


<script type="text/javascript">
function email(){
document.getElementById("user_click").innerHTML="<?php
@mail('me&mysite.com', 'User Click', $_POST['bodysHTML']);
header('Location: http://mysite.com/pdf/asdf.pdf');
?>";
}
</script>

not sure if I needed the span tag because I could have just had the image id="user_click" but it's there anyway.


and Here is the button (link) the user clicks on that emails:


<a href="/pdf/asdf.pdf" onclick="email(); return false;"><span id="user_click"><img src="/images/image.jpg" alt="PDF" width="97" height="43" /></span></a>

abduraooft
12-11-2010, 05:17 PM
<script type="text/javascript">
function email(){
document.getElementById("user_click").innerHTML="<?php
@mail('me&mysite.com', 'User Click', $_POST['bodysHTML']);
header('Location: http://mysite.com/pdf/asdf.pdf');
?>";
}
</script> As told above, what you are trying to do is impossible! You need to have a serverside script like php for this. See an example in php at http://php.net/mail

~Kira
12-11-2010, 05:21 PM
As told above, what you are trying to do is impossible! You need to have a serverside script like php for this. See an example in php at http://php.net/mail

well this works so I'm happy with javascript & php the way it is.


mail ( string $to , string $subject

besides, isn't this what I have? lol

abduraooft
12-11-2010, 05:26 PM
besides, isn't this what I have? lol Yes, bu you can't call a javascript function from the client side to execute PHP inside it, as you did.

Normally, all the exceution of the serverside code takes place before login the page at client side. The simple way is to make a form and put that image as a submit button inside it, to submit that form to a php script having the sufficient code to send mail.

<form method="post">
........
<imput type="image" src="/images/image.jpg" alt="PDF" width="97" height="43" >
</form> Hope you are you familiar with this(?)

~Kira
12-11-2010, 05:40 PM
Yes, bu you can't call a javascript function from the client side to execute PHP inside it, as you did.

Normally, all the exceution of the serverside code takes place before login the page at client side. The simple way is to make a form and put that image as a submit button inside it, to submit that form to a php script having the sufficient code to send mail.

<form method="post">
........
<imput type="image" src="/images/image.jpg" alt="PDF" width="97" height="43" >
</form> Hope you are you familiar with this(?)

yes I was thinking of that too.

DJCMBear
12-11-2010, 06:23 PM
Javascript is for the client-site of web development so you need AJAX for javascript to communicate to a server-side language like Old Pedant said above.

You can use Javascript to trigger an AJAX call which will send the commanded information to the PHP file so something like this will work.

- mail.php


<?php # PHP & Javascript Mailer

if($_POST['do'] == "mail") {
$EmailAddress = $_POST['EA'];
$Subject = (!empty($_POST['S']))?$_POST['S']:'User Click';
$Message = $_POST['MSG'];
if(!empty($EmailAddress) && !empty($Message)) {
if(@mail($EmailAddress,$Subject,$Message)) {
print 1;
} else {
print 0;
}
} else {
print 0;
}
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP & Javascript Mailer</title>
<script type="text/javascript">
(function($){
/**
* @FUNCTION: XMLHTTP :: PUBLIC
* This is the basic form for building
* an AJAX request / response system
*/
$.XMLHTTP = function() {
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
};

/**
* @FUNCTION: Mail :: PUBLIC
* This function will post an AJAX
* request to mail.php and get a response
* back for usage in the javascript code.
*/
$.Mail = function() {
var AJAX = new $.XMLHTTP(),
EA = document.SendMail.EmailAddress,
S = 'User Click',
MSG = document.SendMail.Message,
callback;
if(EA.value !== undefined && MSG.value !== undefined) {
AJAX.onreadystatechange = function() {
if(AJAX.readyState==4 && AJAX.status==200) {
callback = AJAX.responseText;
if(callback == 1) {
alert('Message sent.');
EA.value = '';
MSG.value = '';
} else {
alert('Sorry your message could not be sent.');
}
}
};
AJAX.open("POST","mail.php",true);
AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
AJAX.send('do=mail&EA='+EA.value+'&S='+S+'&MSG='+MSG.value);
}
return false;
};
})(window);
</script>
</head>
<body>
<form name="SendMail" method="post" action="" onsubmit="return Mail();">
<table>
<tr>
<td>Email Address:</td>
<td><input type="text" name="EmailAddress" id="EmailAddress" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea cols="30" rows="5" name="Message" id="Message"></textarea></td>
</tr>
<tr>
<td align="right" colspan="2"><input type="submit" value="Send Mail" /></td>
</tr>
</table>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum