Q
Q
Qsnakes2021-07-19 21:50:05
PHP
Qsnakes, 2021-07-19 21:50:05

Is there anyone who can check php code for errors?

The bottom line is that it gives an error 500 on js with a reference to php here The error on js crashes:

async function formSend(e) {
        e.preventDefault();

        const error = formValidate(form);
        let formData = new FormData(form);
        formData.append('image', formImage.files[0]);
        if (error === 0) {
            form.classList.add('_sending');
            let response = await fetch('sendmail.php', {
                method: 'POST',
                body: formData
            });
            if (response.ok){
                let result = await response.json();
                alert(result.message);
                formPreview.innerHTML = '';
                form.reset();
                form.classList.remove('_sending');
            }else {
                alert('Что-то пошло не так');
                form.classList.remove('_sending');
            }

        } else{
            alert('Заполните обязательные поля');
        }
    }


Here is the PHP code itself, in which there is most likely an error, I need the help of those who know, I will be most grateful
<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'phpmailer/src/Exception.php';
    require 'phpmailer/src/PHPMailer.php';

    $mail = new PHPMailer(true);
    $mail->CharSet = 'UTF-8';
    $mail->setLanguage('ru', 'phpmailer/language/');
    $mail->IsHTML(true);

    //От кого письмо
    $mail->setForm('[email protected]', 'Никита Андреевич');
    // Кому отправить
    $mail->addAddress('[email protected]');
    //Тема письма
    $mail->Subject = 'Неужели ты смог, спасибо папаша';

    //Рука
    $hand = 'Правая';
    if($_POST['hand'] == 'left') {
        $hand = 'Левая';
    }

    //Тело письма
    $body = '<h1>Встречайте мега письмо</h1>';

    if(trim(!empty($_POST['name']))){
        $body.='<p><strong>Имя:</strong> '.$_POST['name'].'</p>';
    }

    if(trim(!empty($_POST['email']))){
        $body.='<p><strong>E-mail:</strong> '.$_POST['email'].'</p>';
    }

    if(trim(!empty($_POST['hand']))){
        $body.='<p><strong>Рука:</strong> '.$hand.'</p>';
    }

    if(trim(!empty($_POST['age']))){
        $body.='<p><strong>Возраст:</strong> '.$_POST['age'].'</p>';
    }

    if(trim(!empty($_POST['message']))){
        $body.='<p><strong>Сообщение:</strong> '.$_POST['message'].'</p>';
    }

    //Прикручиваем файлы
    if (!empty($_FILES['image']['tmp_name'])) {
        //Путь загрузки файлов
        $filePath = __DIR__ . '/files/' . $_FILES['image']['name'];
        //Грузим файл
        if (copy($_FILES['image']['tmp_name'], $filePath)) {
            $fileAttach = $filePath;
            $body.='<p><strong>Фото в приложении:</strong></p>';
            $mail->addAttachment($fileAttach);
        }
    }

    $mail->Body = $body;

    //Отправляем 
    if (!$mail->send()) {
        $message = 'Ошибка отправки';
    } else {
        $message = 'Сообщение отправлено!';
    }

    $response = ['message' => $message];

    header('Content-type: application/json');
    echo json_encode($response);
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2021-07-19
@Qsnakes

D - debug. read logs/enable error output.
and it's not about unwillingness to help, it's about being unable to take into account your environment and compile the code in your mind. except to guess.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question