v2.5.2
Giriş yap

Formlu Basit Bir PHPmailler İşlemi

ilkertuncer
427 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Basit bir formlu PHPmailler işlemi lazım.

ebykdrms
764 gün önce

PHPMailer Örneği

Basit bir örnek paylaşıyorum.

PHPMailer Kütüphanesi

github.com/PHPMailer/PHPMailer

index.html
<div id="form">
    <input name="name" type="text" placeholder="Ad Soyad" />
    <input name="email" type="email" placeholder="E-posta" />
    <textarea name="message"></textarea>
    <button data-type="send-mail">Gönder</button>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="sendmail.js"></script>
sendmail.js
var $form = $("#form");
var $name = $form.find("[name='name']");
var $email = $form.find("[name='email']");
var $message = $form.find("[name='message']");
var $submitButton = $form.find("[data-type='send-mail']");

$submitButton.on("click",function(){
    $submitButton.prop("disabled", true);
    $.ajax({
        url: "sendmail.php",
        data: { name:$name.val(), email:$email.val(), message:$message.val() },
        type: "post",
        dataType: "json",
        success: function(response) {
            alert("MAİL GÖNDERİMİ\n"+response.message);
            if(response.type==="success") {
                console.log("Mail gönderildi.");
            }
            else if(response.type==="error") {
                console.warn("Mail gönderilemedi.");
            }
            else {
                console.error("Mail gönderiminde beklenmedik bir response");
            }
        },
        error: function(err) {
            alert("HATA!");
            console.log(err);
        },
        complete: function() {
            $submitButton.prop("disabled", false);
        }
    });
});
sendmail.php
<?php
header('Content-Type: application/json');

date_default_timezone_set('Europe/Istanbul');
$fromName = "prototurk.com"; // Gönderen adı
$title = 'Web sitesinden mesaj var'; // Mail konusu

$mailFrom = '[email protected]';
$password = 'proto1234';
$mailTo = '[email protected]';
$host = 'smtp.yandex.com';
$port = 465;

$name = isset($_POST["name"]) ? htmlentities($_POST["name"]) : "<i style='color:#777'>Belirtilmedi</i>";
$email = isset($_POST["email"]) ? htmlentities($_POST["email"]) : "<i style='color:#777'>Belirtilmedi</i>";
$message = isset($_POST["message"]) ? htmlentities($_POST["message"]) : "<i style='color:#777'>Belirtilmedi</i>";
$date = date('d.m.Y H.i.s');
$ipAddress = $_SERVER['REMOTE_ADDR'];

$tableRowStyle = "display:table-row";
$tableCellStyle = "display:table-cell; padding:5px; border-bottom:1px solid #eee;";
$body = "<div style='display:table;'>";
$body .= "<div style='$tableRowStyle'><b style='$tableCellStyle'>Ad Soyad</b><span style='$tableCellStyle'>$name</span></div>";
$body .= "<div style='$tableRowStyle'><b style='$tableCellStyle'>E-posta</b><span style='$tableCellStyle'>$email</span></div>";
$body .= "<div style='$tableRowStyle'><b style='$tableCellStyle'>Mesaj</b><span style='$tableCellStyle'>$message</span></div>";
$body .= "<div style='$tableRowStyle'><b style='$tableCellStyle'>Gönderim Tarihi</b><span style='$tableCellStyle'>$date</span></div>";
$body .= "<div style='$tableRowStyle'><b style='$tableCellStyle'>IP Adresi</b><span style='$tableCellStyle'>$ipAddress</span></div>";
$body .= "</div>";

$altBody = "Ad Soyad: $name\n";
$altBody .= "E-posta: $email\n";
$altBody .= "Mesaj: $message\n";
$altBody .= "Gönderim Tarihi: $date\n";
$altBody .= "IP Adresi: $ipAddress\n";

include("PHPMailer.php");
include("SMTP.php");
include("Exception.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
  $mail->SMTPDebug = 0;
  $mail->isSMTP();
  $mail->CharSet = 'UTF-8';
  //$mail->setLanguage('tr',"language/");
  $mail->Host       = $host;
  $mail->SMTPAuth   = true;
  $mail->Username   = $mailFrom;
  $mail->Password   = $password;
  $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  $mail->Port       = $port;

  $mail->From = $mailFrom;
  $mail->FromName = $fromName;
  $mail->addAddress($mailTo);

  $mail->isHTML(true);
  $mail->Subject = $title;
  $mail->Body    = $body;
  $mail->AltBody = $altBody;

  $mail->send();
  echo json_encode([
    "type" => "success",
    "message" => "Mesajınız gönderildi. Teşekkür ederiz."
  ]);
} 
catch (Exception $e) {
  echo json_encode([
    "type" => "error",
    "message" => "Bir sorun oluştu ve mesajınız gönderilemedi."
  ]);
}
exit;