Bradox
Bradox

Reputation: 109

Configure postfix to use external smtp server

I'm trying to configure postfix to use an external smtp server. I have managed to configure it to send emails from root@host-name, but I would like to be able to send it from my domain. I followed this tutorial and also added smtp_generic_maps, but it is not working.
This is my configuration:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_security_level=may
smtpd_tls_protocols = !SSLv2, !SSLv3
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = aba-elearning.com
#ABA.Moodle
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
milter_protocol = 2
milter_default_action = accept
smtpd_milters = inet:localhost:12301
non_smtpd_milters = inet:localhost:12301




# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

smtp_generic_maps = hash:/etc/postfix/generic

Upvotes: 1

Views: 2052

Answers (1)

Gryu
Gryu

Reputation: 2189

External SMTP Server configuration

  1. Go to sendgrid and register a profile
  2. Go to Sender Authentication and create a new sender (Picture)
    Specify some not-free email (I used office 365 Online account), so Sender could be verified by SendGrid.
  3. Check your mailbox and click Verify Single Sender button in the letter (Picture)
  4. Go to SMTP Relay integration page and create an apikey, you will put in postfix's sasl_passwd file (Picture). Do not close a verification page till you insert it there and verify.

Postfix configuration:

Install some packages and configure postfix:

sudo apt install postfix mailutils

Configure /etc/postfix/main.cf file the next way, adding or modifying the next lines:

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
compatibility_level = 2
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = localhost.localdomain

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = test
mydestination = $myhostname, localhost
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous

Create /etc/postfix/sasl_passwd file (api secret key would be gained further):

[smtp.sendgrid.net]:587 apikey:SG.kjaksdjkfajskdk_ASDk.lkjaoIO_Kjkoaofs3i99asfd_kkjasdof99882348IKII

Apply settings:

sudo postmap /etc/postfix/sasl_passwd
sudo systemctl restart postfix

Test email sending during SendGrid's verification

  1. Click Next Verify Integration button. You'll be on Let's test your integration page.

  2. Click the Verify Integration button

  3. Run a different terminal window to see logs, using journalctl -f command

  4. Run a command in a terminal to test mail sending, using Single Sender after -r parameter created in External SMTP Server configuration section.

     echo "Test Email message body" | mail -r [email protected] -s "Email test subject" [email protected]
    

As a result, my gmail.com mailbox received a test letter. I've also sent it to my yahoo.com mail address and it also have been placed into a Spam folder after some time of deferring. It is better to not use this method with yahoo at all, because of it is not reliable. Important notification will not be retrieved for "decades".

Configure /usr/local/nagios/etc/objects/commands.cfg the next way:

define command {
    command_name    notify-service-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -r [email protected] -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

Here I've replaced /usr/sbin/sendmail by /usr/bin/mail -r [email protected]

Upvotes: 1

Related Questions