Sending a mail from a linux shell script

后端 未结 11 2105
一个人的身影
一个人的身影 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:24

    Another option for in a bash script:

    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
    
    • The file /tmp/mailtest is overwritten everytime this script is used.
    • The location of sendmail may differ per system.
    • When using this in a cron script, you have to use the absolute path for the sendmail command.

提交回复
热议问题