Postfix 메일 발송 - Postfix meil balsong

2014년 7월 15일

서비스나 웹사이트를 테스트를 하다보면 사용자에게 메일이 발송되는지 확인해야 하는 경우가 있다. 또한 실제로 운영되는 서비스를 테스트 환경에 놓고 테스트 하다가 메일이 사용자에게 발송되어 버리는 경우가 생길 수 있다. 물론 sendmail을 꺼두는 것도 방법이긴 하지만… Postfix를 사용하고 있다면 서버에서 발송되는 모든 메일을 하나의 메일 주소로 수신하게 설정할 수 있다. (Postfix가 서버에 없다면 설치하자.)

먼저 /etc/postfix/recipient_canonical_map 파일을 다음과 같이 작성한다.

이 맵핑 파일에서 앞부분은 정규표현식이며 수신자가 정규표현식에 해당하면 뒷부분의 이메일로 리다이렉트 하도록 처리해준다. 정규표현식으로 여러 조건을 만들어 여러 이메일로 리다이렉트 할 수도 있다. 해당 파일을 저장하고 나서 /etc/postfix/main.cf를 열어 맵핑한 파일을 참조하도록 다음 설정을 추가한다.

recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

위 설정에서 recipient_canonical_classes는 기본값이 envelope_recipient, header_recipient인데 header_recipient는 수신자를 해당 리다이렉트 되는 이메일로 치환해버리기 때문에 확인하는데 불편함이 있어서 위와 같이 envelope_recipient만 설정한다. 두번째는 보는 것과 같이 맵파일의 경로를 설정해준다.

수정을 완료하고 저장한 후 postfix 설정을 다시 불러오면 모든 과정이 끝난다.

postfix 설정에 관한 자세한 내용은 postfix 문서에서 확인할 수 있다.

참고로 Windows에서는 Papercut을 켜두면 해당 서버에서 발송되는 메일이 실제로는 발송되지 않고 모두 로컬에 저장되어 그 내용을 확인할 수 있다.

  • ← MAMP에서 Sublime Text로 Xdebug 사용하기
  • Mono 환경에서 OWIN 시작하기 →

  POSTFIX 구축 시 letsencrypt 인증기관에서 발급받은 인증서를 사용할 것이다.  앞서 아래 콘텐츠를 먼저 열람하길 바란다.

[홈서버] letsencrypt 인증서 발급 받기

메일 서버, 웹서버 운영을 보안 프로토콜로 사용하기 위해서는 허가받은 인증기관의 인증서를 사용하는 것이 좋다. 이런 인증기관의 인증서는 대부분 유료고 개인이 홈서버를 위해 발급받기는

malchooni.name

Postfix 메일 발송 - Postfix meil balsong

  메일 서버는 발송하는 부분과 사용자가 수신받는 부분으로 나눌 수 있다.  메일을 발송하는 SMTP 서버 구축을 위해 POSTFIX를 사용하기로 한다.  사이트에서 POSTFIX는 구글의 'Wietse Venema'라는 개발자가 개발했다고 한다.

The Postfix Home Page

The Postfix Home Page All programmers are optimists -- Frederick P. Brooks, Jr. First of all, thank you for your interest in the Postfix project. What is Postfix? It is Wietse Venema's mail server that started life at IBM research as an alternative to the

www.postfix.org

 OS는 우분투 20.04 버전에서 작업을 하였다.  설치는 간단한 명령어로 가능하다.

  > apt-get install postfix

root@mail:/# apt-get install postfix
.
.

Postfix Configuration
---------------------


Please select the mail server configuration type that best meets your needs.


 No configuration:
  Should be chosen to leave the current configuration unchanged.
 Internet site:
  Mail is sent and received directly using SMTP.
 Internet with smarthost:
  Mail is received directly using SMTP or by running a utility such
  as fetchmail. Outgoing mail is sent using a smarthost.
 Satellite system:
  All mail is sent to another machine, called a 'smarthost', for delivery.
 Local only:
  The only delivered mail is the mail for local users. There is no network.


  1. No configuration  2. Internet Site  3. Internet with smarthost  4. Satellite system  5. Local only
General type of mail configuration: 1


