shellmailx

shell script - Sending mails to users without disclosing email addresses


I am trying to send mail using mailx in my shell script to users without disclosing email addresses.

This is my piece of code -

query1=$(sqlplus -s ${ORA_UID_PSWD} << 'EOF'
set heading OFF
SELECT cu.cntct_email
        FROM cm_user cu, cm_usertype ct
        WHERE trunc(cu.xprtn_dt) = trunc(sysdate) - 60
        AND cu.cm_user_id=ct.cm_user_id
        AND ct.user_type = 'E'
        AND cu.cntct_email is not null;
EOF
)
user_list1=$(echo "$query1" | tr '\n' ',' | sed 's:^.\(.*\).$:\1:')
echo $user_list1
echo -e "Hi,\nFYI.. Your password is expired 60 days ago. Please login and get it reset.\n\nThanks" |mailx -s "Password expired" -b $user_list1

I tried using -b option(BCC), but I am getting error as -

Send options without primary recipient specified.

Usage:

mailx -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE
      -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users

Anybody know how can I send without using To(Primary reciepnt)


Solution

  • If mailx doesn't do what you want, you can talk directly to sendmail.

    # Speculative; see below
    PATH=/usr/libexec:$PATH
    
    query1=$(sqlplus -s "$ORA_UID_PSWD" << 'EOF'
    set heading OFF
    SELECT cu.cntct_email
            FROM cm_user cu, cm_usertype ct
            WHERE trunc(cu.xprtn_dt) = trunc(sysdate) - 60
            AND cu.cm_user_id=ct.cm_user_id
            AND ct.user_type = 'E'
            AND cu.cntct_email is not null;
    EOF
    )
    # no need for further normalization of $query1 actually
    sendmail -oi $query1 <<'EOF'
    Subject: Password expired
    To: undisclosed-recipients:;
    
    Hi,
    FYI... Your password has expired 60 days ago.
    Please login and get it reset.
    
    Thanks
    EOF
    

    If sendmail is not in your PATH, probably update your PATH (perhaps just within this script) so it is. Common locations include /usr/sbin, /usr/libexec, etc; but consult your platform's documentation for better guesses if these don't help.

    There is no need for an explicit Bcc: header; Sendmail in fact ignores the headers when you specify the recipients on the command line.