Variables in heredoc

I currently use this message to send e-mails in a script but I would also like to save the output of this code to a file as well while preserving the variables. What's the easiest way to accomplish this?

#Sending mail notification
when=`/bin/date`
/usr/sbin/sendmail -t >2 <<-EOM
Subject:User access disabled.
From:testuser@test.com
To:$EMAIL
User $USERNAME has been disabled.


Regards,
$variable Support.
EOM

You can put redirection and pipes after a here-doc so, something like

tee filename <<EOF | sendmail -t
...
...
EOF
1 Like

Works perfectly. Thank you for the timely response.