Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #!/bin/bash
  2. # author: Timo Stankowitz <timo.stankowitz@gmail.com>
  3. # create date: 2016-08-15
  4. # last change: 2016-08-15
  5.  
  6. # Dieses Script konfiguriert Postfix so, dass mailgun (oder andere) als e-mail relay konfiguriert wird
  7. # somit werden e-mails รผber mailgun, und nicht direkt von Postfix gesendet.
  8. # Dies hat den Vorteil, dass die e-mails nicht im Spam Ordern, oder รผberhaupt nicht beim client landen.
  9.  
  10. postfixMainConfig="/etc/postfix/main.cf"
  11. postfixSaslPasswdFile="/etc/postfix/sasl_passwd"
  12.  
  13. fqdn=$(hostname).example.de
  14. relayhost="smtp.mailgun.org"
  15. relayhostPort=587
  16. smtpUserName="username@example.com"
  17. smtpUserPassword=""
  18.  
  19. # first check if the user is root
  20. if [ "$(id -u)" != 0 ]; then
  21. echo "Please execute this script as root"
  22. exit 1
  23. fi
  24.  
  25. # check if postfix is installed on the system
  26. if [ ! -f $postfixMainConfig ]; then
  27. printf "Postfix is not installed. Do you wish to install postfix now? (Y/n): "
  28. read
  29.  
  30. case $REPLY in
  31. yes|Yes|y|Y|j|J|'')
  32. echo "Installing Postfix now"
  33. echo "Please select 'Internet Site' for the postfix configuration"
  34. echo "and after that press enter for the hostname"
  35. sleep 5
  36.  
  37. # check if apt-get is installed on the system
  38. which apt-get > /dev/null 2>&1
  39. checkAptGetExists=$?
  40. if [ "$checkAptGetExists" != 0 ]; then
  41. echo "ERROR: apt-get is not installed on your system."
  42. printf "Please install apt-get on your system. \nEXIT"
  43. exit 1
  44. fi
  45.  
  46. apt-get update
  47. apt-get install postfix -y
  48. ;;
  49.  
  50. n|N|no)
  51. echo "Please install postfix manually."
  52. exit 1
  53. ;;
  54. esac
  55. fi
  56.  
  57. echo
  58. printf "Please Enter your FQDN [$fqdn]: "
  59. read
  60.  
  61. if [ "$REPLY" != '' ]; then
  62. fqdn=$REPLY
  63. fi
  64.  
  65. echo "set FQDN to $fqdn"
  66. echo
  67. printf "Please set your relayhost [$relayhost]: "
  68. read
  69.  
  70. if [ "$REPLY" != '' ]; then
  71. relayhost=$REPLY
  72. echo "set relayhost to $relayhost"
  73. echo
  74. printf "Please set to port of the relayhost [$relayhost] : "
  75. read
  76.  
  77. if [ "$REPLY" != '' ]; then
  78. relayhostPort=$REPLY
  79. echo "set port to $relayhostPort"
  80. fi
  81. fi
  82.  
  83. echo "set relayhost to $relayhost"
  84. echo
  85. printf "Please set the smtp username [$smtpUserName]: "
  86. read smtpUserName
  87. echo "smtp username set to $smtpUserName"
  88. echo
  89. printf "Please set the smtp user password (will be displayed in plaintext): "
  90. read smtpUserPassword
  91. echo "smtp user password set to $smtpUserPassword"
  92.  
  93. # checking configuration
  94. echo
  95. echo "here is your configuration: "
  96. echo
  97. echo "FQDN: $fqdn"
  98. echo "smtp relay host: $relayhost"
  99. echo "smpt relay host port: $relayhostPort"
  100. echo "smtp username: $smtpUserName"
  101. echo "smpt user password: $smtpUserPassword"
  102. echo
  103. printf "is everything fine? (Y/n): "
  104. read
  105.  
  106. case $REPLY in
  107. yes|Yes|y|Y|j|J|'')
  108. ;;
  109.  
  110. n|N|no)
  111. "oh shit... sorry... try again... EXIT"
  112. exit 1
  113. ;;
  114. esac
  115.  
  116. # create a backup of the existing postfix conf file
  117. echo "creating a backup of the orig config file"
  118. cp $postfixMainConfig $postfixMainConfig.orig
  119.  
  120. # working on the postfix config file
  121. # deleting those entries
  122. sed -i '/appending .domain is the/d' $postfixMainConfig
  123. sed -i '/append_dot_mydomain/d' $postfixMainConfig
  124. sed -i '/relayhost/d' $postfixMainConfig
  125. sed -i '/myhostname/d' $postfixMainConfig
  126. sed -i '/mydestination/d' $postfixMainConfig
  127.  
  128. # append to the config file
  129. echo >> $postfixMainConfig
  130. echo "# custom stuff" >> $postfixMainConfig
  131. echo "append_dot_mydomain = yes" >> $postfixMainConfig
  132. echo >> $postfixMainConfig
  133. echo "myhostname = $hostname" >> $postfixMainConfig
  134. echo "mydestination = $hostname, localhost.localdomain, localhost" >> $postfixMainConfig
  135. echo >> $postfixMainConfig
  136. echo "relayhost = [$relayhost]:$relayhostPort" >> $postfixMainConfig
  137. echo "smtp_sasl_auth_enable = yes" >> $postfixMainConfig
  138. echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> $postfixMainConfig
  139. echo "smtp_sasl_security_options = noanonymous" >> $postfixMainConfig
  140. echo "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" >> $postfixMainConfig
  141. echo "smtp_use_tls = yes" >> $postfixMainConfig
  142.  
  143. echo "[$relayhost]:$relayhostPort $smtpUserName:$smtpUserPassword" > $postfixSaslPasswdFile
  144. postmap $postfixSaslPasswdFile
  145.  
  146. # change permission
  147. chmod 600 /etc/postfix/sasl_passwd*
  148.  
  149. echo
  150. echo "Configuration is now done. Restarting postfix now..."
  151. sleep 2
  152. /etc/init.d/postfix restart
  153.  
  154. # check if mailutils are installed
  155. which mail > /dev/null 2>&1
  156. checkMailInstalled=$?
  157. if [ "$checkMailInstalled" != 0 ]; then
  158. echo
  159. printf "mail tools are not installed on your system. Do you wan't to install the tools now? (Y/n): "
  160. read
  161.  
  162. case $REPLY in
  163. yes|Yes|y|Y|j|J|'')
  164. apt-get install mailutils -y
  165. ;;
  166.  
  167. n|N|no)
  168. echo "okay - please install the tools manualy and send a test mail."
  169. ;;
  170. esac
  171. fi
  172.  
  173. echo
  174. echo "DONE. now you can test the configuration by sending a testmail."
  175. echo "echo "this is just a test" | mail -s "[test] just a test" foobar@example.com"
  176. echo
  177. echo "If you receve any error check /var/log/syslog. "
  178.  
  179. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement