I often use telnet or netcat to connect smtp servers to send an email as a test.

Does anyone know how you would send an email using telnet or netcat but attach a file as well? There are probably better ways, but I still want to know :-)

I would be happy with a solution that uses a little bash shell to accomplish the goal, but don't want to use any other tools...

  • 1
    ehlo, this will probably include creating a mime attachment in a file, encode it and then paste it into your window. While it can be very useful to be able to just telnet to an SMTP server, write a short mail and send it (and I can), sending a file like that just isn't very practical. Try 'sendfile' or 'mutt' (even if you don't like to use tools).
    – Andrioid
    Jul 10, 2009 at 14:10
  • Just after echo '.'; you might want to add this echo '.'; sleep 3; echo 'quit';
    – user65880
    Jan 6, 2011 at 12:26

9 Answers 9


Okay, so using everyone's comments as a starting point I came up with this silly mess :-) ...

    sleep 5; 
    echo 'ehlo';
    sleep 3;
    echo 'MAIL FROM:<Test@test.com>';
    sleep 3; 
    echo 'RCPT TO: <kyle@test_dest.com>';
    sleep 3;
    echo 'DATA';
    sleep 3;
    echo -e 'To:kyle@testdest.com\nMIME-Version: 1.0 (mime-construct 1.9)\nContent-Type: application/zip\nContent-Transfer-Encoding: base64\n\n';
    dd if=/dev/urandom bs=4 count=10 2>/dev/null | openssl base64;
    echo '.';
} | telnet mx1.testdest.com 25
  • Although since I am just sending random data, not sure if the data is remaining intact ... Jul 10, 2009 at 19:03
  • You have a higher tolerance for tedium than me! >smile< Jul 11, 2009 at 1:38
  • How could I alter this to send a specific text file? Oct 7, 2016 at 18:45

Ick. You're going to have to base64 encode the attachment and create the MIME headers.

Rather than generating a new message "on the fly" each time, it would probably be easier just to email yourself a very short example message from a "real" email program (leveraging the work that the people who wrote it did to put the attachment into the proper encoding and creating the MIME headers).

Save that message off into a text file w/ its headers (removing the transport header, of course), and just modify / copy / paste it into telnet or netcat for future sessions.


While hand testing SMTP servers by hand is possible and viable, using a tool designed for this will be much easier.

This article explains SWAKS. swaks is designed for smtp server testing. Supports attachments, authentication and encryption!

  • +1 Now that is something I am going to start using! :-) Jul 10, 2009 at 19:20

i sumbled upon this entry while i were searching for something of the same. and from the awnsers here and som additional research i managed to make this script.


# Default reception
# Default Subject
SUBJECT="You got mail - $DATE";
# Default Contents
MSGBODY="Hello, this is the default message body";
# Default Attachment
# Default smtp server

