JavaScript로 이메일 보내는 방법: SendLayer API 완전 가이드
Source: Dev.to
전제 조건
- Node.js가 머신에 설치되어 있어야 합니다. 최신 버전은 여기서 다운로드하세요.
- 코드 편집기(예: Visual Studio Code).
- HTML 및 JavaScript에 대한 기본 지식.
- SendLayer 계정. 무료 체험 계정을 통해 최대 200통의 이메일을 무료로 보낼 수 있습니다.
SendLayer에서 무료 체험 시작
계정을 만든 후, 도메인 인증을 반드시 진행하세요. 이 단계는 사이트의 이메일 전달성을 향상시키는 데 필수적입니다.
SendLayer API를 통한 이메일 전송 (권장)
API를 이용한 이메일 전송은 SMTP보다 빠르고 안전합니다. SendLayer와 같은 트랜잭션 이메일 서비스는 프로그래밍 방식으로 이메일을 보낼 수 있는 API와 고급 기능을 제공합니다.
이 가이드에서는 SendLayer SDK를 사용합니다. SDK에는 이메일 검증, 오류 처리, 파일 첨부 인코딩 등의 기능이 기본 제공됩니다.
API 키 가져오기
SendLayer SDK를 사용하려면 활성 API 키가 필요합니다. SendLayer 계정에 로그인하고 Settings » API Keys 로 이동합니다.

API 키를 복사하세요 – 다음 단계에서 사용할 것입니다.
SendLayer SDK 설치
npm install sendlayer
첫 번째 이메일 전송
새 JavaScript 파일(e.g., emailService.js)을 만들고 다음 코드를 추가합니다:
import { SendLayer } from 'sendlayer';
const apiKey = 'your-sendlayer-api-key';
// Initialize the SendLayer package with your API key
const sendlayer = new SendLayer(apiKey);
const sendEmail = async () => {
// Email data
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test Email via SendLayer API',
text: 'This is a test email sent via the SendLayer API.'
};
// email sending function with error handling
try {
const response = await sendlayer.Emails.send(params);
console.log('Email sent successfully:', response);
} catch (error) {
console.error('Error sending email:', error.message);
}
};
sendEmail();
코드 설명
from– 발신자 이메일 주소(인증된 도메인에 속해야 함).to– 수신자 이메일 주소 또는 수신자 목록.subject– 이메일 제목.text또는html– 이메일 내용(일반 텍스트 또는 HTML).
sendlayer.Emails.send() 메서드는 이메일을 전송하고, try...catch 블록은 오류를 처리합니다.
코드 실행
node emailService.js
성공하면 다음과 같은 확인 메시지가 표시됩니다:
{
"MessageID": "62e09048-039f-4fce-a744-b096981e4990"
}

HTML 이메일 전송
HTML 형식의 이메일을 보내려면 text 대신 html 매개변수를 사용합니다:
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test HTML Email',
html: '
## Hello!
This is an **HTML email** sent via the SendLayer API.
'
};
다수 수신자에게 전송
여러 주소에 대해 수신자 객체 배열을 제공하세요:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [
{ name: "Recipient 1", email: "recipient1@example.com" },
{ name: "Recipient 2", email: "recipient2@example.com" }
],
subject: "Test Email to Multiple Recipients",
html: "
This email is sent to multiple recipients.
"
};
CC 및 BCC 수신자를 포함할 수도 있습니다:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [{ name: "Recipient 1", email: "recipient1@example.com" }],
subject: "Test Email",
html: "
This email includes CC and BCC recipients.
",
cc: ["cc@example.com"],
bcc: [{ name: "BCC", email: "bcc@example.com" }]
};
Note: 이메일 요청당 수신자 수에 제한이 있습니다. 자세한 내용은 개발자 문서를 참조하세요.
첨부 파일이 있는 이메일 전송
params에 attachments 필드를 추가하세요. SDK가 자동으로 인코딩을 처리합니다.
const params = {
from: { name: 'Sender Name', email: 'sender@example.com' },
to: [{ name: 'Recipient Name', email: 'recipient@example.com' }],
subject: 'Test Email with Attachment',
html: '
This email includes an attachment.
',
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
}
]
};
const response = await sendlayer.Emails.send(params);
여러 파일을 첨부할 수 있으며, 원격 URL도 가능합니다:
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
},
{
path: 'https://example.com/image.png',
type: 'image/png'
}
]
Pro Tip: type 필드는 파일의 MIME 타입과 일치시켜야 이메일 클라이언트가 올바르게 처리합니다.