프로그래밍/Linux

shell script로 이메일 보내기

kugancity 2015. 12. 26. 10:57
반응형



뉴스 업데이트 결과를 메일로 보내는 쉘 스크립트를 작성하였다. 

스크립트를 실행하기 전에 mail 프로그램이 깔려있어야 한다. 



#!/bin/bash

subject="today update summary"

email="user@naver.com"

filename="/tmp/message.txt"

updated_cnt=`grep -c "there is new" /home/log/sort_date.log`

echo "updated news count:${updated_cnt}" > ${filename}


#메일 발송하기

mail -s "$subject" "$email" < $filename





참고로 메일설치 삽질과정도 기록해둔다. 


apt-get install mailutils



위 명령어를 입력하면 아래 설정창이 나온다. 

이 중에서 하나를 선택해야 한다. 


SMTP를 사용해서 메일을 보내고 받으려고 하면 

Internet Site   를 선택하면 된다. 







기존 설정이 없던 상태에서 No configuration 을 선택했더니 아래와 같은 에러가 발생하였다. 

참고로 /var/log/mail.err 에서 메일 관련 에러 메세지를 확인할 수 있다. 





#echo "Test" | mail -s "Test" username@naver.com

mail: cannot send message: Process exited with a non-zero status



/var/log# ll *mail*

-rw-r----- 1 syslog adm 214 12월 25 16:33 mail.err

-rw-r----- 1 syslog adm 214 12월 25 16:33 mail.log



vi mail.err

postfix/sendmail[11425]: fatal: open /etc/postfix/main.cf: No such file or directory

postfix/sendmail[11453]: fatal: open /etc/postfix/main.cf: No such file or directory






검색해보니 메일 보내기 설정이 제대로 되어있지 않아서 그런것이라고 해서 

sudo dpkg-reconfigure postfix

로 다시 위 설정창을 띄운 후 Internet site로 설정을 변경했다. 


참고: http://jonsview.com/how-to-setup-email-services-on-ubuntu-using-postfix-tlssasl-and-dovecot



General type of mail configuration? Internet Site
System mail name? server1.example.com
Root and postmaster mail recipient? Leave blank
Other destinations to accept mail for? server1.example.com, example.com, localhost.example.com, localhost
Force synchronous updates on mail queue? No
Local networks? Leave default (127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128)
Mailbox size limit (bytes)? 0
Local address extension character? Leave default (+)
Internet protocols to use? ipv4 (most likely)



설정 변경후 정상적으로 메일 프로그램이 작동하는 것을 확인하였다. 




728x90
반응형