반응형
SMTP 테스트: 첨부파일 포함 메일 보내기
SMTP 서버를 개발하거나 디버깅할 때, 첨부파일 포함 메일을 빠르게 보내는 방법에 대해서 정리해 보았습니다.
1. swaks (추천)
SMTP 전용 테스트 도구로, 첨부파일을 포함한 거의 모든 시나리오를 한 줄로 처리가능.
# 설치
sudo apt install swaks # Debian/Ubuntu
brew install swaks # macOS
# 첨부파일 포함 전송
swaks --to user@example.com --from sender@example.com \
--server localhost --port 2525 \
--attach report.pdf \
--auth --auth-user user --auth-password pass
--attach 옵션 하나로 끝. 여러 파일은 --attach를 반복하면 됩니다.
2. Python
별도 설치 없이 Python 표준 라이브러리만으로 가능.
python3 -c "
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = 'from@example.com'
msg['To'] = 'to@example.com'
msg['Subject'] = 'Test'
msg.set_content('Hello')
with open('report.pdf', 'rb') as f:
msg.add_attachment(f.read(), maintype='application',
subtype='octet-stream', filename='report.pdf')
s = smtplib.SMTP('localhost', 2525)
s.login('user', 'pass')
s.send_message(msg)
s.quit()
"
EmailMessage가 MIME multipart 구조를 자동으로 만들어 줌.
3. curl
curl은 SMTP를 지원하지만, 첨부파일은 MIME 형식의 raw 이메일 파일을 직접 작성해야 함.
먼저 이메일 파일(email.txt)을 작성한다:
From: sender@example.com
To: user@example.com
Subject: Test with attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="BOUNDARY"
--BOUNDARY
Content-Type: text/plain
Hello, this is the body.
--BOUNDARY
Content-Type: application/octet-stream; name="report.pdf"
Content-Disposition: attachment; filename="report.pdf"
Content-Transfer-Encoding: base64
(base64 인코딩된 파일 내용)
--BOUNDARY--
첨부파일을 base64로 인코딩해서 넣고 전송:
# 첨부파일 인코딩
base64 report.pdf >> email.txt
echo "--BOUNDARY--" >> email.txt
# 전송
curl --url smtp://localhost:2525 \
--mail-from sender@example.com \
--mail-rcpt user@example.com \
--upload-file email.txt \
--user "user:pass"
정리
| 도구 | 첨부파일 | 난이도 |
|---|---|---|
| swaks | --attach 한 줄 |
쉬움 |
| Python | add_attachment() |
쉬움 |
| curl | MIME 직접 작성 | 번거로움 |
간단한 테스트라면 swaks가 가장 편하고, 설치 없이 해야 한다면 Python이 차선입니다.
반응형