function sonuc() {
message="Sonuçlar:n"
İstediğim, sonuçlandıktan sonra istediğim mail adresine sonucu göndermek.
Ziyaretçi adı
Soyadı:
Sonuç :

4
●45

<script>
$('#finish').click(function() {
var result = sonucuDondurenFonksiyon();
$.ajax({
type: "POST",
url: "sendmail.php",
data: {
sonuc: result
},
success: function(response) {
if (response == 'success') {
$('#sonucdiv').html('<p>Sonuç:'+result+'</p>');
}
}
})
});
</script>sendmail.php (PHPMailer Gerektirir)<?php
if(isset($_POST['sonuc'])){ //"sonuc" postumuz gelmiyorsa sayfayı çalıştırmaya gerek yok
/* Mailer Hazırlıkları */
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
require 'vendor/autoload.php';
/* Mailer Hazırlıkları */
$mail = new PHPMailer(true);
$sonuc = $_POST['sonuc'];
try {
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@gmail.com'; //SMTP username
$mail->Password = 'pass'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS ; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->setFrom('user@gmail.com', 'Mailer'); //Kimden
$mail->addAddress('hedefmail@example.net', 'User'); //Kime
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Konu;
$mail->Body = 'Sınav sonucu:' . $sonuc;
$mail->send();
echo 'success';
} catch (Exception $e) {
echo 'error';
}
}
exit;
?>JS'de sonuç fonksiyonunuz gerekli işlemleri yaptıktan sonra sadece sonucu return etmelidir, ajax kodumuz success döndürdüğü zaman sonucu p etiketimiz ile beraber yazdırıyoruz. İsterseniz click edildiğinde de sonucu yazdırabilirsiniz, mail işlemi arkaplanda gerçekleştirilir işlemin durumu ne olursa olsun kullanıcıya sonuç gösterilir.