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...
Okay, so using everyone's comments as a starting point I came up with this silly mess :-) ...
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!
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.
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.
Telnet - send email with multiple attachments
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 :
Hope it was good for you ;)
psirac.
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.
There is a wonderful Perl script to this job. You can find it here:
http://www.logix.cz/michal/devel/smtp-cli/
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 ;)