2011-12-11 98 views
0

我做了一個簡單的網站,您輸入訪問代碼並顯示seceret消息。有用。的Wooo。簡單網站的jQuery AJAX調用

但是,我想補充一點,所以當seceret被發現時,電子郵件會在後臺發送給用戶。

我知道我使用ajax來調用PHP腳本來發送電子郵件,但我有點不確定要使用的代碼。

我知道用戶的電子郵件地址,因爲該網站是針對該用戶的。

感謝

+0

你能說明你的代碼看起來如何? – Cyclonecode

+0

你讀過[$ .post()](http://api.jquery.com/jQuery.post/)了嗎?如果沒有,開始。 – Pointy

回答

0

那麼,在您通過AJAX調用PHP腳本,你可以只是做沿着這些路線的東西:

$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers); 

如果你有這個網站:

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

你可以用jQuery連接以下內容:

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

我知道PHP的一部分,但ajax/jQuery的一部分? – nmyster

+0

@nmyster我已經更新了我的答案。 –

1
 
$.ajax({ 
    type: "POST", 
    url: "your_php_url", 
    data: "email="+email, 
    success:function(response) { 
    alert("Mail sent"); 
    } 
}); 
0

幸得從到Blogger:http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/ --and直接引用(轉貼鏈接,信用和額外信息)

定義常量的地方:

define('GUSER', '[email protected]'); // 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('[email protected]', '', '[email protected]', 'yourName', 'test mail message', 'Hello World!'); 
?> 
0

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

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