Send an email with an attached file using telnet or netcat

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...


Solution 1:

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:<[email protected]>';
    sleep 3; 
    echo 'RCPT TO: <kyle@test_dest.com>';
    sleep 3;
    echo 'DATA';
    sleep 3;
    echo -e 'To:[email protected]\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

Solution 2:

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.

Solution 3:

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!