How to setup correctly Exim4

First install the Exim4 package on your linux distribution :

sudo apt-get -y install exim4

Run

dpkg-reconfigure exim4-config

Then configure Exim 4 :

Mail sent by smarthost; received via SMTP or fetchmail
– Type System Mail Name: yourdomainname.com
– Type IP Adresses to listen on for incoming SMTP connections: 127.0.0.1
– Leave Other destinations for which mail is accepted blank : yourdomainname.com
– Leave Machines to relay mail for: blank
-Type Machine handling outgoing mail for this host (smarthost): smtp.gmail.com::587
– hide local mail name in outgoing mail : NO
– Keep number of DNS-queries minimal (Dial-on-Demand) : NO
Mbox
– Split configuration into small files : NO

After that :

nano /etc/exim4/exim4.conf.template

Find the line .ifdef DCconfig_smarthost DCconfig_satellite and add this just after :

send_via_gmail:
driver = manualroute
domains = ! +local_domains
transport = gmail_smtp
route_list = * smtp.gmail.com

Comment out the next domains = ! +local_domains, i.e. smarthost :

    #smarthost:
    #  debug_print = "R: smarthost for $local_part@$domain"
    #  driver = manualroute
    #  domains = ! +local_domains
    #  transport = remote_smtp_smarthost
    #  route_list = * DCsmarthost byname
    #  host_find_failed = defer
    #  same_domain_copy_routing = yes
    #  no_more

Find begin authenticators, and after this add :

gmail_login:
driver = plaintext
public_name = LOGIN
client_send = : yourname@gmail.com : YourGmailPassword

You have to set of course your gmail email address and to add your gmail email account password.

Find transport/30_exim4-config_remote_smtp_smarthost, and after that add :

gmail_smtp:
driver = smtp
port = 587
hosts_require_auth = $host_address
hosts_require_tls = $host_address

Comment out the end of the file :

#.ifndef AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS
    #  client_send = "<; ${if !eq{$tls_cipher}{}\
    #                    {^${extract{1}{:}{PASSWDLINE}}\
    #        ^${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}\
    #      }fail}"
    #.else
    #  client_send = "<; ^${extract{1}{:}{PASSWDLINE}}\
    #       ^${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}"
    #.endif

    #login:
    #  driver = plaintext
    #  public_name = LOGIN

    #.ifndef AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS
      # Return empty string if not non-TLS AND looking up $host in passwd-file
      # yields a non-empty string; fail otherwise.
    #  client_send = "<; ${if and{\
    #                          {!eq{$tls_cipher}{}}\
    #                          {!eq{PASSWDLINE}{}}\
    #                         }\
    #                      {}fail}\
    #                 ; ${extract{1}{::}{PASSWDLINE}}\
    #    ; ${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}"
    #.else
      # Return empty string if looking up $host in passwd-file yields a
      # non-empty string; fail otherwise.
    #  client_send = "<; ${if !eq{PASSWDLINE}{}\
    #                      {}fail}\
    #                 ; ${extract{1}{::}{PASSWDLINE}}\
    #    ; ${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}"
    #.endif
    #####################################################
    ### end auth/30_exim4-config_examples
    #####################################################

Run :

update-exim4.conf

And finally :

/etc/init.d/exim4 restart

Or

Service exim4 restart