-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacto.php
157 lines (126 loc) · 5.53 KB
/
contacto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
use PHPMailer\PHPMailer\{PHPMailer, SMTP, Exception};
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
if(isset($_POST['submit'])){
$nombre = $_POST['nombre'];
$email = $_POST['email'];
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
$errors = array();
if(empty($nombre)){
$errors[] = 'El campo nombre es obligatorio';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors[] = 'La dirección de correo electrónico no es válida';
}
if(empty($asunto)){
$errors[] = 'El campo asunto es obligatorio';
}
if(empty($mensaje)){
$errors[] = 'El campo mensaje es obligatorio';
}
if(count($errors) == 0){
$msj = "De: $nombre <a href='mailto:$email'>$email</a><br>";
$msj .= "Asunto: $asunto<br><br>";
$msj .= "Cuerpo del mensaje:";
$msj .= '<p>' .$mensaje. '</p>';
$msj .= "--<p>Este mensaje se ha enviado desde un formulario de contacto en PHP y MySQL</p>";
$mail = new PHPMailer(true);
try{
$mail -> SMTPDebug = SMTP::DEBUG_OFF;
$mail -> isSMTP();
$mail -> Host = 'mail.dominio.com';
$mail -> SMTPAuth = true;
$mail -> Username = '[email protected]';
$mail -> Password = 'TuPassword';
$mail -> SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail -> port = 465;
$mail -> setFrom('[email protected]','Emisor');
$mail -> addAddress('[email protected]', 'Receptor');
//$mail -> addReplyTo('[email protected]');
$mail -> isHTML(true);
$mail -> Subject = 'Formulario de contacto';
$mail -> Body = utf8_decode($smj);
$mail -> send();
$respuesta = 'Correo enviado de forma extitosa';
} catch (Exception $e){
$respuesta = 'Mensaje ' . $mail -> ErrorInfo;
}
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contacto</title>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<main>
<div class="container py-3">
<header class="mb-4 border-bottom">
<h1 class="fs-4">Contacto</h1>
</header>
<?php
if(isset($errors)){
if(count($errors) > 0){
?>
<div class="row">
<div class="col-lg-6 col-md-12">
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php
foreach($errors as $error){
echo $error . '<br>';
}
?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
</div>
<?php
}
}
?>
<div class="row">
<div class="col-lg-6 col-md-12">
<form class="form" method="POST" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" autocomplete="off">
<div class="mb-3">
<label for="nombre" class="form-label"> <b>Nombres y Apellidos:</b> </label>
<input type="text" class="form-control" id="nombre" name="nombre" required autofocus>
</div>
<div class="mb-3">
<label for="email" class="form-label"> <b>Correo Electrónico:</b> </label>
<input type="text" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="asunto" class="form-label"> <b>Asunto:</b> </label>
<input type="text" class="form-control" id="asunto" name="asunto" required>
</div>
<div class="mb-3">
<label for="mensaje" class="form-label"> <b>Mensaje:</b> </label>
<textarea class="form-control" id="mensaje" name="mensaje" rows="3" required> </textarea>
</div>
<botton type="submit" name="submit" class="btn btn-primary">Enviar</button>
</form>
</div>
</div>
<?php if(isset($respuesta)){ ?>
<div class="row py-3">
<div class="col-lg-6 col-md-12">
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $respuesta; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
</div>
<?php } ?>
</div>
</main>
</body>
</html>