작업실

우분투 20.04에서 구글 SMTP메일서버를 활용한 postfix 메일 보내기

컨텐츠 정보

  • 16,503 조회
  • 2 댓글
  • 0 추천
  • 목록

본문

우분투에는 메일서버를 단독으로 설치할 수 있지만 관리 및 스팸처리 등 손볼게 많습니다.


그래서 구글 계정을 이용한 SMTP 메일서버를 구축하면 별다른 고민없이 자신의 메일로 발송할 수 있게 됩니다.


특히 그누보드 회원가입시 사이트에서 메일이 오게 되는데 그 기능도 가능합니다.


다만 개인 메일로 했을시 귀찮아질 수 있으므로, 사이트 전용 이메일을 만들어서 쓰는 것도 좋은 방법입니다.



1. 패키지 설치



sudo apt install libsasl2-modules mailutils


위 명령어로 필요한 패키지를 설치해줍니다.



Internet Site가 선택되어 있습니다. 엔터를 누릅니다.



본인의 주소에 맞게 넣어줍니다. mail.example.com 이런 식으로요.


엔터를 누르면 자동으로 설치가 됩니다.



2. 구글 앱 비밀번호 생성하기


https://myaccount.google.com/security



위 링크에 가면 구글계정의 보안에 들어가게 되는데 2단계 인증을 하셔야 합니다.


2단계 인증이 완료되면 바로 밑에 있는 앱 비밀번호를 클릭합니다.


비밀번호 넣고 들어가면 




위와 같이 앱비밀번호를 생성할 앱 및 기기를 선택할 수 있습니다.


메일, 윈도우즈 컴퓨터를 선택하고 생성을 누릅니다.



qwer tyui asfg zxcv


위와 같이 총 16자리가 비번이 생깁니다. 띄어쓰기 빼고 연속된 16자리가 비밀번호가 됩니다.



3. Postfix 설정



sudo nano /etc/postfix/sasl/sasl_passwd


위 명령어를 넣고 비번을 넣어줍니다.



[smtp.gmail.com]:587 userid@gmail.com:apppassword


위와 같이 넣습니다. userid에 본인의 지메일 계정, apppassword에는 아까 만든 앱비밀번호 16자리를 넣으면 됩니다.


컨트롤 O, 엔터, 컨트롤 X로 저장 후 나옵니다.



sudo postmap /etc/postfix/sasl/sasl_passwd


위 명령어로 해시 db 파일을 생성합니다.


잘 되었다면 /etc/postfix/sasl/ 이 경로에 sasl_passwd.db 파일이 있을겁니다.


만든 파일들을 root 권한으로 바꿔줍니다. 



sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db


위 두 명령어로 됩니다.




4. Postfix 릴레이 서버 설정하기



sudo nano /etc/postfix/main.cf


위 명령어로 설정에 들어갑니다.



밑에서 6번째 줄에 보면 


relayhost =  가 있는데



relayhost = [smtp.gmail.com]:587 


위와 같이 바꿔줍니다.



그리고 제일 밑에



# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt


위 내용을 넣어줍니다. 컨트롤 O, 엔터, 컨트롤 X로 저장 후 나옵니다.



sudo systemctl restart postfix


위 명령어로 postfix를 재시작 해줍니다.



5. 테스트해보기



sudo echo "Test mail from postfix" | mail -s "Test Postfix" youremail@example.com


youremail@example.com 부분을 이메일을 받고 싶은 부분으로 수정하고 보내봅니다.


자신의 이메일에 확인해보면



위와 같이 성공한 것을 확인할 수 있습니다.



6. 그누보드 본인인증용 셋팅


이렇게 셋팅하면 SSH 상에서 메일 보내는 것도 가능하고, 워드프레스에서도 이메일 인증이 가능합니다.


그런데 유난히 그누보드에서는 본인인증용 메일이 안보내집니다. TLS 에러 때문인데요. letsencryt 인증서로 바꾸면 해결됩니다.



sudo nano /etc/postfix/main.cf


26번째 줄에 보면



# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key


위와 같은 내용이 있는데, 이 상태로 그누보드 메일 테스트를 하면 인증서 문제로 안보내집니다.



sudo tail -f /var/log/mail.log


위 명령어로 로그를 보면


Jun  4 01:29:57 aced-final postfix/smtpd[149601]: connect from localhost[127.0.0.1]

Jun  4 01:29:57 aced-final postfix/smtpd[149601]: SSL_accept error from localhost[127.0.0.1]: -1

Jun  4 01:29:57 aced-final postfix/smtpd[149601]: warning: TLS library problem: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca:../ssl/record/rec_layer_s3.c:1543:SSL alert number 48:

Jun  4 01:29:57 aced-final postfix/smtpd[149601]: lost connection after STARTTLS from localhost[127.0.0.1]

Jun  4 01:29:57 aced-final postfix/smtpd[149601]: disconnect from localhost[127.0.0.1] ehlo=1 starttls=0/1 commands=1/2

이렇게 TLS library problem 문제가 생깁니다. 따라서 letsencrypt 인증서로 바꿔주면 해결이 됩니다.



smtpd_tls_cert_file=/etc/letsencrypt/live/your.domain/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/your.domain/privkey.pem


위와 같이 your.domain 부분 수정 후 바꿔주면 됩니다.


그런데 그누보드에서는 PHPMailer/class.smtp.php 를 이용하는데, 렛츠인크립트 인증서를 넣으면 반드시 


stream_socket_enable_crypto(): Peer certificate CN=`your.domian' did not match expected CN=`127.0.0.1'

위와 같이 CN 미스매치가 뜹니다. 해결하는 방법은 그누보드 루트에 있는 config.php를 열어서 대략 173번째 줄에 있는



// SMTP
// lib/mailer.lib.php 에서 사용
//define('G5_SMTP',      '127.0.0.1');
define('G5_SMTP_PORT', '25');


위와 같이 SMTP 아이피를 주석처리하면 됩니다. 이거 해결방법 찾는다고 2시간 넘게 썼네요 ㅠㅠ


이제 구글 계정으로 편하게 그누보드 메일인증까지 가능하게 됩니다.




참조사이트


https://webdir.tistory.com/227

https://www.linode.com/docs/email/postfix/configure-postfix-to-send-mail-using-gmail-and-google-apps-on-debian-or-ubuntu/#configure-the-postfix-relay-server

관련자료

댓글 2 / 1 페이지

owen님의 댓글

안녕하세요. 만약 relayhost 작성 부분에 포트를 25로 지정하게 될경우 postfix 서버의 사용포트는 25번이 되는건가요?
전체 157 / 2 페이지
RSS

최근글


새댓글


알림 0