views:

24

answers:

2

Hi Everyone,

I have run across an interesting problem. I am sending email with attachments through the NET::SMTP class in ruby through Apple's me.com SMTP servers and I am running into some funny issues.

I am trying to send a series of jpg files through the SMTP server. I am encoding them in ruby and when I send to another me.com email all five jpg images show up at the other end in perfect condition. When I send to my gmail address the files truncate at 90k (they are normally around 500k). When I open the two emails in textmate I see the encoding on the text portion of the email is 8bit on the email sent to the .me address and 7bit in the email sent to the gmail server. I'm not sure if this is my problem or not.

Here is a brief of the code I am using:

file1Content = File.read(directory +'/Photo_1.jpg')
file1 = [file1Content].pack("m")

marker = "AUNIQUEMARKER"

body =<<EOF
#{emailbody}
EOF

# Define the main headers.
part1 =<<EOF
From: #{from}
To: #{donor}
Subject: #{subject}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF

# Define the message action
part2 =<<EOF   
Content-Transfer-Encoding:8bit
Content-Type: text/plain
#{body}
--#{marker}
EOF

# Define the attachment section
part3 =<<EOF
Content-Type: image/jpeg; name=\"Photo_1.jpg\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="Photo_1.jpg"
#{file1}
--#{marker}
EOF

(etc to 5 files where I end the marker with --#{marker}--

I would really appreciate any help you could give. I'm completely stumped. A couple of other notes. I am using MacRuby and not all Gems work on it, especially for embeded MacRuby. I have had some success with small libraries but I haven't had any luck with ActionMailer.

A: 

I have no experience with attachment encoding, but I think 7bit is still the standard.

I recommend using a mail lib that does all this for you, like the one from Mikel. Re-inventing the wheel is not really useful, unless you only want to learn inventing wheels.

Link to Mikel's mail lib: http://github.com/mikel/mail

Ariejan
Thanks Ariejan, As I mentioned in my email I am using MacRuby and a number of the gems don't work for it. I really need to roll my own. I don't like inventing wheels but it's the price I pay for working with new technologies.
tsugua
In that case, you could use Mikel's library as a good starting point. The 7bit encoding will be in there somewhere for you to look at.
Ariejan
A: 

I had a friend come in and we worked through it and here is the result.

In the email encodings the line breaks are extremely important. Some mail servers appear to be more forgiving (apple's) which is why I didn't see the problem initially.

Here is the working code:

marker = "AUNIQUEMARKER"

body =<<EOF
#{emailbody}
EOF

# Define the main headers.
part1 =<<EOF
From: #{from}
To: #{donor}
Subject: #{subject}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}

--#{marker}
EOF

# Define the message action
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit

#{body}
--#{marker}
EOF

# Define the attachment section
part3 =<<EOF
Content-Type: image/jpeg; name=Photo_1.jpg
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=Photo_1.jpg

#{file1}
--#{marker}
EOF

part4 =<<EOF
Content-Type: image/jpeg; name=Photo_2.jpg
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=Photo_2.jpg

#{file2}
--#{marker}
EOF
tsugua