https://kincoding.com/entry/Google-Gmail-SMTP-%EC%82%AC%EC%9A%A9%EC%9D%84-%EC%9C%84%ED%95%9C-%EC%84%B8%ED%8C%85-2025%EB%85%84-%EB%B2%84%EC%A0%84#google_vignette
- 기본 셋팅은 위에 링크를 기반으로 GCP 가서 세팅을 한다.
- 셋팅이 끝나면 edge function 만들기
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'
}
});
}
const attachmentUrls = [];
if (attachments) {
if (Array.isArray(attachments)) attachmentUrls.push(...attachments);
else if (typeof attachments === 'string') attachmentUrls.push(attachments);
}
const attachmentsHtml = attachmentUrls.length ? `<h4>첨부파일(URL)</h4><ul>` + attachmentUrls.map((u)=>`<li><a href="${u}">${u}</a></li>`).join('') + `</ul>` : '';
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'
}
});
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: SMTP_USER,
pass: SMTP_PASS
}
});
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
};
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'
}
});
}
});
- 위 내용 기반으로 edge function 이용해서 메일 송부 요청 하자.