2 March 2019

When you set up Apache in vpn you also might want to send emails with PHP. You can use msmtp to send emails from the system using smtp. Eliminating the need to use some php library for smtp.

Get msmtp, which is a very simple and easy to use SMTP client with fairly complete sendmail compatibility. Use your favourite package manager to get msmtp package.
Source code viewer
  1. yay -S msmtp
Programming Language: Bash
Create a new configuration file for the smtp settings. There is no default config file in etc for msmtp.
Source code viewer
  1. # Default global settings.
  2. defaults
  3. # Use authentication SSL/TLS.
  4. auth on
  5. tls on
  6. tls_starttls off
  7. # Log errors to systemlog. Easiest way to log with log-rotation out of the box.
  8. syslog on
  9. # Set timeout for the email sending.
  10. timeout 5
  11.  
  12. # Setup your smtp "profile".
  13. account [profile-name]
  14. host [host-address]
  15. port [port]
  16. from [default-from-email-address]
  17. user [smtp-username]
  18. password [smtp-password]
  19.  
  20. # Set your smtp "profile" as default.
  21. account default : [profile-name]
Programming Language: INI
Set sendmail_path in /etc/php/php.ini so mail() funtion in PHP will work.
Source code viewer
  1. sendmail_path = /usr/bin/msmtp -C /path/to/msmtp.conf -t
Programming Language: INI
# This file has to be owned by apache user and permissions set to 600 or you get a security error from msmtp.
Source code viewer
  1. sudo chown www-data:users /dir/to/msmtp.conf
  2. sudo chmod 600 /dir/to/msmtp.conf
Programming Language: Bash
Test your work and see if everything works.
Source code viewer
  1. echo -e "Subject: Test email" | /usr/bin/msmtp -v -C /dir/to/msmtp.conf sent-to@example.com
Programming Language: Bash