电子邮件编码并通过 SMTP 发送 - Ruby
我遇到了一个有趣的问题。我通过 Apple 的 me.com SMTP 服务器通过 ruby 中的 NET::SMTP 类发送带有附件的电子邮件,并且遇到了一些有趣的问题。
我正在尝试通过 SMTP 服务器发送一系列 jpg 文件。我用 ruby 对它们进行编码,当我发送到另一封 me.com 电子邮件时,所有五个 jpg 图像都以完美状态显示在另一端。当我发送到我的 Gmail 地址时,文件会在 90k 处截断(通常约为 500k)。当我在 textmate 中打开这两封电子邮件时,我看到电子邮件文本部分的编码在发送到 .me 地址的电子邮件中为 8 位,在发送到 gmail 服务器的电子邮件中为 7 位。我不确定这是否是我的问题。
以下是我正在使用的代码简介:
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
(等等 5 个文件,其中我以 --#{marker}-- 结束标记 -
我非常感谢您提供的任何帮助。我完全被难住了。其他注意事项 我正在使用 MacRuby,但并非所有 Gems 都可以使用它,特别是对于嵌入式 MacRuby,我在小型库方面取得了一些成功,但在 ActionMailer 方面却没有任何运气。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个朋友进来,我们一起解决了这个问题,这就是结果。
在电子邮件编码中,换行符非常重要。一些邮件服务器似乎更宽容(苹果的),这就是为什么我最初没有看到这个问题。
这是工作代码:
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:
我没有附件编码的经验,但我认为 7 位仍然是标准。
我建议使用一个可以为您完成所有这些操作的邮件库,例如 Mikel 的邮件库。重新发明轮子并没有多大用处,除非你只想学习发明轮子。
链接到 Mikel 的邮件库:http://github.com/mikel/mail
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