0
good afternoon! Well, I opened a small business, and I’m doing my site myself (rs), I haven’t done anything in PHP and, even worse, in Js. Basically I took a code ready somewhere on the internet for this form, and I even managed to make it work with PHP mail, but locally. Now that I have moved to hosting, it is possible only using this Phpmailer, as the support guided me, after all my package is free and does not support this php function. Basically, this code uses parts of Javascript and php to post an HTML form and returns in the form itself the successfully or error sending message.
HTML Code (Form):
<div class="contact-form">
<div id="success"></div>
<form name="sentMessage" id="contactForm" novalidate="novalidate">
<div class="control-group">
<input type="text" class="form-control" id="name" placeholder="Seu nome" required="required" data-validation-required-message="* Nome não inserido" />
<p class="help-block text-danger"></p>
</div>
<div class="control-group">
<input type="email" class="form-control" id="email" placeholder="Seu E-mail" required="required" data-validation-required-message="* E-mail não inserido" />
<p class="help-block text-danger"></p>
</div>
<div class="control-group">
<input type="text" class="form-control" id="subject" placeholder="Qual dúvida/motivo?" required="required" data-validation-required-message="* Motivo/dúvida não inserido(a)" />
<p class="help-block text-danger"></p>
</div>
<div class="control-group">
<textarea class="form-control" id="message" placeholder="Detalhes da Mensagem" required="required" data-validation-required-message="* Você deve inserir sua mensagem"></textarea>
<p class="help-block text-danger"></p>
</div>
<div>
<button class="btn" type="submit" id="sendMessageButton">Enviar</button>
</div>
</form>
</div>
Javascript code (contact.js)
$(function () {
$("#contactForm input, #contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function ($form, event, errors) {
},
submitSuccess: function ($form, event) {
event.preventDefault();
var name = $("input#name").val();
var email = $("input#email").val();
var subject = $("input#subject").val();
var message = $("textarea#message").val();
$this = $("#sendMessageButton");
$this.prop("disabled", true);
$.ajax({
url: "contact.php",
type: "POST",
data: {
name: name,
email: email,
subject: subject,
message: message
},
cache: false,
success: function () {
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Sua mensagem foi enviada com sucesso! </strong>");
$('#success > .alert-success')
.append('</div>');
$('#contactForm').trigger("reset");
},
error: function () {
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append($("<strong>").text("Desculpe-nos " + name + ", aparentemente nosso servidor de e-mail esta offline. Por favor, tente novamente mais tarde."));
$('#success > .alert-danger').append('</div>');
$('#contactForm').trigger("reset");
},
complete: function () {
setTimeout(function () {
$this.prop("disabled", false);
}, 1000);
}
});
},
filter: function () {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function (e) {
e.preventDefault();
$(this).tab("show");
});
});
$('#name').focus(function () {
$('#success').html('');
});
PHP CODE (contact.php) - Call Phpmailer:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER['DOCUMENT_ROOT'] . 'Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . 'PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . 'SMTP.php';
if(empty($_POST['name']) || empty($_POST['subject']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
http_response_code(500);
exit();
}
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = false; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 465; // TLS only
$mail->SMTPSecure = 'ssl'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // email
$mail->Password = 'mariaclara121314'; // password
$mail->setFrom($_POST['email'], strip_tags(htmlspecialchars($_POST['name']))); // From email and name
$mail->addAddress('[email protected]', 'Site Construtora Fernandes'); // to email and name
$mail->Subject = $_POST['subject'];
$mail->msgHTML("Você recebeu uma nova mensagem enviada pelo formulário do site.\n\n"."Aqui está os detalhes:\n\nNome: $name\n\n\nEmail: $email\n\nAssunto: $m_subject\n\nMensagem: $message";
$header = "De: $email";
$header .= "Responder para: $email";);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if(!$mail->send()){http_response_code(500);
}
?>
I have no idea how to search and/or where the logs of these Phpmailer errors are saved? Remembering that I have limited access to hosting (I can’t touch php.ini and the like) and this form will send very few emails. It returns error successfully, but I can’t understand where it pulls this JS to work, if I really need it since I’m not using native PHP mail but Phpmailer, and how can I get the log to try to figure out the problem.
I thank you in advance friends for your help! Live site: constructafernandessc.com.br