Sending Mail from Linux servers

Posted on 2014-11-14
Last Modified: 2014-11-25
Hi Experts,

Can some body help me with code to send mails from Linux server which is connected to internet.

Thanks in Advance

Question by:GK GK
LVL 24

Expert Comment

ID: 40442864
there are many different mail programs you can use, but the simplest is to type


from the command line, using ^D to end the message.

Of course, you have to make sure that your linux machine doesn't have any blocked ports, has sendmail running, is associated with a DNS so that others can find you, has a trusted site that it can relay mail from, etc, etc.

here is a tutorial on setting up sendmail:
LVL 25

Expert Comment

ID: 40442982
You could use sendmail,,,,

In /etc/mail/ file contains following line:
dnl define(`SMART_HOST',`smtp.your.provider')

Replace "smtp.your.provider" with the fully qualified domain name to look like this:

Then restart
# service sendmail restart
LVL 23

Expert Comment

ID: 40443329
Is the machine already setup to send email?  

There is a lot that goes into sending mail.  You can configure postfix or sendmail to use your ISP email account to send mail which is the easiest way.  What flavor of linux are you using?

Accepted Solution

Ernesto Couso Artiles earned 500 total points
ID: 40462261
Sendmail is a most popular smtp server used in most of Linux/Unix distribution. sendmail allows to send email from command line. Use below instructions to send email using ‘sendmail‘ command.

Create a file using following content.

# vim /tmp/email.txt
Subject: Terminal Email Send

Email Content line 1
Email Content line 2
Subject: line read as Subject of email.

Now send email using following command.

# sendmail  < /tmp/email.txt
Read more: Install and Configure Sendmail on CentOS/RHEL

2. Using 'mail' Command

mail command is most popular command to send emails from Linux terminal. Use few of below examples to send email.

# mail -s "Test Email" < /dev/null
-s is used for subject

To send email with attachment.

# mail -a /opt/backup.sql -s "Backup File" < /dev/null
-a is used for attachments

Also we can add comma separated emails to send email to multiple recipients together.

# mail -s "Test Email", < /dev/null
3. Using 'mutt' command

Mutt is basically use for reading mails from Linux terminal from local user mail boxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send email.

# mutt -s "Test Email" < /dev/null
Send email including attachment

# mutt  -s "Test Email" -a /opt/backup.sql < /dev/null
4. Using 'SSMTP' Command

sSMTP allows users to send emails from SMTP server from Linux command line. For example to send email to user use following command. Now type your subject of email as below with keyword Subject. After that type your message to be sent to user, After finishing your message press CTRL+d (^d) to send email.

# ssmtp
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
Read more: How to Setup SSMTP Server on Linux

5. Using 'telnet' Command

As my experience all system administrators use telnet command to test remote port connectivity test or login to server remotely. Most of newbie in Linux doesn't know that we can send email using telnet also, which is better way to trouble shoot email sending problems. Below is an example of email sending.

Red marked text is the user input and remaining is the responses of that commands.

# telnet localhost smtp

Connected to localhost.localdomain (
Escape character is '^]'.
220 ESMTP Sendmail 8.13.8/8.13.8; Tue, 22 Oct 2013 05:05:59 -0400
250 Hello [], pleased to meet you
mail from:
250 2.1.0 Sender ok
rcpt to:
250 2.1.5 Recipient ok
354 Enter mail, end with "." on a line by itself
This is test email only

250 2.0.0 r9M95xgc014513 Message accepted for delivery
221 2.0.0 closing connection
Connection closed by foreign host.

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help on running oracle SQL script in linux 3 59
windows 7 starter missing password 21 89
Why  my code (program) build with old compiler? 11 75
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question