1
\$\begingroup\$

This is how I configure the utility SSMTP that allows me to send mail via smtp protocol through a Google-Gmail proxy instead I'll have to manually configure an "heavier" Postfix bi-directional email server on my environment. I configure it in my own server environment that only I uses.

#!/bin/bash

myHostName="$HOSTNAME"
read -sp "Please paste your Gmail proxy email address (due to pasting, no verfication needed): " gmail_proxy_email_address  && echo
read -sp "Please paste your Gmail proxy email password (due to pasting, no verfication needed):" gmail_proxy_email_password && echo

cat <<-EOF > /etc/ssmtp/ssmtp.conf
    root=${gmail_proxy_email_address}
    AuthUser=${gmail_proxy_email_address}
    AuthPass=${gmail_proxy_email_password}
    hostname=${myHostName}
    mailhub=smtp.gmail.com:587
    rewriteDomain=gmail.com
    FromLineOverride=YES
    UseTLS=YES
    UseSTARTTLS=YES
EOF

Would you do this even otherwise? Something to shorten? Some alternative utility maybe?

\$\endgroup\$
1
\$\begingroup\$

First, I recommend set -eu early in the script to abort on errors and to make any use of unset variables be an error.

Second, If HOSTNAME is unset, we probably want to fallback to running the hostname command. There seems little point in assigning it to a variable we use only once:

# (simplified to show only the relevant part)
cat <<EOF
    hostname=${HOSTNAME:-$(hostname)}
EOF

Then we can delete the line myHostName="$HOSTNAME", as it's no longer used.

Third, the other thing missing is umask 077 to ensure that the file isn't created readable by anyone else (important, as it contains cleartext credentials).


When reading email address, it shouldn't be necessary to hide it using read -s - only the password ought to be a secret.

When reading the password, there's no need to mention that confirmation is not required, as we wouldn't normally expect it here: we use confirmation when creating a password (so the user doesn't accidentally lock herself out of her account by a simple invisible typing error). Here, we're entering an existing password, and any error can be corrected just by re-running the script.


Edited script

Applying my suggestions, we get:

#!/bin/bash

set -eu

umask 077  # Ensure others can't read the file

read -p "Please paste your Gmail proxy email address: " \
     gmail_proxy_email_address
read -sp "Please paste your Gmail proxy email password:" \
     gmail_proxy_email_password && echo

cat <<-EOF > /etc/ssmtp/ssmtp.conf
    root=${gmail_proxy_email_address}
    AuthUser=${gmail_proxy_email_address}
    AuthPass=${gmail_proxy_email_password}
    hostname=${HOSTNAME:-$(hostname)}
    mailhub=smtp.gmail.com:587
    rewriteDomain=gmail.com
    FromLineOverride=YES
    UseTLS=YES
    UseSTARTTLS=YES
EOF
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.