Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I need to format a text file so that it can be read by an SMTP service to send an e-mail that has an attachment?

We have a server where we are using II6 SMTP to send mail from a Pickup folder. The goal is to drop a properly formatted text file into Mailroot\Pickup and then the file will be automatically processed and sent to the correct SMTP recipient.

For simple files, this works correctly. Here's an example of a simple file that works (domain names changed):

    From:bmccormack@mydomain.com
    To:bmccormack@mydomain.com
    Subject:Hello World!

    Test Body Of The E-mail

When I drop a text file containing the above contents into the Mailroot\Pickup folder, it sends correctly. However, I haven't been able to figure out how to get an attachment to work.

I found some material that explained how to encode an SMTP attachment and another tool for simple base64 encoding conversion. Using the information on those pages, I came up with the following text:

    From:bmccormack@mydomain.com
    To:bmccormack@mydomain.com
    Subject:Hello World!

    MIME-Version: 1.0
    Content-Type: text/plain;
      boundary="Attached"
    Content-Disposition: inline;

    --Attached
    Content-Transfer-Encoding: base64
    Content-Type: text/plain;
      name="attachment.txt"
    Content-Disposition: attachment;
      filenamename="attachment.txt"

    VGhpcyBpcyBhIHRlc3Qgb2Ygc29tZXRoaW5nIHRvIGVuY29kZS4NCk5ldyBsaW5lDQpOZXcgTGlu
    ZQ0KIkhlbGxvdyIgISEhDQo9PT09ICcgZnNkZnNkZiAxMjM1NDU2MzQzNA==
    --Attached--

However, when I place the above text in a file and drop it into Mailroot\Pickup, it doesn't send an attachment correctly. Instead, an e-mail shows up with the following in the body of the e-mail:

    MIME-Version: 1.0
    Content-Type: text/plain;
      boundary="Attached"
    Content-Disposition: inline;

    --Attached
    Content-Transfer-Encoding: base64
    Content-Type: text/plain;
      name="attachment.txt"
    Content-Disposition: attachment;
      filenamename="attachment.txt"

    VGhpcyBpcyBhIHRlc3Qgb2Ygc29tZXRoaW5nIHRvIGVuY29kZS4NCk5ldyBsaW5lDQpOZXcgTGlu
    ZQ0KIkhlbGxvdyIgISEhDQo9PT09ICcgZnNkZnNkZiAxMjM1NDU2MzQzNA==
    --Attached--

I can't figure out what I need to do to format the text file so that the SMTP service correctly sends attachments.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The formatting for files to be read by SMTP can be a little tricky. Be careful with white space and semicolons because the file reader is likely not going to be very forgiving.

Try the following text file, which is a message with 3 attachments. The first attachment is plain ASCII text and will be read as if it is the message body. The other attachments will read as actual file attachments.

From: <bmccormack@mydomain.com>
To: <scott@mydomain.com>
Subject: Hello World!
MIME-Version: 1.0
Content-Type: multipart/mixed;
  boundary="Attached"


--Attached
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit

Hey Scott, I think I've got it figured out.  See the attached file.
--Attached
Content-Type: text/plain;
  name="TestAttached.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
  filename="TestAttached.txt"

VGhpcyBpcyBhbiBleGFtcGxlIG9mIGJhc2U2NCBlbmNvZGVkIHRleHQu
--Attached
Content-Type: text/plain;
  name="SourceCode.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
  filename="SourceCode.txt"

RnJvbTogPGJtY2Nvcm1hY2tAbXlkb21haW4uY29tPg0KVG86IDxibWNjb3JtYWNrQG15ZG9tYWlu
LmNvbT4NClN1YmplY3Q6IEhlbGxvIFdvcmxkIQ0KTUlNRS1WZXJzaW9uOiAxLjANCkNvbnRlbnQt
VHlwZTogbXVsdGlwYXJ0L21peGVkOw0KICBib3VuZGFyeT0iQXR0YWNoZWQiDQoNCg0KLS1BdHRh
Y2hlZA0KQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOw0KQ29udGVudC1UcmFuc2Zlci1FbmNvZGlu
ZzogN2JpdA0KDQpIZXkgU2NvdHQsIEkgdGhpbmsgSSd2ZSBnb3QgaXQgZmlndXJlZCBvdXQuICBT
ZWUgdGhlIGF0dGFjaGVkIGZpbGUuDQotLUF0dGFjaGVkDQpDb250ZW50LVR5cGU6IHRleHQvcGxh
aW47DQoJbmFtZT0iYXR0YWNoZWRJbmZvLnR4dCINCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6
IGJhc2U2NA0KQ29udGVudC1EaXNwb3NpdGlvbjogYXR0YWNobWVudDsNCglmaWxlbmFtZT0iYXR0
YWNoZWRJbmZvLnR4dCINCg0KVkdocGN5QnBjeUJoYmlCbGVHRnRjR3hsSUc5bUlHSmhjMlUyTkNC
bGJtTnZaR1ZrSUhSbGVIUXUNCg0KLS1BdHRhY2hlZC0t

--Attached--
share|improve this answer
    
You're asking and answering your own questions? –  David Smith Mar 7 '10 at 16:56
3  
@David Well, I asked a question, didn't receive an answer from anyone, and in the meantime, found the answer myself. I decided to provide an answer in case others looking for a solution need it. –  Ben McCormack Mar 8 '10 at 19:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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