Skip to content

Instantly share code, notes, and snippets.

@HectorBlisS
Created May 23, 2022 20:18
Show Gist options
  • Save HectorBlisS/2c93fa416e7c82cb2ac9d5d92f346ac4 to your computer and use it in GitHub Desktop.
Save HectorBlisS/2c93fa416e7c82cb2ac9d5d92f346ac4 to your computer and use it in GitHub Desktop.
import './App.css';
function App() {
const handleSubmit = async (event) => {
event.preventDefault();
const { target } = event;
const form = {
name: target.name.value,
email: target.email.value,
message: target.message.value,
};
const result = await fetch(
'/contact_form',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ ...form, secret: 'firebaseIsCool' }),
}
);
//if (result.ok).....
alert("Pronto nos pondremos en contacto contigo!");
// route to another page...
}
return (
<form onSubmit={handleSubmit}>
<label>
Escribe tu nombre
<input name="name" type="text" />
</label>
<label>
Escribe tu Email
<input name="email" type="text" />
</label>
<label>
Escribe tu mensaje
<input name="message" type="text" />
</label>
<button>enviar</button>
</form>
);
}
export default App;
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/contact_form",
"function": "contactForm"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
}
const functions = require("firebase-functions");
const nodemailer = require('nodemailer');
const transport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "[email protected]",
pass: "hwmhrwisevhugrgf",
},
});
const sendContactForm = (form) => {
return transport
.sendMail({
subject: "👾🤖Nuevo mensaje de tu formulario de contacto😎",
bcc: ["[email protected]"],
html: `<h3>¡Tienes un nuevo mensaje!</h3>
<p> Nombre: ${form.name} </p>
<p> Correo: ${form.email} </p>
<p>Mensaje: ${form.message} </p>
`,
})
.then((r) => {
console.log("Accepted => ", r.accepted)
console.log("Rejected => ", r.rejected)
})
.catch((e) => console.log(e))
}
exports.contactForm = functions.https.onRequest((request, response) => {
if (req.body.secret !== 'firebaseIsCool') return res.send('Missing secret');
sendContactForm(req.body);
res.send("Sending email...");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment