Frontend ๐Ÿ“š/React

[React]GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 4. EmailjS๋ฅผ ์ด์šฉํ•ด ์„œ๋ฒ„ ์—†์ด ์ด๋ฉ”์ผ ์ „์†ก ๊ตฌํ˜„ํ•˜๊ธฐ

leejaejae 2024. 9. 18. 18:00

1. EmailJS

https://www.emailjs.com

 

Send email directly from your code | EmailJS

No server side code required. Add static or dynamic attachments, dynamic parameters, captcha code and more. Start with our free tier!

www.emailjs.com

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. ๊ฒฐ๊ณผ ๐ŸŽ‰

 

'Frontend ๐Ÿ“š > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React]GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 6. ์™„์„ฑ ๋ฐ ํšŒ๊ณ   (1) 2024.09.20
[React]GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 5. ํ”„๋กœ์ ํŠธ ์Šฌ๋ผ์ด๋” + ํ”„๋กœ์ ํŠธ ๋‚ด ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋”  (1) 2024.09.20
[React]GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 3. Header ๊ตฌํ˜„ (useref ๋ฅผ ์ด์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ฐ„ ์Šคํฌ๋กค ์ด๋™) + ์ตœ์ƒ๋‹จ ์ด๋™๋ฒ„ํŠผ  (2) 2024.09.18
[React]GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 2. Archiving, Skills, Project, Contact ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ๋ฐ ๊ธฐ๋ณธ CSS ์ž‘์„ฑ + ๊ธ€์”จ์ฒด ๋ณ€๊ฒฝ  (1) 2024.09.18
[React] GitHub Pages + React๋กœ ๋‚˜๋งŒ์˜ ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“ค๊ธฐ - 1. ๊ธฐํš ๋ฐ ๋””์ž์ธ ๊ตฌ์„ฑ  (0) 2024.09.04