1. EmailJS
1) ์ ์ฌ์ดํธ์ ์ ์ํด ํ์๊ฐ์ ํ, Email Service > Add New Service ์ ํ
2) Gmail ์ถ๊ฐํด ๋ฉ์ผ์ ๋ฐ์ ๊ตฌ๊ธ ์์ด๋์ ์ฐ๋
3) Email Templates๋ก ๊ฐ์ ์์ ์ด๋ฉ์ผ ์์ ์์
- ์ฌ๊ธฐ์ ์ฌ์ฉ๋๋ name, email, message ๋ณ์๋ ๋ด๊ฐ ์ฝ๋์ form์์ ์ฌ์ฉํ input name ๊ฐ๊ณผ ๋์ผํด์ผ ํจ
2. EmailJS ๋ฆฌ์กํธ ์ฝ๋ ์์ฑ
1) emailjs sdk ์ค์น
npm install @emailjs/browser --save
2) emailjs ๋ฆฌ์กํธ ์ฝ๋
import React, { useRef } from 'react';
import emailjs from '@emailjs/browser';
export const ContactUs = () => {
const form = useRef();
const sendEmail = (e) => {
e.preventDefault();
emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', form.current, 'YOUR_PUBLIC_KEY')
.then((result) => {
console.log(result.text);
}, (error) => {
console.log(error.text);
});
};
return (
<form ref={form} onSubmit={sendEmail}>
<label>Name</label>
<input type="text" name="user_name" />
<label>Email</label>
<input type="email" name="user_email" />
<label>Message</label>
<textarea name="message" />
<input type="submit" value="Send" />
</form>
);
};
- EmailJS์ ๊ณต์ ๋ฌธ์ ์ฝ๋์. ์ด์ง๋ง ๋ฐ๊ฟ์ ๊ทธ๋๋ก ๋ฐ์ํ๋ฉด ๋จ.
3) sendForm์ผ๋ก ์ ์กํ Key๋ค์ ์์น
- Service ID, Template ID, Public ID๊ฐ ํ์ํ๋ฐ, ์๋ค๋ค์ ํ๊ฒฝ๋ณ์๋ก ๊ด๋ฆฌ!
Service ID
Template ID
Public ID
4) ์ต์ข ์ฝ๋
ContactDetail.js
import React, { forwardRef, useRef } from "react";
import emailjs from "@emailjs/browser";
import Card from "../components/Card";
import "../Details_css/ContactDetail.css";
const ContactDetail = forwardRef((props, ref) => {
const form = useRef();
const sendEmailHandler = (e) => {
e.preventDefault();
emailjs
.sendForm(
process.env.REACT_APP_EMAILJS_SERVICE_ID,
process.env.REACT_APP_EMAILJS_TEMPLATE_ID,
form.current,
process.env.REACT_APP_EMAILJS_PUBLIC_KEY
)
.then(
function (response) {
console.log("SUCCESS!", response.status, response.message);
alert(
"์ด๋ฉ์ผ ์ ์ก์ด ์๋ฃ๋์์ต๋๋ค! ๋น ๋ฅธ ์์ผ ๋ด์ ๋ต์ฅ ๋๋ฆฌ๊ฒ ์ต๋๋ค."
);
form.current.name.value = "";
form.current.email.value = "";
form.current.message.value = "";
},
function (error) {
console.log("FAILED...", error);
}
);
};
return (
<div className="Contact" ref={ref}>
<h1 className="contact-title">
<a name="Contact">Contact</a>
</h1>
<p>
<a href="mailto:leesengjie@jbnu.ac.kr" className="email-link">
๐ง leesengjie@jbnu.ac.kr
</a>
</p>
<div className="contact-card-wrapper">
<Card className="contact-card">
<form className="contents" ref={form} onSubmit={sendEmailHandler}>
<h3>Name</h3>
<input type="text" name="name" />
<h3>Email</h3>
<input type="email" name="email" />
<h3>Message</h3>
<textarea name="message" />
<div className="send-btn">
<button type="submit">Send</button>
</div>
</form>
</Card>
</div>
</div>
);
});
export default ContactDetail;
3. ๊ฒฐ๊ณผ ๐