Sending a mail from a linux shell script

后端 未结 11 2093
一个人的身影
一个人的身影 2020-12-04 06:52

I want to send an email from a Linux Shell script. What is the standard command to do this and do I need to set up any special server names?

11条回答
  •  感动是毒
    2020-12-04 07:17

    If both exim and ssmtp are running, you may enter into troubles. So if you just want to run a simple MTA, just to have a simple smtp client to send email notifications for insistance, you shall purge the eventually preinstalled MTA like exim or postfix first and reinstall ssmtp.

    Then it's quite straight forward, configuring only 2 files (revaliases and ssmtp.conf) - See ssmtp doc - , and usage in your bash or bourne script is like :

    #!/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 $?  
    

    Obviously do not forget to open your firewall output to the smtp port (25).

提交回复
热议问题