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'?

--
Posted via http://www.ruby-forum.com/.

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

···

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

....

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|

....

···

--
Posted via http://www.ruby-forum.com/\.

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.

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

···

--
Posted via http://www.ruby-forum.com/\.

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].

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

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

[1] https://travis-ci.org/#!/mikel/mail/jobs/2730216

1.8.7

···

--
Posted via http://www.ruby-forum.com/.

how to send multiple attachment using net/smtp?

···

--
Posted via http://www.ruby-forum.com/.

ajay paswan wrote in post #1080283:

how to send multiple attachment using net/smtp?

multiple attachment?

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Oct 18, 2012, at 23:22 , ajay paswan <lists@ruby-forum.com> wrote:

ajay paswan wrote in post #1080283:

how to send multiple attachment using net/smtp?

multiple attachment?

Ryan Davis wrote in post #1080440:

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.

···

On Oct 18, 2012, at 23:22 , ajay paswan <lists@ruby-forum.com> wrote:

--
Posted via http://www.ruby-forum.com/\.