A
A
anxieter2017-08-07 16:02:56
PHP
anxieter, 2017-08-07 16:02:56

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>


The first thing I ran into was that console.log(res); outputs not 1 or 0, but this is what is in the console:
<html>
<head>
  <title>Отправка писем с помощью PHPmailer</title>
  <meta charset="utf-8">
</head>
<body>
11

Explain why it clings to the HTML code???
Further I don't understand what happens in this section
if( $mail->send() ){
echo $answer = '1';
}else {
$answer = '0';
}
echo $answer = '1'; As I understand it, this is 1 after </body>
But I don’t see it in the document, but I see it only in the console.
And in general, everything inside this condition is not displayed. As if $mail->send() does not react to the condition.
Next I want to intercept;
echo "<pre>";
    print_r($_POST);
  echo "</pre>";

- this is how an empty array Array() is displayed;
it is clear that the subsequent foreach did not display anything.
How can I display POST data?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2017-08-07
@djQuery

Dark options. If you want, we can dig through TeamViewer....

A
anxieter, 2017-08-07
@anxieter

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";

Displayed
Notice: Undefined index: submit in C:\xampp\htdocs\MAILSEND\index.php on line 58
no POST data

It's strange, but at the very beginning of the script there is a check if( $_POST ){...
AND the whole script is executed, that is, the POST data is going somewhere. But $_POST['submit']it turns out not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question