Skip to content

Instantly share code, notes, and snippets.

@svgerasimov
Last active April 12, 2021 05:37
Show Gist options
  • Save svgerasimov/820dcdcf4e9eba19fe8dad1cff26d979 to your computer and use it in GitHub Desktop.
Save svgerasimov/820dcdcf4e9eba19fe8dad1cff26d979 to your computer and use it in GitHub Desktop.
Отправка писем на почту из формы PHPMailer + AJAX
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if($_POST){
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/POP3.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'prudelnikov1992'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Владельцу ресурса MineCash.ru'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Новая заявка с сайта MineCash.ru';
$mail->Body = ' <html>
<head>
<title>Новая заявка с сайта</title>
</head>
<body>
<p>Имя: '.$_POST['name'].'</p>
<p>Телефон: '.$_POST['phone'].'</p>
<p>Сообщение: '.$_POST['message'].'</p>
</body>
</html>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>
<form id="userData" class="form" method="post" action="">
<h3 class="form__title">Получить консультацию</h3>
<div class="form__fields">
<input name="name" class="form__input" type="text" placeholder="Ваше имя" data-valid="required"><span></span>
<input name="phone" class="form__input form__input--split" type="tel" placeholder="Ваше номер" data-valid="required" data-phone ><span></span>
<input name="email" id="email" class="form__input form__input--split" type="email" placeholder="Ваш e-mail" data-valid="required" data-email><span></span>
<textarea name="message" class="form__input" placeholder="Ваше сообщение" rows="15"></textarea>
</div>
<input class="button button__form" type="submit" value="отправить">
</form>
<script>
$(document).ready(function(){
$('#userData').submit(function(){
let errors = false;
$(this).find('span').empty();
$(this).find('input, textarea').each(function(){
if( $.trim( $(this).val() ) == ''){
errors = true;
$(this).next().text('Не заполнено поле ');
}
});
if(!errors){
let data = $('#userData').serialize();
$.ajax({
url: 'index.php',
type: 'POST',
data: data,
success: function(res){
if (data['error']) {
alert(data['error']);
} else {
console.log('Письмo oтврaвлeнo!');
}
},
error: function(){
alert('Ошибка');
}
});
}
return false;
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment