import sendgrid from "@sendgrid/mail";
import { error } from "console";

export class SendGridService {
  constructor() {
    if (!process.env.SENDGRID_API_KEY) {
      throw error;
    }
    sendgrid.setApiKey(process.env.SENDGRID_API_KEY);
  }

  /**
   * メールを送信する
   * @param to
   * @param from
   * @param subject
   * @param text
   * @param html
   * @returns mail
   */
  async sendMail(
    to: string,
    from: string,
    subject: string,
    text: string,
    html: string
  ): Promise<void> {
    try {
      const res = await sendgrid
        .send({
          from: {
            email: from,
          },
          to: {
            email: to,
          },
          subject: subject,
          text: text,
          html: html,
        })
        .then((v) => v[0]);
    } catch (e) {
      throw e;
    }
  }
}