showUsage() {
        echo "$0 -a /file/to/attach [-m /message/file] [-M \"Message string\"] -s \"subject\" -r receiver@domain.com"
        echo "The attachment (-a) is required, if no attachment is used then rather use sendmail directly."

fappend() {
    echo "$2">>$1;

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This might need correction to work on more places, this is tested at a ubuntu 13.10 machine.  #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
domain=`grep search /etc/resolv.conf | awk '{print $2;}'`

while getopts "M:m:a:s:r:" opt; do
  case $opt in
          SUBJECT="$OPTARG - $DATE"
          MSGBODY=`cat $OPTARG`

if [ "$ATTACHMENT" = "" ]; then
        exit 1

MIMETYPE=`file --mime-type -b $ATTACHMENT`
TMP="/tmp/tmpmail_"`date +%N`;
BOUNDARY=`date +%s|md5sum|awk '{print $1;}'`

DATA=`cat $ATTACHMENT|base64`

rm $TMP 2> /dev/null

fappend $TMP "EHLO $computer.$domain"
fappend $TMP "RCPT TO:<$TOEMAIL>"
fappend $TMP "DATA"
fappend $TMP "From: $FREMAIL"
fappend $TMP "To: $TOEMAIL"
fappend $TMP "Reply-To: $FREMAIL"
fappend $TMP "Subject: $SUBJECT"
fappend $TMP "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
fappend $TMP ""
fappend $TMP "This is a MIME formatted message.  If you see this text it means that your"
fappend $TMP "email software does not support MIME formatted messages."
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: text/plain; charset=UTF-8; format=flowed"
fappend $TMP "Content-Disposition: inline"
fappend $TMP ""
fappend $TMP "$MSGBODY"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\""
fappend $TMP "Content-Transfer-Encoding: base64"
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";"
fappend $TMP ""
fappend $TMP "$DATA"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY--"
fappend $TMP ""
fappend $TMP "."
fappend $TMP "quit"

netcat $mailserver $mailserverPort < $TMP >> $TMP
if [ "$rc" -ne "0" ]; then
    echo "Returncode: $rc"
    echo "Please inspect $TMP"
    rm $TMP;

One thing you might want to add is authentication. i dont need it so i havent added it.

I think it only requires md5sum, netcat, file, awk and the base64 commands, id guess they are pretty standard in most systems.

  • Would it be possible to add multiple attachments easily, to this solution? Jun 22, 2016 at 21:37

Telnet - send email with multiple attachments

cat attachment.zip | base64 > zip.txt
cat attachment.pdf | base64 > pdf.txt

# Content-Type: text/csv; name="$FILE"                        # for CSV files
# Content-Type: application/x-msdownload; name="$FILE"    # for executable 
# Content-Type: text/xml; name="$FILE"                        # for xml files or try application/xml

telnet smtp.server.dom 25

MAIL FROM: email@server.com
RCPT TO: email@server.com
Subject: Test email
From: email@server.com
To: email@server.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="X-=-=-=-text boundary"

--X-=-=-=-text boundary
Content-Type: text/plain

Put your message here...

--X-=-=-=-text boundary
Content-Type: application/zip; name="file.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.zip"

UEsDBBQAAAAIAG1+zEoQa.... copy/paste zip.txt

--X-=-=-=-text boundary
Content-Type: text/pdf; name="file.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.pdf"

UEsDBBQAAAAIAG1+zEoQa.... copy/paste pdf.txt

--X-=-=-=-text boundary

  • 2
    Welcome to serverfault. People who post answers like yours are really welcome on this website! :) gratz
    – bgtvfr
    Jun 13, 2017 at 8:40

This is what i'm doing to send email with bash. I use it to send me a log file and external IP adress, feel free to use it :

# Send email from bash with attachment
# by Psirac - www.subk.org
mylogin=`echo 'MYUSERNAME' | openssl base64`
mypassword=`echo 'MYPASSWORD' | openssl base64`
myip=`wget -qO- icanhazip.com`
myfile=`cat /tmp/mytest.log | openssl base64`

exec 9<>/dev/tcp/$mailserver/25
echo "HELO routeur.tripfiller" >&9
read -r temp <&9
#echo "$temp"
echo "auth login" >&9
read -r temp <&9
#echo "$temp"
echo "$mylogin=" >&9
read -r temp <&9
#echo "$temp"
echo "$mypasswd=" >&9
read -r temp <&9
#echo "$temp"
echo "Mail From: $from" >&9
read -r temp <&9
#echo "$temp"
echo "Rcpt To: $to" >&9
read -r temp <&9
#echo "$temp"
echo "Data" >&9
read -r temp <&9
#echo "$temp"
echo "To:$to" >&9
echo "MIME-Version: 1.0" >&9
echo "Subject: Test mail sended at $mydate" >&9
echo "From: $from" >&9
echo "To: $to" >&9
echo "Content-Type: multipart/mixed; boundary=sep" >&9
echo "--sep" >&9
echo "Content-Type: text/plain; charset=UTF-8" >&9
echo "Here your text..." >&9
echo "External IP adress: $myip" >&9
echo "--sep" >&9
echo "Content--Type: text/x-log; name=\"mytest.log\"" >&9
echo "Content-Disposition: attachment; filename=\"mytest.log\"" >&9
echo "Content-Transfer-Encoding: base64" >&9
echo "" >&9
echo "$myfile" >&9
echo "--sep--" >&9
echo "." >&9
read -r temp <&9
echo "$temp"
echo "quit" >&9
read -r temp <&9
echo "$temp"
#echo "All Done. See above for errors"
exit 0

Hope it was good for you ;)



You'll need to review the SMTP protocol specification. It's a surprisingly light read for a technical specification, and will help you understand how the email process works.

Specifically, realize that attachments are converted into MIME types and encoded in text, so any attachments you'd like to send via telnet would have to be converted into text and transmitted as such via the telnet protocol.


If all you're testing is 'did the attachment deliver', you might possibly get away with using the pre-MIME standard of attachments: uuencode. Unlike MIME, it is a lot simpler to create messages. Unlike MIME it doesn't require any headers. However, not all mail clients recognize uuencoded files as attachments anymore so I suggest testing to see if you can use it. If it does, you've just saved yourself a lot of effort. If it doesn't, then pre-constructing your MIMEed message via perl or something and piping it through something like NetCat is probably the way to go.

Worth a look.

  • 20 years ago this was a good answer. Not now. Use one of the many MIME libraries and create a MIME-compliant email, complete with Base64 encoding. Aug 5, 2014 at 14:32

There is a wonderful Perl script to this job. You can find it here:


smtp-cli v2.9

Script is from author: Michal Ludvig (c) 2003-2011 http://smtp-cli.logix.cz

I use it myself and it is works great, thanks to Michal ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.