nodemailer

This commit is contained in:
Ruidy Nemausat 2020-03-08 13:03:55 +01:00
parent 90f9bda6f4
commit 5b2cc29195
6 changed files with 67 additions and 12 deletions

3
.gitignore vendored
View file

@ -22,4 +22,5 @@ npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
# /src/utils/auth_config.json /src/utils/auth_config.json
/src/utils/secret.js

2
package-lock.json generated
View file

@ -14215,4 +14215,4 @@
} }
} }
} }
} }

View file

@ -33,4 +33,4 @@
"last 1 safari version" "last 1 safari version"
] ]
} }
} }

View file

@ -1,18 +1,13 @@
import React, { useState } from "react"; import React, { useState } from "react";
// import { useInput } from "../utils/inputHook"; // import { notificationMail, confirmationMail } from "../utils/mail";
export const ContactForm = ({ onSubmit }) => { export const ContactForm = ({ setIsSubmitted }) => {
const [firstName, setFirstName] = useState(""); const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState(""); const [lastName, setLastName] = useState("");
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [phone, setPhone] = useState(""); const [phone, setPhone] = useState("");
const [message, setMessage] = useState(""); const [message, setMessage] = useState("");
const handleSubmit = ev => {
ev.preventDefault();
onSubmit(true);
};
const changeFirstName = ev => { const changeFirstName = ev => {
const { value } = ev.target; const { value } = ev.target;
setFirstName(value); setFirstName(value);
@ -34,6 +29,14 @@ export const ContactForm = ({ onSubmit }) => {
setMessage(value); setMessage(value);
}; };
const handleSubmit = ev => {
ev.preventDefault();
// confirmationMail(email);
// const body = `Sender: ${firstName} ${lastName}\nPhone: ${phone}\nMessage: ${message}`;
// notificationMail(email, `New message from ${firstName} ${lastName}`, body);
setIsSubmitted(true);
};
return ( return (
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<div className="row"> <div className="row">
@ -61,7 +64,13 @@ export const ContactForm = ({ onSubmit }) => {
/> />
</div> </div>
<div className="col s12 m6"> <div className="col s12 m6">
<ContactFormInput id="Phone" value={phone} onChange={changePhone} /> <ContactFormInput
id="Phone"
value={phone}
type="tel"
// pattern="[0-9]"
onChange={changePhone}
/>
</div> </div>
<div className="col s12"> <div className="col s12">
<ContactFormTextArea <ContactFormTextArea
@ -87,6 +96,7 @@ const ContactFormInput = ({ id, type = "text", value, onChange }) => {
id={id} id={id}
value={value} value={value}
onChange={onChange} onChange={onChange}
required
/> />
<label htmlFor={id}>{id}</label> <label htmlFor={id}>{id}</label>
</div> </div>
@ -103,6 +113,7 @@ const ContactFormTextArea = ({ id, value, onChange }) => {
name={id} name={id}
value={value} value={value}
onChange={onChange} onChange={onChange}
required
/> />
</div> </div>
); );

View file

@ -17,7 +17,7 @@ export const ContactPage = () => {
) : ( ) : (
<div className="container"> <div className="container">
<h2 className="logo">Contact Us</h2> <h2 className="logo">Contact Us</h2>
<ContactForm onSubmit={setIsSubmitted} /> <ContactForm setIsSubmitted={setIsSubmitted} />
</div> </div>
); );
}; };

43
src/utils/mail.js Normal file
View file

@ -0,0 +1,43 @@
// This must be set on the server using express
import { createTransport } from "nodemailer";
import { mailPassword } from "./secret";
const myMail = "ruidy.nemausat@gmail.com";
const myPass = mailPassword;
const handleMail = (mailTo, subject, text) => {
let transporter = createTransport({
service: "gmail",
auth: {
user: myMail,
pass: myPass
}
});
let mailOptions = {
from: myMail,
to: mailTo,
subject: subject,
text: text
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
};
export const confirmationMail = (
mailTo,
subject = "Your message has been sent",
text = "Thanks for your message. We'll reply you soon."
) => {
handleMail(mailTo, subject, text);
};
export const notificationMail = (mailTo = myMail, subject, text) => {
handleMail(mailTo, subject, text);
};