2016-12-02 250 views
1

我一直在试图弄清楚如何使用标准net/smtp将二进制附件发送到我的gmail帐户。到目前为止,我已经成功地安装成功的文本文件 - (基于做了什么别人)以下适用于这样的:使用ruby net/smtp发送带附件的电子邮件

#!/usr/bin/env ruby 

require 'net/smtp' 

addressee = '[email protected]' 
server = 'smtp.gmail.com' 
port  = 587 
account = 'ACCOUNT' 
from  = addressee 
name  = 'NAME' 
domain = 'gmail.com' 
subject = 'test of smtp using ruby' 
body  = 'Test of SMTP using Ruby.' 
marker = "PART_SEPARATOR" 
filename = "test-attachment" 
filetext = "attachment contents" 

print "Enter password for #{account}: " 
password = $stdin.gets.chomp 

# Define the main headers. 
part1 = <<EOF 
From: #{name} <#{from}> 
To: <#{addressee}> 
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: text/plain 
Content-Disposition: attachment; filename="#{File.basename(filename)}" 

#{filetext} 
--#{marker}-- 
EOF 

message = part1 + part2 + part3 

puts message 

smtp = Net::SMTP.new server, port 
smtp.enable_starttls 

smtp.start(domain, account, password, :login) do 
    smtp.send_message message, from, addressee 
end 

问题是与编码的二进制附件替换文本附件。上述下面的变化看起来应该基于什么我已经能够谷歌工作,但不能正确地发送附件:

#!/usr/bin/env ruby 

require 'net/smtp' 

addressee = '[email protected]' 
server = 'smtp.gmail.com' 
port  = 587 
account = 'ACCOUNT' 
from  = addressee 
name  = 'NAME' 
domain = 'gmail.com' 
subject = 'test of smtp using ruby' 
body  = 'Test of SMTP using Ruby.' 
marker = "PART_SEPARATOR" 
filename = "test-attachment" 
filetext = "attachment contents" 

print "Enter password for #{account}: " 
password = $stdin.gets.chomp 

# Encode contents into base64 format 
encodedcontent = [filetext].pack("m") 

# Define the main headers. 
part1 = <<EOF 
From: #{name} <#{from}> 
To: <#{addressee}> 
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: multipart/mixed; name="#{File.basename(filename)}" 
Content-Transfer-Encoding:base64 
Content-Disposition: attachment; filename="#{File.basename(filename)}" 

#{encodedcontent} 
--#{marker}-- 
EOF 

message = part1 + part2 + part3 

puts message 

smtp = Net::SMTP.new server, port 
smtp.enable_starttls 

smtp.start(domain, account, password, :login) do 
    smtp.send_message message, from, addressee 
end 

谁能告诉我什么,我做错了什么?

回答

0

我终于成功地发送二进制附件 - 秘密使用

Content-Type: application/octet-stream; name="#{filename}" 
Content-Disposition: attachment; filename="#{filename}"; size=#{size} 
在安装部

消息(第3部分)。

另一件事,这对一个小测试附件工作正常,但是当我尝试更大的(140K)附件时,附件被截断。使用

filecontent = File.binread(pathname) 

而不是

filecontent = File.read(pathname) 

似乎解决了问题。 (我不太清楚为什么。)

相关问题