-2
I have already created a folder inside my server phpmailer with the classes Phpmailer.php and SMTP.php. Now I have this code to insert into the database table:
$data = isset($_POST["DataRegisto"]) ? $_POST["DataRegisto"] : '';
$contacto = isset($_POST["Contacto"]) ? $_POST["Contacto"] : '';
$telefone = isset($_POST["Telefone"]) ? $_POST["Telefone"] : '';
$crianca = isset($_POST["NomeCrianca"]) ? $_POST["NomeCrianca"] : '';
$nascimento = isset($_POST["DataNasc"]) ? $_POST["DataNasc"] : '';
$visita = isset($_POST["Visita"]) ? $_POST["Visita"] : '';
$datavisita = isset($_POST["DataVisita"]) ? $_POST["DataVisita"] : '';
$observacao1 = isset($_POST["Observacao1"]) ? $_POST["Observacao1"] : '';
$sql = "INSERT INTO InscricoesInf (`DataRegisto`,`Nome`,`Contacto`,`Telefone`,`NomeCrianca`,`DataNasc`,`Visita`,`DataVisita`,`Observacao1`) VALUES('$data','xxxxxx','$contacto','$telefone','$crianca','$nascimento','$visita','$datavisita','$observacao1')";
$qr = mysqli_query($conn, $sql);
$conn->close();
I intended that whenever I make a new entry in the table of the database send an email to the responsible for this subject.
With the solutions presented I developed my code, is entering in the database, but is not sending the email:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Mail
{
public function sendMail($crianca, $contacto, $nascimento, $message)
{
require './PHPMailer';
require './SMTP';
require './Exception';
$mail = new PHPMailer();
try {
// Server settings
$mail->isSMTP(); // Define o mail para usar o SMTP
$mail->Host = 'smtp.hotmail.com'; // Define o host do e-mail
$mail->SMTPAuth = true; // Permite autenticação SMTP
$mail->Username = '[email protected]'; // Conta de e-mail que enviará o e-mail
$mail->Password = 'xxxxxxx'; // Senha da conta de e-mail
$mail->SMTPSecure = 'tls'; // Permite encriptação TLS
$mail->Port = 587; // Porta TCP que irá se conectar
$mail->SMTPOptions = array( // Configuração adicional, não obrigatória (caso de erro de ssl)
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
// Recipients
$mail->setFrom('[email protected]', 'Título do e-mail, ou assunto'); // Define o remetente
$mail->addAddress('[email protected]', 'Contato Site'); // Define o destinário
// Content
$mail->isHTML(true); // Define o formato do e-mail para HTML
$mail->Subject = 'Contato feito pelo site';
$mail->Body = "
<html>
<head>
</head>
<body>
<h2>Pedido de Informação</h2>
<table>
<tr>
<th>Nome</th>
<th>Telefone</th>
<th>Data Nascimento</th>
</tr>
<tr>
<td>$crianca</td>
<td>$contacto</td>
<td>$nascimento</td>
</tr>
</table>
<h2>Novo registo nos pedidos de informação./h2>
<p>$message</p>
</body>
</html>";
$mail->send(); // Envia o e-mail
return true;
} catch (Exception $e) { // Se capturar exceção retorna false
return false;
}
}
}
$data = isset($_POST["DataRegisto"]) ? $_POST["DataRegisto"] : '';
$contacto = isset($_POST["Contacto"]) ? $_POST["Contacto"] : '';
$telefone = isset($_POST["Telefone"]) ? $_POST["Telefone"] : '';
$crianca = isset($_POST["NomeCrianca"]) ? $_POST["NomeCrianca"] : '';
$nascimento = isset($_POST["DataNasc"]) ? $_POST["DataNasc"] : '';
$visita = isset($_POST["Visita"]) ? $_POST["Visita"] : '';
$datavisita = isset($_POST["DataVisita"]) ? $_POST["DataVisita"] : '';
$observacao1 = isset($_POST["Observacao1"]) ? $_POST["Observacao1"] : '';
$sql = "INSERT INTO InscricoesInf (`DataRegisto`,`Nome`,`Contacto`,`Telefone`,`NomeCrianca`,`DataNasc`,`Visita`,`DataVisita`,`Observacao1`)
VALUES ('$data','xxxxxx','$contacto','$telefone','$crianca','$nascimento','$visita','$datavisita','$observacao1')";
if ($conn->query($sql)) {
$Mail = new Mail();
$Mail->sendEmail('Seu nome', 'Telefone', 'Data Nascimento', 'Mensagem do e-mail');
$conn->close();
} else {
echo 'Erro';
}
you can check the edition I made in the question with the code developed following your example?
– Bruno
Place this line of code and see if it shows any error message:
$mail->SMTPDebug = 2;
. Another thing, remember to follow the structure of the folders as I put in my example, it is essential for it to work.– João Pedro Schmitz
but my code is correct? No error detected? The folder structure is correct
– Bruno
@Beginner, I updated my reply in the part of the Mail class where the sending of e-mail is performed. Now check and see if it shows error message.
– João Pedro Schmitz
we can chat just to ask some questions that I think the problem will be there?
– Bruno
Yes, it is possible
– João Pedro Schmitz
Let’s go continue this discussion in chat.
– Bruno
already managed to solve the problem and already sends emails
– Bruno
Glad it worked out! If my answer helped please consider a positive vote and mark as the correct one!
– João Pedro Schmitz