리눅스 쉘 스크립트에서 메일 보내기
Linux Shell 스크립트에서 이메일을 보내려고합니다. 이를 수행하기위한 표준 명령은 무엇이며 특수 서버 이름을 설정해야합니까?
서버가 제대로 구성되어있는 경우 (예 : MTA가 시작되어 실행중인 경우) mail 명령 만 사용할 수 있습니다.
예를 들어 파일의 내용을 보내려면 다음을 수행하십시오.
$ cat /path/to/file | mail -s "your subject" your@email.com
man mail
상세 사항은.
당신이 bash는 깨끗하고 간단한 방법을 원하고, 당신이 사용하지 않을 경우 cat
, echo
등, 가장 간단한 방법은 다음과 같습니다
mail -s "subject here" email@address.com <<< "message"
<<<
표준 입력을 리디렉션하는 데 사용됩니다. 오랫동안 배쉬의 일부였습니다.
exim과 ssmtp가 모두 실행 중이면 문제가 발생할 수 있습니다. 따라서 간단한 MTA를 실행하려는 경우 간단한 smtp 클라이언트가 전자 메일 알림을 보내도록하려면, exim 또는 postfix 와 같이 미리 설치된 MTA를 먼저 제거하고 ssmtp를 다시 설치해야합니다.
그런 다음 2 개의 파일 (revaliases 및 ssmtp.conf) 만 구성하면 매우 간단합니다. ssmtp doc 참조-bash 또는 bourne 스크립트의 사용법은 다음과 같습니다.
#!/bin/sh
SUBJECT=$1
RECEIVER=$2
TEXT=$3
SERVER_NAME=$HOSTNAME
SENDER=$(whoami)
USER="noreply"
[[ -z $1 ]] && SUBJECT="Notification from $SENDER on server $SERVER_NAME"
[[ -z $2 ]] && RECEIVER="another_configured_email_address"
[[ -z $3 ]] && TEXT="no text content"
MAIL_TXT="Subject: $SUBJECT\nFrom: $SENDER\nTo: $RECEIVER\n\n$TEXT"
echo -e $MAIL_TXT | sendmail -t
exit $?
방화벽 출력을 smtp 포트 (25)로 여는 것을 잊지 마십시오.
bash 스크립트의 또 다른 옵션은 다음과 같습니다.
mailbody="Testmail via bash script"
echo "From: info@myserver.test" > /tmp/mailtest
echo "To: john@mywebsite.test" >> /tmp/mailtest
echo "Subject: Mailtest subject" >> /tmp/mailtest
echo "" >> /tmp/mailtest
echo $mailbody >> /tmp/mailtest
cat /tmp/mailtest | /usr/sbin/sendmail -t
/tmp/mailtest
이 스크립트를 사용할 때마다 파일 을 덮어 씁니다.- sendmail의 위치는 시스템마다 다를 수 있습니다.
- cron 스크립트에서 이것을 사용할 때는 sendmail 명령에 절대 경로를 사용해야합니다.
일반적으로 mail
명령을 사용하여 로컬 MTA를 사용하여 메시지를 보내려고합니다 (SMTP를 사용하여 메시지를 대상으로 전달하거나 ISP 등의보다 강력한 SMTP 서버로 전달). 로컬 MTA가없는 경우 (UNIX 계열 시스템에서 생략하는 것이 조금 이상하지만) ssmtp 와 같은 최소한의 MTA를 사용할 수 있습니다 .
ssmtp
구성하기가 매우 쉽습니다. 기본적으로 공급자의 SMTP 서버 위치를 지정하면됩니다.
# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and you mailhub is so named.
mailhub=mail
다른 옵션은 SMTP 서버에 직접 연결하여 Smtp-Auth-Email-Script , smtp-cli , SendEmail 등과 같은 메시지를 게시하는 무수한 스크립트 중 하나를 사용하는 것입니다 .
일부 SMTP 서버를 사용하려면 다음을 수행하십시오.
export SUBJECT=some_subject
export smtp=somehost:someport
export EMAIL=someaccount@somedomain
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
Change somehost
, someport
, and someaccount@somedomain
to actual values that you would use. No encryption and no authentication is performed in this example.
You don't even need an MTA. The SMTP protocol is simple enough to directly write it to your SMTP server. You can even communicate over SSL/TLS if you have the OpenSSL package installed. Check this post: https://33hops.com/send-email-from-bash-shell.html
The above is an example on how to send text/html e-mails that will work out of the box. If you want to add attachments the thing can get a bit more complicated, you will need to base64 encode the binary files and embed them between boundaries. THis is a good place to start investigating: http://forums.codeguru.com/showthread.php?418377-Send-Email-w-attachments-using-SMTP
On linux, mail utility can be used to send attachment with option "-a". Go through man pages to read about the option. For eg following code will send an attachment :
mail -s "THIS IS SUBJECT" -a attachment.txt name@domain.com <<< "Hi Buddy, Please find failure reports."
The mail
command does that (who would have guessed ;-). Open your shell and enter man mail
to get the manual page for the mail
command for all the options available.
you can use 'email' or 'emailx' command.
(1) $ vim /etc/mail.rc # or # vim /etc/nail.rc
set from = xxx@xxx.com #
set smtp = smtp.exmail.gmail.com #gmail's smtp server
set smtp-auth-user = xxx@xxx.com #sender's email address
set smtp-auth-password = xxxxxxx #get from gmail, not your email account passwd
set smtp-auth=login
Because if it is not sent from an authorized account, email will get to junk mail list.
(2) $ echo "Pls remember to remove unused ons topics!" | mail -s "waste topics" -a a.txt developer@xxx.com #send to group user 'developer@xxxx.com'
참고URL : https://stackoverflow.com/questions/5155923/sending-a-mail-from-a-linux-shell-script
'Programing' 카테고리의 다른 글
C #의 System.Threading.Timer가 작동하지 않는 것 같습니다. (0) | 2020.08.05 |
---|---|
Android 디자인 라이브러리-부동 작업 버튼 패딩 / 여백 문제 (0) | 2020.08.05 |
sklearn에서 가져 오기에서 ImportError : 이름 check_build를 가져올 수 없습니다 (0) | 2020.08.05 |
printf ()를 사용하여 인쇄 할 문자열의 문자 수를 지정하는 방법이 있습니까? (0) | 2020.08.05 |
UIView에서 xib 파일을로드하는 방법 (0) | 2020.08.05 |