-
Hello everyone, first of all, thanks for reading. I'm new to this so I have some doubts about how to create a contact form. I'm doing a project for school and got the form to work correctly, but would like to add a redirect to index.html once the message is sent successfully. I have no idea how to do that, I've been researching on forums, etc but nothing seems to work. Also, is there a way to include the IP, browser, operating system, etc the person is using when sending the message? It would be great for statistical reasons. Thank you very much in advance!! Oh btw, the code I'm using is this: <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$myPersonalEmail = "blah blah blah";
$externalMailHost = "blah blah blah";
$externalMailAddress = "blah blah blah";
$externalMailSMTPAuth = true;
$externalMailUsername = "blah blah blah";
$externalMailPassword = "blah blah blah";
$externalMailSMTPSecure = "tls";
$externalMailPort = 587;
require $_SERVER['DOCUMENT_ROOT'] . '/blah blah blah/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/blah blah blah/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/blah blah blah/SMTP.php';
if(isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $externalMailHost;
$mail->SMTPAuth = $externalMailSMTPAuth;
$mail->Username = $externalMailUsername;
$mail->Password = $externalMailPassword;
$mail->SMTPSecure = $externalMailSMTPSecure;
$mail->Port = $externalMailPort;
$mail->setFrom($externalMailAddress, 'Contacto');
$mail->addAddress($myPersonalEmail);
$mail->addReplyTo($_POST['email'], $_POST['nombre']);
$mail->isHTML(false);
$mail->Body = <<<EOT
Asunto: {$_POST['asunto']}
Nombre: {$_POST['nombre']}
Email: {$_POST['email']}
Mensaje: {$_POST['mensaje']}
EOT;
try {
$mail->send();
echo 'Tu mensaje se envió correctamente; recibirás una respuesta a la brevedad!';
} catch (Exception $e) {
echo "Tu mensaje no pudo ser enviado! PHPMailer Error: {$mail->ErrorInfo}";
}
} else {
echo "Hay un problema con el documento contacto.html!";
}
?> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Your code looks fine as far as email sending goes, though I'd recommend you learn to use composer. To redirect after doing something, you need to send the browser a try {
$mail->send();
header('Location: /email_success.php');
exit;
} catch (Exception $e) {
header('Location: /email_failed.php');
exit;
}
} else {
echo "Hay un problema con el documento contacto.html!";
} where each destination is a script (or just an HTML file) that displays an appropriate post-send (or post-failure) message. |
Beta Was this translation helpful? Give feedback.
Your code looks fine as far as email sending goes, though I'd recommend you learn to use composer.
To redirect after doing something, you need to send the browser a
Location
header telling it where to go. Importantly, you have to do this before you send any output. So replace yourecho
statementswith lines using the [
header` function](https://www.php.net/manual/en/function.header.php), and then immediately exit your script, like this:where each desti…