[Linux]쉽게 따라하는 Linux 서버에서 메일 보내기(postfix 사용)
리눅스 서버에서 postfix 패키지를 이용해서 메일 보내기 기능을 설정해 보겠습니다. 메일 보내기 기능을 설치해 놓으면 Daily Health Check Report 등을 이메일로 보낼 수 있으며, crontab과 연결해서 예약 메일 보내기 기능도 구현할 수 있습니다. 여기는 naver 메일의 smtp 서버(smtp.naver.com)과 포트(465)를 이용하겠습니다. 네이버 메일 > 환경설정 > POP3/SMTP 사용함으로 저장해야 합니다.
- 메일 서비스 관련 패키지 설치 및 확인
- 메일 서버 환경 설정 및 확인
- 메일 보내기 테스트
- 예약 메일 보내기
1. 메일 서버시 관련 패키지 설치 및 데몬 실행
- postfix, mailx, cyrus-sasl-plain 패키지를 설치합니다.
[root@centos8 ~]# yum -y install postfix mailx cyrus-sasl-plain Last metadata expiration check: 0:01:33 ago on Wed 26 Jul 2023 02:39:35 AM KST. Package postfix-2:3.5.8-6.el8.x86_64 is already installed. Package mailx-12.5-29.el8.x86_64 is already installed. Package cyrus-sasl-plain-2.1.27-6.el8_5.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete!
- `rpm -ql postfix` 명령어로 패키지에서 설치한 파일들을 확인합니다.
/etc/postfix/ 디렉토리에 환경설정 파일이 있으며, postfix.service 데몬을 실행시켜야 합니다.[root@centos8 ~]# systemctl enable --now postfix.service Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service /usr/lib/systemd/system/postfix.service.
2. 메일 서버 환경 설정 및 확인
- /etc/postfix/main.cf이 환경설정 파일입니다. 아래에 있는 지시자들을 등록해야 합니다.
붉은 색 지시자만 수정 또는 추가 등록하면 됩니다. 주석처리(#)된 지시자는 주석처리를 삭제하세요.
설정 완료 후 `postconf -n` 명령어로 점검하면 잘못되었거나 누락된 설정사항을 확인할 수 있습니다.alias_database = hash:/etc/aliases alias_maps = hash:/etc/aliases command_directory = /usr/sbin compatibility_level = 2 daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix debug_peer_level = 2 debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5 html_directory = no inet_interfaces = loopback-only inet_protocols = all mail_owner = postfix mailq_path = /usr/bin/mailq.postfix manpage_directory = /usr/share/man meta_directory = /etc/postfix mydestination = $myhostname, localhost.$mydomain, localhost mydomain = centos8 myhostname = centos8 myorigin = $mydomain newaliases_path = /usr/bin/newaliases.postfix queue_directory = /var/spool/postfix readme_directory = /usr/share/doc/postfix/README_FILES relayhost = [smtp.naver.com]:465 sample_directory = /usr/share/doc/postfix/samples sendmail_path = /usr/sbin/sendmail.postfix setgid_group = postdrop shlib_directory = /usr/lib64/postfix smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt smtp_tls_CApath = /etc/pki/tls/certs smtp_tls_security_level = encrypt smtp_tls_wrappermode = yes smtp_use_tls = yes smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem smtpd_tls_key_file = /etc/pki/tls/private/postfix.key smtpd_tls_security_level = encrypt unknown_local_recipient_reject_code = 550
- SMTP 서버의 사용자 등록 및 해시 파일 생성
[root@centos8 postfix]# vi sasl_passwd [smtp.naver.com]:465 your_id@naver.com:your_passwd [root@centos8 postfix]# postmap ./sasl_passwd [root@centos8 postfix]# chown root:root ./sasl_passwd* [root@centos8 postfix]# chmod 0600 ./sasl_passwd* [root@centos8 postfix]# ll | grep sasl_passwd -rw-------. 1 root root 51 Jul 26 03:13 sasl_passwd -rw-------. 1 root root 12288 Jul 26 03:15 sasl_passwd.db
3. 메일 보내기 테스트
- postfix 데몬 재시작 및 상태 확인
[root@centos8 postfix]# systemctl restart postfix [root@centos8 postfix]# systemctl status postfix [0m postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2023-07-26 03:20:03 KST; 36s ago
- 이메일 보내기 테스트 및 확인
[root@centos8 postfix]# echo "This is the body of the email" | mail -s "This is the subject line" -r your_id@naver.com receiver_email [root@centos8 postfix]# mailq Mail queue is empty
- 메일이 전송되지 않는다면 전송오류로 쌓인 메일큐를 삭제, 설정 재확인 후 postfix 데몬 재시작
[root@centos8 postfix]# mailq | awk '$7~/@/{print$1}' | while read qid; do postsuper -d $qid; done [root@centos8 postfix]# vi main.cf [root@centos8 postfix]# systemctl restart postfix.service
4. 예약 메일 보내기
- crontab 명령어를 사용해서 보낼 메일을 등록해 놓는다
[root@centos8 postfix]# vi /root/sendmail.sh TITLE="subject" FR_ADDR="your_id@naver.com" TO_ADDR="email1 email2" ATTACH="/path/attachment_file" BODY="/path/content_file" mail -s ${TITLE} -a ${ATTACH} -r ${FR_ADDR} ${TO_ADDR} < ${BODY} [root@centos8 postfix]# chmod 755 /root/sendmail.sh [root@centos8 postfix]# crontab -e 분 시 일 월 주 /root/sendmail.sh