Search Posts on Binpipe Blog

Configuring MSMTP to work as SMTP Mail Relay for Php Mail() in Linux Server

Most of the times, the php mail function dosen't work in Linux servers because Postfix or Sendmail relays are misconfigured. To workaround this situation you can build your own mail relay using MSMTP and use an SMTP server as the carrier I have tested this solution in a CentOS 5.8 box, however it should work in any Linux distribution as we compile the main package (msmtp) from source.



Please follow the following command sequence to get going. In case of commands not working for you please post a comment so that we can help.

yum install make gcc pkgconfig
wget http://voxel.dl.sourceforge.net/sourceforge/msmtp/msmtp-1.x.x.tar.bz2
tar xjvf msmtp-1.x.x.tar.bz2
cd msmtp-x.x
./configure
make
make install
make clean

vi /etc/msmtp   (put the contents below)

#####NO_TLS#####
defaults
tls off
tls_certcheck off
tls_starttls off
#tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.com
port 25
auth on
user support@testdomain.com
password xxxxxxx
from no-reply@testdomain.com
logfile /var/log/msmtp.log
#####

OR


#####WITH TLS#####
defaults
tls on
tls_certcheck off
tls_starttls on
#tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.testdomain.com
port 587
auth on
user binpipe@testdomain.com
password xxxxxxx
from no-reply@testdomain.com
logfile /var/log/msmtp.log
#####


ln -s /etc/msmtp /usr/local/etc/msmtprc

touch /var/log/msmtp.log
chmod 777 /var/log/msmtp.log

Test with this:

echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=test@example.com -t tester@testmail.com


To integrate with php, write this in /etc/php.ini

;sendmail_path = /usr/sbin/sendmail -t -i
sendmail_path = /usr/local/bin/msmtp -t -i


service httpd restart

Now, php mail function will work using smtp as the relay and mails will go through.

Autocomplete in shell scripts with COMPGEN

Today, I was required to write a shell script which will detect a directory which has a random name-ending (Eg. version-YQEG$1200 etc), enter it and run some operations inside that directory. When we work manually in commandline, this is easy because since we know that the directory begins with 'version' we type version and hit 'tab' key which autocompletes it. But how to do that in a shell script ?

That is when this command compgen came to my rescue.

So lets have a look at a command that I did not know hitherto: compgen


The compgen builtin command expands a list of arguments to generate completion matches, so it allows us to display the commands, functions, directories and aliases available for the current user. In short compgen is a command that displays commands.

So compgen is a command builtin in the bash shell so if you use this shell you have this command available in the terminal,
Let’s see how we can use it directly from the command line, as first thing let’s say that in general compgen has different options that you can use to specify which kind of thing you are looking for, so the basic usage is:

compgen [option] [word]

This generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output let’s see some examples:

Working with directories

The option for directories is -d so compgen -d will give as output the list of all the directory in the current path, or you can ask for a specific path such as:

xubuntu-home:~$ compgen -d /etc/s  /etc/sudoers.d  /etc/sysctl.d  /etc/sensors.d  /etc/skel  /etc/samba  /etc/sane.d  /etc/ssh  /etc/speech-dispatcher  /etc/systemd  /etc/sgml  /etc/ssl  /etc/sound  /etc/smartmontools  /etc/snmp  /etc/security

In this case I’ve asked compgen to show all the directory in /etc/ that start with the letter s, using just /etc/ as parameter it will show all the directories in /etc/.

Working with commands

In the same way compgen can also show all the available commands for an user with the flag -c : compgen -c if you run this command probably you’ll get a list of some thousands of commands so it’s better to use it with a | less or to show only some commands use an additional parameter that represents a part of the word you are looking for:

xubuntu-home:~$ compgen -c ss  sshd  ssgrep  ssh-vulnkey  ssindex  ssconvert  ssh-import-id  ssh  ssh-copy-id  ssh-argv0  ssh-keygen  ssh-askpass  ssh-add  ssh-keyscan  ssh-agent  ss

In this example I can see all the commands that i can use that start with ss

Other Functions

This is a short list of options that you can use with compgen:

Show all the bash built-ins commands.

compgen -b

Show all the files and directories in current path (or specify a path like in the directory)

compgen -f

Show all the bash keywords

xubuntu-home:~# compgen -k  if  then  else  elif  fi  case  esac  for  select  while  until  do  done  in  function  time  {  }  !  [[  ]]  coproc

Show all the bash functions
compgen -A function

Show all the alias available for the current user

xubuntu-home:~$ compgen -a  vps  egrep  fgrep  grep  ls

Show all the usernames defined in the server:

xubuntu-home:~# compgen -u s  sys  sync  syslog  speech-dispatcher  saned  sshd  sslh

Show all the groups defined in the server:

xubuntu-home:~# compgen -g p  proxy  plugdev  pulse  pulse-access  postfix  postdrop