Answer the question
In order to leave comments, you need to log in
PHPMailer intercepting and displaying transmitted data?
Hello.
I recently started learning PHP, so I just can't figure out what's going on with this code.
I made a script for sending a letter according to one lesson - it works well, but in order to work with it further - I want to see what data it sends in POST. But there is some magic going on here.
<html>
<head>
<title>Отправка писем с помощью PHPmailer</title>
<meta charset="utf-8">
</head>
<body>
<?php
if( $_POST ){
require_once "lib/class.phpmailer.php";
$mail = new PHPmailer;
$mail->IsSMTP();
$mail->Host = "127.0.0.1";
$mail->Port = 25;
$mail->CharSet = 'UTF-8';
$mail->Username = "xxxxx";// логин под которым заходим в почту
$mail->Password ="xxxx";// пароль от почты
$mail->SetFrom('[email protected]', 'Локальный компьютер'); // От кого отправлено письмо
//Тело письма
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = "Имя: {$_POST['name']}<br> Email: {$_POST['email']}<br> Сообщение: " . nl2br($_POST['body']);
$mail->AltBody = "Имя: {$_POST['name']}\r\n Email: {$_POST['email']}\r\n Сообщение: {$_POST['body']}";
$address = "[email protected]"; // кому отправляем
$mail->AddAddress($address, 'xxxxxxxx');// можно дублировать
$mail->AddAttachment("files/mail.pdf");
if( $mail->send() ){
echo $answer = '1';
}else {
$answer = '0';
}
die( $answer );
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
echo "<pre>";
foreach ($_POST as $key=>$value){
print_r("<p>".$key." = " . $value . "</p>");
};
echo "</pre>";
?>
<form action="" method="POST" id="contact"><!-- action если обработчик будет на другой странице-->
<p><label for="name">Имя</label><input id="name" name="name" type="text"><span></span></p>
<p><label for="">Тема</label><input id="subject" name="subject" type="text"><span></span></p>
<p><label for="">Email</label><input id="email" name="email" type="text"><span></span></p>
<p><label for="">Сообщение</label><textarea id="body" name="body" id="" cols="30" rows="10"></textarea><span></span></p>
<p><input type="submit" id="submit" name="submit" value="Выслать"><span></span></p>
</form>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('#contact').submit(function(){
var errors = false;
$(this).find('span').empty();
$(this).find('input, textarea').each(function(){
if($.trim( $(this).val() ) == ''){
errors = true;
$(this).next().text('Не заполнено поле' + $(this).prev().text());
}
});
if(!errors) {
var data = $('#contact').serialize();// склеивает данные форму в qerystring
// console.log(data);
$.ajax({
url: 'index.php',
type: 'POST',
data: data,
beforeSend: function(){
$('#submit').next().text('Отправляю...');
},
success: function(res){
if(res){ // подумать почему res != 1;
console.log(res);
$('#contact').find('input:not(#submit), textarea').val('');
$('#submit').next().empty();
}else {
$('#submit').next().empty();
alert('Ошибка отправки');
}
},
error: function() {
alert('Ошибка');
}
});
}
return false;
})
});
</script>
</body>
</html>
<html>
<head>
<title>Отправка писем с помощью PHPmailer</title>
<meta charset="utf-8">
</head>
<body>
11
</body>
echo "<pre>";
print_r($_POST);
echo "</pre>";
Answer the question
In order to leave comments, you need to log in
Thanks for the quick response.
So I'm doing this check.
if( $_POST['submit'] ){
echo "<pre>";
print_r($_POST);
echo "</pre>";
} else echo "no POST data";
Notice: Undefined index: submit in C:\xampp\htdocs\MAILSEND\index.php on line 58
no POST data
if( $_POST ){...
$_POST['submit']
it turns out not.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question