Unpacking postfix (3.4.13-0ubuntu1) ...

  Postfix 설치 시 설치 유형을 물어본다, No configuration을 선택 후 세부적인 설정은 환경설정 파일을 변경하여 진행할 예정이다.

  Postfix의 설정 파일은 /etc/postfix 디렉터리의 main.cf 와 master.cf로 구성되어있다.  이 파일들에 대한 권한은 루트에게 있어야 한다. 환경 설정 변경 후 적용은 재시작 또는 "postfix reload" 명령어를 활용한다.

  설정에 앞서 main.cf 파라미터 정보는 공식 사이트에서 확인할 수 있다.

  보통 /etc/postfix 디렉터리로 이동 후 프로토타입의 설정 파일을 main.cf로 복사 한다. 그러나 필자는 파라미터 정보를 확인 후 새로운 파일로 생성하겠다.

root@mail:/etc/postfix# vi main.cf
# 도메인 이름 (기본값 >>  postconf -d | grep mydomain )
mydomain = yalsooni.name

# hotname (기본값 >>  postconf -d | grep mydomain )
myhostname = mail.yalsooni.name
# 배달되는 도메인 목록
mydestination = $myhostname, $mydomain, localhost
# 배달되는 도메인 이름 (local)
myorigin = $mydomain
# smtp 접속 인사말
smtpd_banner = $myhostname ESMTP $mail_name
# 알림 설정 제거
biff = no
# 메일 크기 제한 (기본값 51200000)
mailbox_size_limit = 0
# 주소 확장 구분 기호
recipient_delimiter = +
# 사용자 홈 디렉터리 설정 for qmail-style delivery
home_mailbox = Maildir/
# 이전 버전과 호환을 위한 설정
compatibility_level=2
# 전달 별칭 데이터베이스 설정
alias_maps = hash:/etc/aliases


# SASL 인증 활성화
smtpd_sasl_auth_enable = yes
# 비표준 지원 여부 (RFC 4954)
broken_sasl_auth_clients = yes
# Dovecot SASL 사용
smtpd_sasl_type = dovecot
# UNIX Domain 
smtpd_sasl_path = private/auth
# 인증 메커니즘
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = noanonymous
smtpd_tls_auth_only = no
smtpd_relay_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
# 로그인 이름에 도메인 추가 e.g. "john" instead of ""
smtpd_sasl_local_domain = $mydomain
# smtp client tls level
smtp_tls_security_level = may
# smtp server tls level
smtpd_tls_security_level = may
# CA인증서
smtpd_tls_CAfile = /etc/letsencrypt/live/yalsooni.name/fullchain.pem
# 공개키
smtpd_tls_cert_file = /etc/letsencrypt/live/yalsooni.name/cert.pem
# 개인키
smtpd_tls_key_file = /etc/letsencrypt/live/yalsooni.name/privkey.pem

  위의 설정 파일은 Dovecot (pop3d, imapd)를 고려한 설정이다.  설정 복사 시 도메인, 인증서 등 필요 설정을 자신에 맞게 변경하면 된다.  변경해야 할 부분은 파란색으로 표시했다. 

  이후 master.cf 설정 파일을 열어 아래와 같이 주석과 설정 값을 변경한다.

root@mail:/etc/postfix# vi master.cf

Postfix 메일 발송 - Postfix meil balsong
master.cf 설정 파일

  위의 스크린숏처럼 빨간 네모 부분을 수정한다.

  POSTFIX 재시작에 앞서 /etc/aliases 파일을 빌드한다.  방법은 두 가지다. 

"run "postalias /etc/aliases" (or wherever your system stores the mail alias file), or simply run "newaliases" to build the necessary DBM or DB file." 

  postalias /etc/aliases 명령어를 실행하거나 newaliases 명령어를 실행하면 된다.

root@mail:/etc/postfix# postalias /etc/aliases

  POSTFIX를 재시작한다.

root@mail:/etc/postfix# /etc/init.d/postfix stop
 * Stopping Postfix Mail Transport Agent postfix                                                                       [ OK ] 

root@mail:/etc/postfix# /etc/init.d/postfix start
 * Starting Postfix Mail Transport Agent postfix                                                                       [ OK ] 
root@mail:/etc/postfix# 

  Dovecot을 활용한 수신 메일 서버를 구축하고 OS 계정을 추가하여 메일 테스트를 진행해 보겠다.

[홈서버] dovecot pop3 imap 메일 서버 구축

[홈서버] letsencrypt 인증서 발급 받기 메일 서버, 웹서버 운영을 보안 프로토콜로 사용하기 위해서는 허가받은 인증기관의 인증서를 사용하는 것이 좋다. 이런 인증기관의 인증서는 대부분 유료��

malchooni.name

Postfix 메일 발송 - Postfix meil balsong