可以将jQuery添加到php邮件功能

I am using the mail() in php and wondered if it was possible to have event listeners included. Below includes code with a button that is to have the paragraph slideup when clicked. Yet, nothing happens.

<html>
    <head>
        <script type="text/javascript"src="js/jquery.js"></script>
        <script type="text/javascript" src="js/jquery-ui.js"></script>
        <script type='text/javascript'>
            $(function() {
                $(':button').click(function(){
                    $("p").slideToggle();   
                });
            });
        </script>
    </head>
    <body>
        <?php
            $to = "Example@yahoo.com"; 
            $subject = "Maybe so??"; 
            $body = "<p>Yeppir </p>
            <button>Click Me </button>"; 
            $headers  = 'MIME-Version: 1.0' . "
";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
"; 

            $headers .= 'From: skywalker@msn.com';
            if (mail($to, $subject, $body, $headers)) { 
                echo("<p>Message sent!</p>"); 
            } else { 
                echo("<p>Message delivery failed...</p>"); 
            } 
        ?> 
    </body>
</html>

You are trying to send an email message and your javascript is on your website. When they open the message, they will see the message, but don't get any of that javascript because you didn't include it in the message in the first place.

What you can do is fix your code to send jQuery in the message along with the body. In theory that would work. In actuality this wouldn't work either as about 99% of email services (100% of all the most commonly used ones) strip javascript tags as they can be malicious. So, rather than spend time on getting toggles in emails, spend time trying to get the user to your site.

jQuery is client-side language and PHP is a server side, the possible way to achieve this is by requesting the PHP page which has the mail function with jQuery (using Ajax) and then change the html to show/hide the results (error or success) upon the PHP response.

read more about this topic in this post jQuery/PHP mail send the easy way?

The short answer is you can't. Mail clients strip out JS

Just stick with plain HTML and CSS that works over most of mail clients.