This is a three in one tutorial which combines how to use MSMTP to send mails via Gmail and Yahoo servers and how to use MSMTP with PHP Mail() function instead of the default sendmail.
Installing msmtp
To install msmtp on Red Hat/CentOS/Fedora type of distributions
yum install msmtp
To install msmtp on Debian/Ubuntu type of distributions
apt-get install msmtp
Configuring msmtp with Gmail and Yahoo
Create or edit the msmtp configuration file in the user’s home directory. I use VI editor to achieve this
vi ~/.msmtprc
Add the following lines to the file, it configures msmtp for both Gmail and Yahoo
account yahoo tls on tls_starttls off auth on host smtp.mail.yahoo.com user user1 from user1@yahoo.com password ******
account gmail tls on auth on host smtp.gmail.com port 587 user user1@gmail.com from user1@gmail.com password ******
Since the file contains sensitive data like passwords you should assign secure permissions
chmod 600 ~/.msmtprc