Unable to send attachment, and dealing with multiple attachment

Hi,

the following function to mail with an attachment is not working, mail
is going but Its not attaching the file:

send_mail(file_attach, mail_to, password, to, from)

filename = file_attach
filecontent = File.read(filename)
encodedcontent = [filecontent].pack(“m”) # base64
marker = “AUNIQUEMARKER”
body =<<EOF
Mail body

Thank you,
Proggrammer
EOF

part1 =<<EOF
From: #{from}
To: #{to}
Subject: test subject
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
–#{marker}
EOF

part2 =<<EOF
#{body}
EOF

part3 =<<EOF
Content-Type: multipart/mixed; name="#{filename}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=“#{filename}”
#{encodedcontent}
–#{marker}–
EOF

mailtext = part1 + part2 + part3

begin
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start(‘smtp.gmail.com’, 587, ‘gmail.com’, from, password,
:login) do |smtp|
smtp.send_message(mailtext, from, mail_to)
end
rescue Exception => e
print "Mailing exception occured: " + e
end
end

After this, how can I send multiple attachment, suppose files to attach
are in array ‘a_array’?

OR EASIEAST WAY TO SEND MULTIPLE ATTACHMENT TO MULTIPLE EMAILS?

On Thursday, 18 October 2012 at 12:21 AM, ajay paswan wrote:

Hi,

the following function to mail with an attachment is not working, mail
is going but Its not attaching the file:

You appear to be missing a marker somewhere. Why not use the mail gem[1]
and let it handle this for you?

mail = Mail.new do
from from
to to
subject ‘test subject’
body <<-BODY
Mail body

Thank you,
Programmer
BODY
add_file :filename => filename, :content => non_encoded_content
end

Now just write mail.to_s instead of mailtext per your example.

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

This is dangerous. Someone could intercept the connection, and possibly
steal your data.

If you obtain cacert.pem from a trusted location (e.g. [2]), you can
configure Net::SMTP accordingly:

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_PEER,
File.join(File.dirname(FILE), “…”, “cacert.pem”))

After this, how can I send multiple attachment, suppose files to attach
are in array ‘a_array’?

With my above example, just call add_file multiple times, e.g. if
a_array was an array of paths:


a_array.each do |attachment|
add_file :filename => File.basename(attachment[:filename]), :content
=> File.read(attachment[:filename])
end

Cheers,

Arlen

[1] GitHub - mikel/mail: A Really Ruby Mail Library
[2] http://curl.haxx.se/ca/cacert.pem

If you obtain cacert.pem from a trusted location (e.g. [2]), you can configure
Net::SMTP accordingly:

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_PEER,
File.join(File.dirname(FILE), “…”, “cacert.pem”))
Sorry, the path to cacert.pem in the Ruby was contrived; can you tell
I’m copying from my own code?

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_PEER,
File.join(File.dirname(FILE), “cacert.pem”))

if in the same directory.

is it compatible with ruby 1.7.8?
Do you mean 1.7.8 or 1.8.7? It’s tested as working on 1.8.7 [1].
Gem::Specification (NilClass instead).
[/usr/lib/ruby/gems/1.8/specifications/mime-types-1.19.gemspec] isn’t a
Gem::Specification (NilClass instead).
ERROR: While executing gem … (ArgumentError)
marshal data too short

I’m not familiar with this; perhaps someone else is familiar.

[1] Home – Travis-CI

1.8.7

how to send multiple attachment using net/smtp?

You appear to be missing a marker somewhere. Why not use the mail gem[1]
and let it handle this for you?
is it compatible with ruby 1.7.8?

and if I try to install mail gem it gives error:

sudo gem install mail
[/usr/lib/ruby/gems/1.8/specifications/treetop-1.4.11.gemspec] isn’t a
Gem::Specification (NilClass instead).
[/usr/lib/ruby/gems/1.8/specifications/i18n-0.6.1.gemspec] isn’t a
Gem::Specification (NilClass instead).
[/usr/lib/ruby/gems/1.8/specifications/polyglot-0.3.3.gemspec] isn’t a
Gem::Specification (NilClass instead).
[/usr/lib/ruby/gems/1.8/specifications/mail-2.4.4.gemspec] isn’t a
Gem::Specification (NilClass instead).
[/usr/lib/ruby/gems/1.8/specifications/mime-types-1.19.gemspec] isn’t a
Gem::Specification (NilClass instead).
ERROR: While executing gem … (ArgumentError)
marshal data too short

On Oct 18, 2012, at 23:22 , ajay paswan [email protected] wrote:

ajay paswan wrote in post #1080283:

how to send multiple attachment using net/smtp?

multiple attachment?

While we’re here to help, we’re not here to do everything for you.

There are 50k hits on google against your exact question:

ruby how to send multiple attachment using net/smtp - Google Search

Try it… you might like it.

ajay paswan wrote in post #1080283:

how to send multiple attachment using net/smtp?

multiple attachment?

Ryan D. wrote in post #1080440:

On Oct 18, 2012, at 23:22 , ajay paswan [email protected] wrote:

ajay paswan wrote in post #1080283:

how to send multiple attachment using net/smtp?

multiple attachment?

While we’re here to help, we’re not here to do everything for you.

There are 50k hits on google against your exact question:

https://www.google.com/search?q=ruby+how+to+send+multiple+attachment+using+net/smtp

Try it… you might like it.

I can see those are dealt with single attachment, I am not getting this
for multiple attachments. Am sorry.

part3 =<<EOF
Content-Type: multipart/mixed; name="#{filename}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=“#{filename}”
#{encodedcontent}
–#{marker}–
EOF
#this code I modified still not working, filename2 is assigned properly
part4=<<EOF
Content-Type: multipart/mixed; name="#{filename2}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=“#{filename2}”
#{encodedcontentt}
–#{marker}–
EOF
mailtext = part1 + part2 + part3 +part4

begin
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start(‘smtp.gmail.com’, 587, ‘gmail.com’, from, password,
:login) do |smtp|