Pejman Moghadam / Scripts

Bash script - Sending mail from command line

Public Domain


Using smtp-auth

#!/bin/bash
TMP=$(mktemp)
FROM_EMAIL_ADDRESS="me@test.com"
TO_EMAIL_ADDRESS="you@example.net"
EMAIL_SUBJECT="This is email subject"
SMTP="192.168.1.10"
FRIENDLY_NAME="$(hostname)"
EMAIL_ACCOUNT_PASSWORD="123456"

cat > $TMP << EOF
This is email body
This is email body
This is email body
EOF

cat $TMP | \
        mailx -v -s "${EMAIL_SUBJECT}" \
        -S smtp-auth=cram-md5 \
        -S smtp=${SMTP} \
        -S from="${FROM_EMAIL_ADDRESS}(${FRIENDLY_NAME})" \
        -S smtp-auth-user=$FROM_EMAIL_ADDRESS \
        -S smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD \
        -S nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/ \
        $TO_EMAIL_ADDRESS

[ -e $TMP ] && rm $TMP

Without authentication

#!/bin/bash
TMP=$(mktemp)
FROM_EMAIL_ADDRESS="me@test.com"
TO_EMAIL_ADDRESS="you@example.net"
EMAIL_SUBJECT="This is email subject"
SMTP="192.168.1.10"
FRIENDLY_NAME="$(hostname)"

cat > $TMP << EOF
This is email body
This is email body
This is email body
EOF

cat $TMP | \
        mailx -v -s "${EMAIL_SUBJECT}" \
        -S smtp=${SMTP} \
        -S from="${FROM_EMAIL_ADDRESS}(${FRIENDLY_NAME})" \
        $TO_EMAIL_ADDRESS

[ -e $TMP ] && rm $TMP

BY: Pejman Moghadam
TAG: mailx, smtp, email, bash
DATE: 2014-01-26 16:53:47


Pejman Moghadam / Scripts [ TXT ]