0

In phpmailer, I want to use a script tag inside the variable body to do something in the click event. For example, this is the body which I send in the email. I put it in $mail->body='<html>..<script>....'

<html>
 <head>
        <title></title>
            <script>
                document.getElementById("btn").onclick=function(){clt()};
                function clt(){
                    console.log("okk");
                }
                console.log("okk");
            </script>
        </head>
        <script>
                document.getElementById("btn").onclick=function() {clt()};
                function clt(){
                    console.log("okk");
                }
                console.log("okk");
            </script>
        <body>
            <p id="txt">tetet</p><button  id="btn" onclick="clt()">click me! </button>
            <script>
                document.getElementById("btn").onclick=function() {clt()};
                function clt(){
                    console.log("okk");
                }
                console.log("okk");
            </script>
        </body>
        </html>
GG.
  • 21,083
  • 14
  • 84
  • 130
  • so what's the question? I suspect your mixing server-side and client side in a way that wont work - but its to unclear to tell –  Jan 24 '17 at 01:53
  • 1
    If the question here is "can I use javascript in an email body", the answer is "[no, that would be a security problem if it worked](http://stackoverflow.com/questions/1088016/html-email-with-javascript)". – tklg Jan 24 '17 at 01:56
  • 6
    Possible duplicate of [Is JavaScript supported in an email message?](http://stackoverflow.com/questions/3054315/is-javascript-supported-in-an-email-message) – HPierce Jan 24 '17 at 01:56
  • you need to set `$mail->IsHTML(true);` – Ehsan Ilahi Jan 24 '17 at 05:53

1 Answers1

1

This isn't anything to do with PHPMailer; email clients do not run javascript, so you simply can't do this.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • there is no way that when the client click on a link 2 pages open instead of one ? – Youssef Mans Jan 24 '17 at 13:53
  • Nope. You could achieve that indirectly by going to one page that opens a second window, but it's very likely to be blocked by pop-up blockers. – Synchro Jan 24 '17 at 13:59
  • i see , so when the pop-up is blocked i can't get the time and the user ip adresse with :$_SESSION['IP_address']=$_SERVER['REMOTE_ADDR'] ? – Youssef Mans Jan 24 '17 at 16:37
  • Pop-ups don't have anything to do with them?! All the info is in the original $_POST and $_SERVER superglobals in the initial request; look in there. – Synchro Jan 24 '17 at 22:39
  • no for example my links is google.com and i want to know how many people clicked on the link, so at the moment when the user click the first page that will show up is google and the second is my page page which will contains $_SESSION['IP_address']=$_SERVER['REMOTE_ADDR'] then i'll stock the info in text file – Youssef Mans Jan 24 '17 at 22:55
  • Use a redirect - go to your page first, record the click, then forward to google. Google isn't going to forward to your page. – Synchro Jan 24 '17 at 23:49