ADD:
# Set defaults.
defaults
# Enable or disable TLS/SSL encryption.
tls off
tls_starttls
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Set up a default account's settings.
#account default
account email_id
host smtp.domain.com
port 587
auth on
user email_id@domain.com
password ****
from email_id@domain.com
logfile /var/log/msmtp/msmtp.log
account default: email_id
nano /etc/php5/conf.d/php.ini
add:
sendmail_path = /usr/bin/msmtp -t
sudo mkdir /var/log/msmtp
sudo chown www-data:adm /var/log/msmtp
sudo nano /etc/logrotate.d/msmtp
/var/log/msmtp/*.log {
rotate 12
monthly
compress
missingok
notifempty
}
Create file called e.g. testmail having the following contents:
To: <username>@domain.com
From: username@gmail.com
Subject: A test
Yadda, yadda, yadda.
And execute cat testmail | msmtp komu@domain.com
یک راهنمای خوب دیگه
http://devblog.virtage.com/2013/05/email-sending-from-ubuntu-server-via-google-apps-smtp-with-msmtp/
It’s common request to allow daemons like cron or applications to send outgoing email to server’s administrator. Instead of configuring full-blown email server like Postfix you can install lightweight forwarder to real external SMTP server, while keeping binary compatibility with traditional Unix sendmail MTA.
Table of Contents[hide]
1 Google Apps / Gmail SMTP ports
2 Get prepared
3 MSMTP configuration
4 Logging
5 Setting PHP to MSMTP
6 Symlink to sendmail
7 Try out MSMTP
8 Error “msmtp: account default not found: no configuration file available”
9 Mail command
10 SSMTP: alternative for MSMTP
Sendmail compatibility is crucial as it is expected by many many applications. Apart from cron, you may need it for PHP mail()function, Bugzilla mailing feature and many others stuff originating from Linux/Unix universe.
For this tutorial I chose MSMTP delegating to secured Google Apps (or Gmail) SMTP. However instructions apply to any SMTP.
Google Apps / Gmail SMTP ports
On Google Support article you find up-to-date SMTP server address and configuration. Google SMTP can be accessed only over secured TLS which complicates MSTMP setup a little.
Get prepared
First off, install MSTMP on your box:
$ sudo apt-get install msmtp
I always start with discovering remote server certificate with --serverinfo option. Also, for Gmail I changed default port 465 which never worked for me to alternative port 587. Your output would be similar to the following:
$ msmtp --serverinfo --host=smtp.gmail.com --tls=on --tls-certcheck=off --port=587 SMTP server at smtp.gmail.com (ee-in-f109.1e100.net [173.194.65.109]), port 587: mx.google.com ESMTP y12sm259113106eeb.11 TLS certificate information: Owner: Common Name: smtp.gmail.com Organization: Google Inc Locality: Mountain View State or Province: California Country: US Issuer: Common Name: Google Internet Authority Organization: Google Inc Country: US Validity: Activation time: St 16. únor 2011, 05:38:09 CET Expiration time: Čt 16. únor 2012, 05:48:09 CET Fingerprints: SHA1: DB:A0:2A:07:00:F9:E3:23:7D:07:E7:52:3C:95:9D:E6:7E:12:54:3F MD5: 02:4C:12:F3:37:1F:0C:C1:EB:10:4B:92:F7:F1:E0:DF Capabilities: SIZE 35882577: Maximum message size is 35882577 bytes = 34,22 MiB STARTTLS: Support for TLS encryption via the STARTTLS command AUTH: Supported authentication methods: PLAIN LOGIN
MSMTP configuration
MSMTP can be driven from command line for one-time use, or from user or system-wide configuration file. Configuration file location is listed on --version command.
$ msmtp --version ... System configuration file name: /etc/msmtprc User configuration file name: /home/libor/.msmtprc ...
If system configuration file /etc/msmtprc doesn’t exist, create it
$ sudo touch /etc/msmtprc
Copy’n'paste & customize the following configuration for your Gmail/Google Apps account. Below I configured one account and again used alternative port 587. Consult MSMTP man page for more details.
# Default settings that all others account inherit defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt # Logging - uncomment either syslog or logfile, having both uncommented disables logging at all. #syslog on # Or to log to log own file #logfile /var/log/msmtp.log keepbcc on # Gmail/Google Apps (configure as may as you want) account gmail host smtp.gmail.com port 587 from your@gmail user your@gmail password secretPassword # Default account to use account default : gmail
Because this file contains password in plain text you should cut down its permissions. Let’s make use of group called mail coming from default Ubuntu installation. Any process which is run under mail group member can read this file and therefore actually send email.
$ sudo chgrp mail /etc/msmtprc $ sudo chmod 660 /etc/msmtprc
Add any users who needs to be member of mail group (like your webserver user etc.)
$ sudo adduser www-data mail $ sudo adduser ... mail
Logging
If not configured differently, MSMTP has logging disabled at all. Hopefully MSMTP can be set to log to separate log file or syslog, but not both (it will stop logging).
In /etc/msmtprc or your user configuration file add/change key
logfile /var/log/msmtp.log or whatever file you wish to log into dedicated file OR
syslog on to enable syslog logging. Default facility is LOG_USER or specify with syslog
MSMTP logs to syslog but we want to tweak it to log on startup to /var/log/msmtp.log instead.
If you log to file, it has to exists prior MSMTP can log into it. Therefore create & set permissions appropriately:
$ sudo touch /var/log/msmtp.log $ sudo chgrp mail /var/log/msmtp.log $ sudo chmod 660 /var/log/msmtp.log
Setting PHP to MSMTP
Open file /etc/php5/apache2/php.ini and change sendmail_path = "/usr/bin/msmtp -t"
Restart Apache to gain permissions to read MSMTP configuration file and send emails sudo service apache2 restart
Symlink to sendmail
However MSMTP is binary compatible with sendmail, it doesn’t create sendmail executable in your path. Applications like Bugzilla has hard-wired link to sendmail executable and in current setup you will see error the following error whey you try to send email:
There was an error sending mail from 'bugzilla-daemon@virtage.com' to 'support@virtage.com': Couldn't find 'sendmail' executable in your PATH and $Email::Send::Sendmail::SENDMAIL is not set.
To fix, symlink MSMTP to common places:
$ ln -s /usr/bin/msmtp /usr/sbin/sendmail $ ln -s /usr/bin/msmtp /usr/bin/sendmail $ ln -s /usr/bin/msmtp /usr/lib/sendmail
Try out MSMTP
Always use -v option to see all SMTP communication. MSMTP reads message body from stdin (as sendmail do).
Create file called e.g. testmail having the following contents:
To: <username>@domain.com
From: username@gmail.com
Subject: A test
Yadda, yadda, yadda.
And execute cat testmail | msmtp komu@domain.com
Do not merely use “echo ‘Yadda, yadda, yadda.’” instead of “cat test.mail”. This causes at least Gmail and Yahoo to deliver the mail incorrectly. Recipient specified as commandline argument is required however To recipient from file takes precedence over commandline.
You may also review msmtp.log with e.g. tail /var/log/msmtp.log
Error “msmtp: account default not found: no configuration file available”
Can you see this error in console or in logs and mail is not actually sent? Check the permissions of/etc/msmtprc for user running msmtp. This error indicates that a process doesn’t have permission to read configuration file.
Mail command
Many scripts assume existence of mail command. For Ubuntu it could be installed via heirloom-mailxpackage.
Mail offers commandline interface more suitable for scripting then msmtp. Send email with mail program is easy as
echo 'Hi from mail body' | mail -s 'Some subject' 'some@email'
SSMTP: alternative for MSMTP
There is also SSMTP but it seems to be abandoned, though it’s still in Ubuntu repositories. For completeness, here are some links for SSMTP:
http://linux.die.net/man/8/ssmtp
https://wiki.archlinux.org/index.php/SSMTP
http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/
http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/