Sending Email with Netcat
Is it possible to send an email from a host that has no email client software installed? As long as you have netcat, of course it is!
Netcat (/usr/bin/nc on Red Hat Enterprise Linux systems) is a simple utility for reading and writing data across TCP/UDP connections. It's often used for testing and debugging network connections. In its most basic usage, netcat allows you to feed a stream of data to a specific port on a specific host, which is perfect for our purpose here. Check the netcat man page for more information on it's various features. There are also sample scripts under /usr/share/doc/nc-*/. If netcat is not installed on your Red Hat Enterprise Linux, CentOS or Fedora system, you can install it with the command yum install nc.
What we will be doing with netcat is using it to feed a stream of data to port 25 (SMTP) on a mail relay, making it believe it's talking to a regular email client. In order to do this, we first need to figure out what our email server expects to see from a client. This can be done by connecting via telnet to our SMTP relay host and issuing the correct SMTP commands, as in the following example:
[user@host]# telnet smtp.domain.com 25 Trying 192.168.0.1... Connected to smtp.domain.com (192.168.0.1). Escape character is '^]'. 220 myrelay.domain.com ESMTP HELO smtp.domain.com 250 myrelay.domain.com MAIL FROM:<alice@hacker.com> 250 sender <alice@hacker.com> ok RCPT TO:<bob@secure.net> 250 recipient <bob@secure.net> ok DATA 354 go ahead From: [Alice Hacker] <alice@hacker.com> To: [Bob Smith] <bob@secure.net> Date: Mon, 12 Apr 2010 14:21:26 -0400 Subject: Test Message Hi there! This is supposed to be a real email... Have a good day! Alice . 250 ok: Message 222220902 accepted QUIT 221 myrelay.domain.com Connection closed by foreign host. [user@host]#
Note that the userid part of the "From" address does not have to contain a valid userid, only a valid domain name. You will have to replace "smtp.domain.com" with a valid SMTP relay that allows relaying from your host. Generally, experienced admins will disallow relaying from unknown hosts to discourage spam. Additionally, the body of the email (everything after the "DATA" command) is ended by sending a blank line, followed by a line with a period (.) on it by itself.
Now that we know what the remote server expects to see, we can craft a text file with our SMTP commands and the message to be sent. The recipients mail server will expect the date to be in a particular format.
Use the command:
date '+%a, %d %b %Y %H:%M:%S %z'
To generate a date string that resembles:
Mon, 12 Apr 2010 14:21:26 -0400
The contents of your message file should resemble this example:
HELO host.example.com MAIL FROM:<test@host.example.com> RCPT TO:<bob@example.com> DATA From: [Alice] <alice@geek.com> To: <bob@example.com> Date: Mon, 12 Apr 2010 14:21:26 -0400 Subject: Test Message Hi there! This is supposed to be a real email... Have a good day! Alice . QUIT
Now we can feed this text file to the netcat program as follows:
# /usr/bin/nc smtp.domain.com 25 < /tmp/message 220 myrelay.domain.com ESMTP 250 myrelay.domain.com 250 sender <alice@hacker.com> ok 250 recipient <bob@secure.net> ok 354 go ahead 250 ok: Message 222220902 accepted 221 myrelay.domain.com #
And your email has been sent!
Again, what we did here was feed data to netcat, which then sends that data to port 25 on the specified host (our mail relay). Since we've formatted the data to look like an email. the SMTP server accepts it as it would any other email and sends it, assuming of course that we're allowed to relay email.
Given a little time and effort, a nice bash or korn shell script can be written that automates the creation of the message text file. You can specify multiple recipients in the email header, and include the output of other commands in the body of the email. For example, a monitoring script which is periodically executed via a cron job can email it's standard output to a list of recipients.
Pete Vargas Mas is an avid indoorsman and a Linux Consultant in the Washington DC Metro area. Pete is a RHCE and a MCITP, which so far has not caused any eddies in the space-time continuum. He spends most of his time these days herding 529 Linux servers.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- Keeping track of IP address
1 hour 7 min ago - Roll your own dynamic dns
6 hours 20 min ago - Please correct the URL for Salt Stack's web site
9 hours 32 min ago - Android is Linux -- why no better inter-operation
11 hours 47 min ago - Connecting Android device to desktop Linux via USB
12 hours 15 min ago - Find new cell phone and tablet pc
13 hours 14 min ago - Epistle
14 hours 42 min ago - Automatically updating Guest Additions
15 hours 51 min ago - I like your topic on android
16 hours 37 min ago - This is the easiest tutorial
23 hours 13 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
How to test a smtp server
Hi,
I've setup a new Exchange 2007 Edge as my smtp server for sending and receiving external emails. Now sending is fine. But i'm not able to receive from internet. The DNS for it domain in internet is already up This server is in DMZ and protected by firewall. I confirmed the rules are open for port 25 from any source. When i telnet from internet to my server using port 25, the connection timeout. Please tell me how i can test this from internet. Thanks.
Use /dev/tcp in bash
You dont need nc, when you can read/write to /dev/tcp :)
http://tldp.org/LDP/abs/html/devref1.html
the correct approach is to
the correct approach is to programatically use "expect", for example (random google hit) http://petervibert.com/articles/2/ "Expect SMTP Script"
not quite
when you speak smtp you need to wait for each server reply.
You REALLY do not want to to this agains any postfix boxes that I administer.
"221 2.7.0 Error: I can break rules, too. Goodbye." is my favorite postfix quote.
send mail with attachment to gmail programatically with python
Similar lines http://www.h3manth.com/content/send-mail-gmail-programatically-python