Sending Email from Linux

 

Here is the simple way to write an email script which can be utilized to send emails to recipients.

Login to your Linux box , I am using a virtual machine with Linux Mint operating system installed running on Oracle Virtual Box.

login as: sloba
sloba@**********'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic i686)

Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Fri Jul 24 12:52:24 2015 from msi-l1028.metricstream.com
sloba@sloba-VirtualBox ~ $ 

Below is a very basic and simple script which can be used to send email :

 

##################################################################
# Email notification
##################################################################
SUBJECT="TEST EMAIL FROM UNIX"
# use form to send from whom the email address has sent
FROM="automail@slobaexpert.wordpress.com"
DESCRIPTION="THIS IS AN AUTO-GENERATED MESSAGE"
#Put the email address of the person to whom you want to send with comma seperator
EMAIL="<<to email address>> @<<hostname >>.com"
EMAILMESSAGE=/tmp/emailmessage

 echo "From: $FROM" > $EMAILMESSAGE
 echo "To: $EMAIL" >> $EMAILMESSAGE
 echo "Subject: $SUBJECT " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo "$DESCRIPTION : DO NOT REPLY TO THIS EMAIL MESSAGE. " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "Test file email " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "Thanks, " >> $EMAILMESSAGE
 echo "$FROM" >> $EMAILMESSAGE
 cat $EMAILMESSAGE | /usr/sbin/sendmail -t

 

You can change the TO address from the above script and test it. But we need to make sure “sendmail ” is already installed on your server to use this , if not then you can install using the below command.

sudo apt-get install sendmail

Enable SSH server in Linux Mint

Normally we do connect to any server from WinScp or Putty tool from Windows platform to connect any Linux environment to do various activities. 

Here I am going to show how we can enable SSH on Linux Mint operating system. I am using a virtual machine was built on Oracle Virtual Box. 

image

The above image show my virtual machine running on Oracle VM virtual box. Now let me open my putty and try connect to my virtual machine. To know the hostname of the VM you can open the terminal and type “hostname” as shown like below:]

image

Open Putty :

image

Click on Open.

image

After this I get the above error. Now let try to enable SSH on our Linux machine we need to first install “OpenSSH” .  

There are two options that we can do to install “OpenSSH” :

Option 1:

  • Open the main menu, select the Software Manager
  • Search for “Openssh ” on the search box
  • Click on the package openssh-server, then select install.
  • Once the installation is completed then start SSH by executing “/etc/init.d/ ssh start”  command.

image

image

image

image

Click on “install”

Option 2: 

As Linux Mint Operating system is based on Ubuntu , so we can install “openssh” from terminal or console.

 

image

image

image

image

Check if port 22 is enabled or not under “sshd_config” file , we can also modify the port in this file to access SSH on another port but I am using the default setup i.e. port 22.

image

Now let’s try connecting through putty from my local system to the VM .

image

 

image

 

image

Now we can see that I am able to connect to my Linux machine using Putty after enabling SSH on VM server.