0
<?php

session_cache_limiter('nocache'); header('Expires: ' . gmdate('r', 0));

header('Content-type: application/json');

$to = '[email protected]';

$enablePHPMailer = true;

$subject = $_POST['subject'];

if(isset($_POST['email'])) {

$name = $_POST['name'];
$email = $_POST['email'];

$fields = array(
    0 => array(
        'text' => 'Name',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email address',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'Message',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

    include('php-mailer/PHPMailerAutoload.php');

    $mail = new PHPMailer;

    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->SMTPDebug = 0;                                 // Debug Mode

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    $mail->Host = 'imap.yandex.ru';               // Specify main and backup server
    $mail->SMTPAuth = true;                             // Enable SMTP authentication
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;                         // Enable encryption, 'ssl' also accepted
    $mail->Username = '[email protected]';             // SMTP username
    $mail->Password = 'pass';                         // SMTP password

    $mail->From = $email;
    $mail->FromName = $_POST['name'];
    $mail->AddAddress($to);                               // Add a recipient
    $mail->AddReplyTo($email, $name);

    $mail->IsHTML(true);                                  // Set email format to HTML

    $mail->CharSet = 'UTF-8';

    $mail->Subject = $subject;
    $mail->Body    = $message;

    if($mail->Send()) {
       $arrResult = array ('response'=>'success');
    } else {
        $arrResult = array ('response'=>'error');
    }

echo json_encode($arrResult);

} else {

$arrResult = array ('response'=>'error');
echo json_encode($arrResult);

} ?>

Не отправляется сообщение с обратной формы, возвращает ошибку, вот в этом фрагменте:

if($mail->Send()) {
       $arrResult = array ('response'=>'success');
    } else {
        $arrResult = array ('response'=>'error');
    }

В чем может быть проблема??

1 ответ 1

1

Переписать так:

try {
    if($mail->Send()) {
       $arrResult = array ('response'=>'success');
    } else {
        $arrResult = array ('response'=>'error');
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

И посмотреть, какую ошибку выдает.

2
  • он мне с else возвращает response error 4 сен 2017 в 6:06
  • попробуй включить SMTPDebug $mail->SMTPDebug = 2;
    – ArtOsi
    4 сен 2017 в 12:29

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.