setup-msmtp-for-gmail.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/sh
  2. # Sending emails using Gmail and msmtp
  3. # Author: [Josef Jezek](http://about.me/josefjezek)
  4. # Donate: [Gittip](https://www.gittip.com/josefjezek)
  5. # Link: [Gist](https://gist.github.com/6194563)
  6. # Usage: setup-msmtp-for-gmail.sh
  7. sudo apt-get update -q
  8. sudo apt-get install msmtp-mta ca-certificates heirloom-mailx -yq
  9. if command -v zenity >/dev/null; then
  10. GMAIL_USER=$(zenity --entry --title="Gmail username" --text="Enter your gmail username with domain (username@gmail.com):")
  11. GMAIL_PASS=$(zenity --entry --title="Gmail password" --text="Enter your gmail password:" --hide-text)
  12. else
  13. read -p "Gmail username with domain (username@gmail.com): " GMAIL_USER
  14. read -p "Gmail password: " GMAIL_PASS
  15. fi
  16. echo # an empty line
  17. if [ -z "$GMAIL_USER" ]; then echo "No gmail username given. Exiting."; exit -1; fi
  18. if [ -z "$GMAIL_PASS" ]; then echo "No gmail password given. Exiting."; exit -1; fi
  19. sudo tee /etc/msmtprc >/dev/null <<__EOF
  20. # Accounts will inherit settings from this section
  21. defaults
  22. auth on
  23. tls on
  24. tls_certcheck off
  25. # tls_trust_file /etc/ssl/certs/ca-certificates.crt
  26. logfile /var/log/msmtp.log
  27. # A first gmail address
  28. account gmail
  29. host smtp.gmail.com
  30. port 587
  31. from $GMAIL_USER
  32. user $GMAIL_USER
  33. password $GMAIL_PASS
  34. # A second gmail address
  35. account gmail2 : gmail
  36. from username@gmail.com
  37. user username@gmail.com
  38. password password
  39. # A freemail service
  40. account freemail
  41. host smtp.freemail.example
  42. from joe_smith@freemail.example
  43. user joe.smith
  44. password secret
  45. # A provider's service
  46. account provider
  47. host smtp.provider.example
  48. # Set a default account
  49. account default : gmail
  50. __EOF
  51. sudo chmod 600 /etc/msmtprc
  52. sudo chown -R www-data:www-data /etc/msmtprc
  53. HOST=$(hostname)
  54. sudo mail -vs "Email relaying configured at ${HOST}" $GMAIL_USER <<__EOF
  55. The postfix service has been configured at host '${HOST}'.
  56. Thank you for using this postfix configuration script.
  57. __EOF
  58. echo "I have sent you a mail to $GMAIL_USER"
  59. echo "This will confirm that the configuration is good."
  60. echo "Please check your inbox at gmail."