commit fc3784f86138801f152b67311d14c52e9b5ad24e Author: Andros Fenollosa Date: Thu Jun 24 15:14:25 2021 +0200 First commit diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6569021 --- /dev/null +++ b/composer.json @@ -0,0 +1,8 @@ +{ + "require": { + "phpmailer/phpmailer": "^6.4", + "sendgrid/sendgrid": "^7.9", + "fzaninotto/faker": "^1.9", + "twig/twig": "^3.0" + } +} diff --git a/contacto.php b/contacto.php new file mode 100644 index 0000000..2ea2784 --- /dev/null +++ b/contacto.php @@ -0,0 +1,90 @@ + + + + + + + + Contacto + + + +
+

Formulario de contacto

+
+
+

+ +

+ +

+ Campo obligatorio +

+ +

+ +

+ +

+ Campo obligatorio +

+ + +

+ Formato no valido +

+ +

+ +

+ +

+ Campo obligatorio +

+ + +

+ Debe tener mas de 20 caracteres +

+ +

+ > Acepto que rastres y vendas mis datos +

+ +

+ Debes aceptar nuestras condiciones +

+ + +

+ Enviado con exito +

+ +

+ +

+
+
+
+ + \ No newline at end of file diff --git a/contactoModel.php b/contactoModel.php new file mode 100644 index 0000000..4c64a4f --- /dev/null +++ b/contactoModel.php @@ -0,0 +1,120 @@ + boo@foo.a + if (filter_var($campo, FILTER_VALIDATE_EMAIL)) { + $emailSeparadoExtension = explode('.', $campo); + if (count($emailSeparadoExtension) === 2 + && strlen($emailSeparadoExtension[1]) < 2) { + return false; + } + } + // Otros + return filter_var($campo, FILTER_VALIDATE_EMAIL); +} + +/** + * Valida que $texto no supere $limite + * @param string $texto + * @param int $limite + * @return bool + */ +function validarLetrasMaximas(string $texto, int $limite): bool +{ + return strlen(trim($texto)) <= $limite; +} + +//====================================================================== +// INICIO +//====================================================================== + +// Envia el correo si al informacion es correcta +if ($validado) { + + // Generamos las plantillas + $loader = new \Twig\Loader\FilesystemLoader('templates'); + $twig = new \Twig\Environment($loader); + $variablesEmail = [ + 'nombre' => $nombre, + 'email' => $email, + 'mensaje' => $mensaje + ]; + $plantillaPlana = $twig->render('contacto.txt', $variablesEmail); + $plantillaHTML = $twig->render('contacto.html', $variablesEmail); + + // Enviamos email + $emailSendGrid = new \SendGrid\Mail\Mail(); + $emailSendGrid->setFrom("andros@fenollosa.email", "Web"); + $emailSendGrid->setSubject("Contacto desde mi web"); + $emailSendGrid->addTo("andros@fenollosa.email", "Yo"); + $emailSendGrid->addContent("text/plain", $plantillaPlana); + $emailSendGrid->addContent( "text/html", $plantillaHTML); + $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); + try { + $response = $sendgrid->send($emailSendGrid); + } catch (Exception $e) { + echo 'Caught exception: ' . $e->getMessage() . "\n"; + } +} + diff --git a/contactoTest.php b/contactoTest.php new file mode 100644 index 0000000..1d0f6ae --- /dev/null +++ b/contactoTest.php @@ -0,0 +1,49 @@ +assertSame(true, validarObligatorio('texto'), "Validador obligatorio no reconoce 'texto'"); + $this->assertSame(false, validarObligatorio(''), "Validador obligatorio no reconoce ''"); + } + + public function testFormatoEmail(): void + { + // Buenos + $miFaker = Faker\Factory::create(); + foreach (range(0, 50) as $pos) { + $tempEmail = $miFaker->email; + $this->assertSame(true, validarFormatoEmail($tempEmail), "Validador email $tempEmail valido"); + } + // Malos + $this->assertSame(false, validarFormatoEmail('@correo'), "Validador falso email"); + $this->assertSame(false, validarFormatoEmail('@correo.com'), "Validador falso email"); + $this->assertSame(false, validarFormatoEmail('mi@correo'), "Validador falso email"); + $this->assertSame(false, validarFormatoEmail('micorreo'), "Validador falso email"); + $this->assertSame(false, validarFormatoEmail('micorreo@ff.e'), "Validador falso email"); + $this->assertSame(false, validarFormatoEmail('micorreo@f-f.e'), "Validador falso email"); + } + + + public function testLetrasMaximas(): void + { + + // Funciona - Limite 20 con caracteres de 0 a 20 + foreach (range(0, 20) as $pos) { + $tempText = generateRandomString($pos); + $this->assertSame(true, validarLetrasMaximas($tempText, 20), "Validador limite $pos maximo '$tempText'"); + } + + // No funciona - Limite 20 con caracteres de 21 a 40 + foreach (range(21, 40) as $pos) { + $tempText = generateRandomString($pos); + $this->assertSame(false, validarLetrasMaximas($tempText, 20), "Validador limite $pos maximo '$tempText'"); + } + } +} + diff --git a/index.php b/index.php new file mode 100644 index 0000000..b29b559 --- /dev/null +++ b/index.php @@ -0,0 +1,20 @@ +setFrom("andros@fenollosa.email", "Example User"); +$email->setSubject("Sending with SendGrid is Fun"); +$email->addTo("andros@fenollosa.email", "Example User"); +$email->addContent("text/plain", "and easy to do anywhere, even with PHP"); +$email->addContent( + "text/html", "and easy to do anywhere, even with PHP" +); +$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); +try { + $response = $sendgrid->send($email); + print $response->statusCode() . "\n"; + print_r($response->headers()); + print $response->body() . "\n"; +} catch (Exception $e) { + echo 'Caught exception: ' . $e->getMessage() . "\n"; +} \ No newline at end of file diff --git a/templates/contacto.html b/templates/contacto.html new file mode 100644 index 0000000..9be85f5 --- /dev/null +++ b/templates/contacto.html @@ -0,0 +1,16 @@ + + + + + Contacto + + +

Nuevo mensaje

+

Nombre: {{ nombre }}

+

Email: {{ email }}

+

Mensaje: {{ mensaje }}

+
+

Escribirle

+

Gracias

+ + \ No newline at end of file diff --git a/templates/contacto.txt b/templates/contacto.txt new file mode 100644 index 0000000..a10969a --- /dev/null +++ b/templates/contacto.txt @@ -0,0 +1 @@ +Mensaje de {{ nombre }} con el email {{ email }} y el mensaje es: {{ mensaje }}