Если заполнены все поля, заявка приходит. Если одно из полей не заполнено, заявка не приходит.
<?php
error_reporting(E_ALL ^ E_NOTICE);
require 'PHPMailer/PHPMailerAutoload.php';
$response = array();
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->setFrom('noreply@naves');
$mail->addAddress('mymail@yandex.ru');
$form = isset( $_POST['form-name'] ) ? $_POST['form-name'] : null;
$subject = '';
$tokens = array();
if ( $form === 'form-consult' ) {
$fields = array(
'name' => array( 'key' => 'msg', 'token' => '[[NAME]]', 'validators' => ['required' => true] ),
'phone' => array( 'key' => 'msg', 'token' => '[[PHONE]]', 'validators' => ['required' => true] )
);
$subject = 'Поступила новая заявка на консультацию';
$html = file_get_contents('templates/form-consult.html');
}
if (isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['file']['tmp_name'],
$_FILES['file']['name']);
}
check_fields( $fields );
$html = str_replace( array_keys($tokens), array_values($tokens), $html );
$mail->Subject = '=?utf-8?B?' . base64_encode($subject) . '?=';
$mail->msgHTML($html);
$send = $mail->send();
if ( $send ) {
$response['error'] = false;
} else {
$response['error'] = true;
}
die( json_encode( $response ) );
// вспомогательные функции
function check_fields( $fields ) {
global $tokens;
foreach ( $fields as $key => $field ) {
$field['value'] = isset( $_POST[$key] ) ? strip_tags( trim( $_POST[$key] ) ) : null;
if ( $field['validators']['required'] ) {
if ( empty( $field['value'] ) ) {
$response['error'] = true;
die( json_encode( $response ) );
}
}
$tokens[ $field['token'] ] = $field['value'];
}
}
?>