It is currently Sat Jan 16, 2021 11:48 pm
All times are UTC + 8 hours

[HOW TO] Use msmtp to send email

Share your awesome tips and tricks here.

[HOW TO] Use msmtp to send email

Postby Mechano » Sat Mar 15, 2014 5:40 am

Here a small how to for sending email both from shell and from PHP using the smtp client msmtp.
It's for ADM <= v2.1.

At first you have to install from App Center the Optware ipkg.
After, enter the shell with root user and give the command:
Code: Select all
ipkg install msmtp

Configuring msmtp for use with Gmail and Yahoo

Create or edit the msmtp configuration file in the user’s home directory.
Remember that every user needs to send email must have it's own .msmtprc
Use vi editor:
Code: Select all
vi ~/.msmtprc

With the following lines you configure msmtp for both Gmail and Yahoo:
Code: Select all
account yahoo
tls on
tls_starttls off
auth on
user user
password ******
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account gmail
tls on
auth on
port 587
password ******
tls_trust_file /etc/ssl/certs/ca-certificates.crt

It's important the line tls_trust_file, the Asustor NAS have already this file and there are already some certificates for the main services.
If you're going to use a not listed provider, you've to find the authority certificate and add into the file /etc/ssl/certs/ca-certificates.crt
or another file to specify into the option tls_trust_file.

To protect passwords give read access only to the owner:
Code: Select all
chmod 600 ~/.msmtprc

Now we can create the mail body message:
Code: Select all
vi test_email

and fill with:
Code: Select all
From: Bob <>
To: Jane <>
Subject: Hello Jane
Test email sent with msmtp

Now we can test email sending with the command:
Code: Select all
cat test_email | msmtp -a yahoo

It's useful to send email from shell scripts when we want to automate certain process into cron and we need to send a feedback.

Now let's configure for use with PHP Mail() function.

Use vi to create the file:
Code: Select all
vi /volume0/usr/builtin/etc/php5/apache2/msmtprc

And fill it like in the previous example.
Protect with right access:
Code: Select all
chmod 600 msmtprc

and change the owner in accord with the ones that runs the process apache2
find with:
Code: Select all
ps -ef | grep apache2

 9916 root      0:00 /usr/builtin/sbin/apache2 -k start
 9918 admin      0:00 /usr/builtin/sbin/apache2 -k start

there're different instances of apache with root and admin, but you use admin user only if it doesn't work change to root:
Code: Select all
chown admin msmtprc

now edit the file php.ini:
Code: Select all
vi /volume0/usr/builtin/etc/php5/apache2/php.ini

and find sendmail_path option to change like this (change yahoo with gmail or whatever in accord with the account name into msmtprc file:
Code: Select all
sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a yahoo -t"

Finally you can try your php code:
Code: Select all
   mail("","Hello World","Email sent using PHP via msmtp");

If you are hosting a CMS like Joomla! or Wordpress they have their builtin function for GMail or Yahoo! smtp server, check CMS documentation for how to set.
Last edited by Mechano on Tue Mar 18, 2014 9:58 pm, edited 2 times in total.
Posts: 25
Joined: Wed Mar 12, 2014 7:34 am

Re: [HOW TO] Use msmtp to send email

Postby orion » Mon Mar 17, 2014 10:56 am

Great! Thanks for sharing.
User avatar
Posts: 2850
Joined: Wed May 29, 2013 11:09 am

Return to Tips & Tricks

  • You cannot post new topics in this forum
    You cannot reply to topics in this forum
    You cannot edit your posts in this forum
    You cannot delete your posts in this forum
    You cannot post attachments in this forum
  • Who is online

    Users browsing this forum: No registered users and 2 guests