Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # You can set up sSMTP by setting the following ENV variables:
  4. #
  5. # SSMTP_TO - This is the address alarms will be delivered to.
  6. # SSMTP_SERVER - This is your SMTP server. Defaults to smtp.gmail.com.
  7. # SSMTP_PORT - This is the SMTP server port. Defaults to 587.
  8. # SSMTP_USER - This is your username for the SMTP server.
  9. # SSMTP_PASS - This is your password for the SMTP server. Use an app password if using Gmail.
  10. # SSMTP_TLS - Use TLS for the connection. Defaults to YES.
  11. # SSMTP_HOSTNAME - The hostname mail will come from. Defaults to localhost.
  12. #
  13. # ... and an addition of SSMTP_FROM, which generates the aliases in
  14. # the mail-out system, replacing USERNAME with the system username
  15. # who is sending mail (root, www-data, whatever,...) and HOSTNAME
  16. # to the effective `/etc/hostname` (env $HOSTNAME) value. More info
  17. # about this is written up below.
  18. #
  19. # TODO: supposedly, ssmtp is not developed anymore. Somebody
  20. # recommended `msmtp` as an alternative; ¯\_(ツ)_/¯
  21. #
  22.  
  23. if [[ $SSMTP_TO ]] && [[ $SSMTP_USER ]] && [[ $SSMTP_PASS ]]; then
  24.  
  25. # set reasonable defaults
  26.  
  27. SSMTP_TLS=${SSMTP_TLS:-YES}
  28. SSMTP_SERVER=${SSMTP_SERVER:-smtp.gmail.com}
  29. SSMTP_PORT=${SSMTP_PORT:-587}
  30. SSMTP_HOSTNAME=${SSMTP_HOSTNAME:-localhost}
  31. SSMTP_FROM=${SSMTP_FROM:-USERNAME@HOSTNAME}
  32.  
  33. # root=$SSMTP_TO
  34. cat << EOF > /etc/ssmtp/ssmtp.conf
  35. mailhub=$SSMTP_SERVER:$SSMTP_PORT
  36. AuthUser=$SSMTP_USER
  37. AuthPass=$SSMTP_PASS
  38. UseSTARTTLS=$SSMTP_TLS
  39. hostname=$SSMTP_HOSTNAME
  40. FromLineOverride=YES
  41. EOF
  42.  
  43. # Here's the catch of the day: we don't know which local users might
  44. # receive mail, and we need to forward them to something that might
  45. # pass firewall rules for example (valid email address) without
  46. # losing this information along the way.
  47.  
  48. USERS=$(cut -d: -f1 /etc/passwd)
  49.  
  50. CONFIG="# sSMTP aliases
  51. #
  52. # Format: local_account:outgoing_address:mailhub
  53. #
  54. # Example: root:your_login@your.domain:mailhub.your.domain[:port]
  55. # where [:port] is an optional port number that defaults to 25.
  56. #"
  57.  
  58. # obviously SSMTP_FROM can be USERNAME@HOSTNAME => root@[hostname]
  59. # but it can also be a valid GMAIL from address => acc@gmail.com,
  60. # or a valid gmail acc+alias => acc+USERNAME+HOSTNAME@gmail.com and
  61. # it *will* get delivered to acc@gmail.com keeping user/host info
  62.  
  63. for USERNAME in $USERS; do
  64. ALIAS=${SSMTP_FROM}
  65. ALIAS=${ALIAS/HOSTNAME/$HOSTNAME}
  66. ALIAS=${ALIAS/USERNAME/$USERNAME}
  67. ALIAS="$USERNAME:$ALIAS:$SSMTP_SERVER:$SSMTP_PORT"
  68. CONFIG="$CONFIG\n$ALIAS"
  69. done
  70.  
  71. echo -e "$CONFIG" > /etc/ssmtp/revaliases
  72.  
  73. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement