【问题标题】:jQuery AJAX call for simple site [closed]jQuery AJAX 调用简单网站 [关闭]
【发布时间】:2011-12-11 14:33:56
【问题描述】:

我创建了一个简单的网站,您可以输入访问代码并显示一条秘密消息。有用。呜呜呜。

不过,我也想补充一点,这样当秘密被泄露时,会在后台向用户发送一封电子邮件。

我知道我使用 ajax 来调用将发送电子邮件的 PHP 脚本,但我有点不确定要使用的代码。

我已经知道用户的电子邮件地址,因为该网站是为那个人准备的。

谢谢

【问题讨论】:

  • 你能展示一下你的代码到目前为止的样子吗?
  • 你读过$.post()了吗?如果没有,就从那里开始。

标签: php jquery ajax email


【解决方案1】:
$.ajax({ 类型:“发布”, 网址:“your_php_url”, 数据:“电子邮件=”+电子邮件, 成功:功能(响应){ alert("邮件发送"); } });

【讨论】:

    【解决方案2】:

    好吧,在您通过 ajax 调用的 php 脚本中,您可以按照以下方式进行操作:

    $to = "someone@example.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "someonelse@example.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    

    如果你有这个 html:

    <a href="#" id="callphp">trigger ajax request</a>
    

    您可以使用 jQuery 连接以下内容:

    $('#callphp').bind('click', function(){
         $.ajax({
            url: "yourscript.php",
            success: function(){
               alert("done");
            }
         });
    });
    

    【讨论】:

    • 我知道 PHP 部分,但是 ajax/jquery 部分?
    • @nmyster 我已经更新了我的答案。
    【解决方案3】:

    致博客作者来自:http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/ -- 并直接引用(发布的致谢和额外信息链接)

    在某处定义常量:

    define('GUSER', 'you@gmail.com'); // GMail username
    define('GPWD', 'password'); // GMail password
    

    存储这个函数:

    function smtpmailer($to, $from, $from_name, $subject, $body) { 
        global $error;
        $mail = new PHPMailer();  // create a new object
        $mail->IsSMTP(); // enable SMTP
        $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
        $mail->SMTPAuth = true;  // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 465; 
        $mail->Username = GUSER;  
        $mail->Password = GPWD;           
        $mail->SetFrom($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if(!$mail->Send()) {
            $error = 'Mail error: '.$mail->ErrorInfo; 
            return false;
        } else {
            $error = 'Message sent!';
            return true;
        }
    }
    

    那么通过 ajax 调用的 PHP 文件将包含:

    <?php
    smtpmailer('to@mail.com', '', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!');
    ?>
    

    【讨论】:

      【解决方案4】:

      使用 jQuery.ajax 函数。 http://api.jquery.com/jQuery.ajax/

      jQuery.ajax({
        url: "url",
        type: "POST",
        data: "email="+email,
        success:function(response) {
             console.log("success");
        }
      }); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        相关资源
        最近更新 更多