How to send all files in a particular folder as email attachments using ruby

i am able to send one file as mail attachment using the below code.

can someone tell how to send more than one file as mail attachment

require ‘net/smtp’

filename = “/tmp/test.txt”

Read a file and encode it into base64 format

filecontent = File.read(filename)
encodedcontent = [filecontent].pack(“m”) # base64

marker = “AUNIQUEMARKER”

body =<<EOF
This is a test email to send an attachement.
EOF

Define the main headers.

part1 =<<EOF
From: Private P. [email protected]
To: A Test User [email protected]
Subject: Sending Attachement
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="#{filename}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"

#{encodedcontent}
–#{marker}–
EOF

mailtext = part1 + part2 + part3

Let’s put our code in safe area

begin
Net::SMTP.start(‘localhost’) do |smtp|
smtp.sendmail(mailtext, ‘[email protected]’,
[‘[email protected]’])
end
rescue Exception => e
print "Exception occured: " + e
end

JANANI REDDY wrote in post #1183213:

i am able to send one file as mail attachment using the below code.

can someone tell how to send more than one file as mail attachment

I use Pony for send email :
https://github.com/benprew/pony

It resolve many problems encounter with low level smtp api.

See spec/pony_spec.rb for lot of exemples.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs