[edge function]supabase edge function

코드왕·2025년 10월 23일
  1. edge function deno버전
import nodemailer from "npm:nodemailer@6.9.4";
console.info('send-contact-email (urls-in-body) function started');
Deno.serve(async (req)=>{
  try {
    if (req.method !== 'POST') return new Response(JSON.stringify({
      error: 'POST only'
    }), {
      status: 405,
      headers: {
        'Content-Type': 'application/json'
      }
    });
    const contentType = req.headers.get('content-type') || '';
    const isJson = contentType.includes('application/json');
    const body = isJson ? await req.json() : await req.formData().then((fd)=>{
      const obj = {};
      for (const [k, v] of fd.entries())obj[k] = v;
      return obj;
    });
    const { category, email, name, phone, subject, message, consent, attachments } = body;
    if (!email || !subject || !message) {
      return new Response(JSON.stringify({
        error: 'Missing required fields: email, subject, message'
      }), {
        status: 400,
        headers: {
          'Content-Type': 'application/json'
        }
      });
    }
    // attachments expected as array of URLs or single URL
    const attachmentUrls = [];
    if (attachments) {
      if (Array.isArray(attachments)) attachmentUrls.push(...attachments);
      else if (typeof attachments === 'string') attachmentUrls.push(attachments);
    }
    // Build list HTML for URLs
    const attachmentsHtml = attachmentUrls.length ? `<h4>첨부파일(URL)</h4><ul>` + attachmentUrls.map((u)=>`<li><a href="${u}">${u}</a></li>`).join('') + `</ul>` : '';
    // Read secrets from env
    const SMTP_USER = Deno.env.get('SMTP_USER');
    const SMTP_PASS = Deno.env.get('SMTP_PASS');
    const TO_EMAIL = Deno.env.get('TO_EMAIL') || 'inscript2023@gmail.com';
    if (!SMTP_USER || !SMTP_PASS) return new Response(JSON.stringify({
      error: 'Missing SMTP credentials in environment'
    }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    });
    // Create transporter
    const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: SMTP_USER,
        pass: SMTP_PASS
      }
    });
    // Build email body
    const html = `
      <h3>인스크립트 문의 메일이 도착하였습니다.</h3>
      <ul>
        <li><strong>카테고리:</strong> ${category || ''}</li>
        <li><strong>이메일:</strong> ${email}</li>
        <li><strong>이름:</strong> ${name || ''}</li>
        <li><strong>연락처:</strong> ${phone || ''}</li>
        <li><strong>내용:</strong><pre>${message}</pre></li>
      </ul>
      ${attachmentsHtml}
    `;
    const textAttachments = attachmentUrls.length ? '첨부파일:\n' + attachmentUrls.join('\n') : '';
    const mailOptions = {
      from: `${name || ''} <${SMTP_USER}>`,
      to: TO_EMAIL,
      subject: subject,
      text: `${message}\n\nFrom: ${name || ''} <${email}>\nPhone: ${phone || ''}\n\n${textAttachments}`,
      html
    };
    // Send mail
    const info = await transporter.sendMail(mailOptions);
    return new Response(JSON.stringify({
      ok: true,
      info
    }), {
      status: 200,
      headers: {
        'Content-Type': 'application/json'
      }
    });
  } catch (err) {
    console.error(err);
    return new Response(JSON.stringify({
      error: err.message
    }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }
});
profile
CODE DIVE!

0개의 댓글