Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts

    send an email when user clicks a link

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Lightbulb

    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:

    Code:
    <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:

    Code:
    <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>
    Last edited by ~Kira; 12-11-2010 at 05:01 PM. Reason: um how do I mark this thread "resolved" ?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    <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
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    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.

    Code:
    mail  ( string $to  , string $subject
    besides, isn't this what I have? lol

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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.
    Code:
    <form method="post">
    ........
    <imput type="image" src="/images/image.jpg" alt="PDF" width="97" height="43" >
    </form>
    Hope you are you familiar with this(?)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    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.
    Code:
    <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.

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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
    Code:
    <?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>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •