Sending a mail from a linux shell script

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

    SEND MAIL FROM LINUX TO GMAIL

    USING POSTFIX

    1: install software

    Debian and Ubuntu:

    apt-get update && apt-get install postfix mailutils
    

    OpenSUSE:

    zypper update && zypper install postfix mailx cyrus-sasl
    

    Fedora:

    dnf update && dnf install postfix mailx
    

    CentOS:

    yum update && yum install postfix mailx cyrus-sasl cyrus-sasl-plain
    

    Arch Linux:

    pacman -Sy postfix mailutils
    

    FreeBSD:

    portsnap fetch extract update
    
    cd /usr/ports/mail/postfix
    
    make config
    

    in configaration select SASL support

    make install clean
    
    pkg install mailx
    

    2. Configure Gmail

    /etc/postfix. Create or edit the password file:

    vim /etc/postfix/sasl_passwd
    

    i m using vim u can use any file editer like nano, cat .....

    >Ubuntu, Fedora, CentOS,Debian, OpenSUSE, Arch Linux:

    add this

    where user replace with your mailname and password is your gmail password

    [smtp.gmail.com]:587    user@gmail.com:password
    

    Save and close the file and Make it accessible only by root: becouse its an sensitive content which contains ur password

    chmod 600 /usr/local/etc/postfix/sasl_passwd
    

    >FreeBSD:

    directory /usr/local/etc/postfix.

    vim /usr/local/etc/postfix/sasl_passwd
    

    Add the line:

    [smtp.gmail.com]:587    user@gmail.com:password
    

    Save and Make it accessible only by root:

    chmod 600 /usr/local/etc/postfix/sasl_passwd
    

    3. Postfix configuration

    configuration file main.cf

    6 parameters we must set in the Postfix

    Ubuntu, Arch Linux,Debian:

    edit

     vim /etc/postfix/main.cf
    

    modify the following values:

    relayhost = [smtp.gmail.com]:587
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options =
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    

    smtp_sasl_security_options which in configuration will be set to empty, to ensure that no Gmail-incompatible security options are used.

    save and close

    as like for

    OpenSUSE:

    vim /etc/postfix/main.cf
    

    modify

    relayhost = [smtp.gmail.com]:587
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options =
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_tls_CAfile = /etc/ssl/ca-bundle.pem
    

    it also requires configuration of file master.cf

    modify:

    vim /etc/postfix/master.cf
    

    as by uncommenting this line(remove #)

    #tlsmgr unix - - n 1000? 1 tlsmg
    

    save and close

    Fedora, CentOS:

    vim /etc/postfix/main.cf
    

    modify

    relayhost = [smtp.gmail.com]:587
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options =
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
    

    FreeBSD:

    vim /usr/local/etc/postfix/main.cf
    

    modify:

    relayhost = [smtp.gmail.com]:587
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options =
    smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
    smtp_tls_CAfile = /etc/mail/certs/cacert.pem
    

    save and close this

    4. Process Password File:

    Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux,Debian:

    postmap /etc/postfix/sasl_passwd
    

    for freeBSD

    postmap /usr/local/etc/postfix/sasl_passwd
    

    4.1) Restart postfix

    Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux,Debian:

    systemctl restart postfix.service
    

    for FreeBSD:

    service postfix onestart
    nano /etc/rc.conf
    

    add

    postfix_enable=YES
    

    save then run to start

    service postfix start
    

    5. Enable "Less Secure Apps" In Gmail using help of below link

    https://support.google.com/accounts/answer/6010255

    6. Send A Test Email

    mail -s "subject" recever@domain.com
    

    press enter

    add body of mail as your wish press enter then press ctrl+d for proper termination

    if it not working check the all steps again and check if u enable "less secure app" in your gmail

    then restart postfix if u modify anything in that

    for shell script create the .sh file and add 6 step command as your requirement

    for example just for a sample

    #!/bin/bash
    REALVALUE=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
    THRESHOLD=80
    
    if [ "$REALVALUE" -gt "$THRESHOLD" ] ; then
        mail -s 'Disk Space Alert' mailid@domainname.com << EOF
    Your root partition remaining free space is critically low. Used: $REALVALUE%
    EOF
    fi
    

    The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD varialbe (80% here).

提交回复
热议问题