Skip to content

Instantly share code, notes, and snippets.

@ThorstenHans
Created July 18, 2011 12:28
Show Gist options
  • Save ThorstenHans/1089367 to your computer and use it in GitHub Desktop.
Save ThorstenHans/1089367 to your computer and use it in GitHub Desktop.
Rails2.x ActionMailer with attachments...
require 'net/smtp'
class DotNetRocksMailer
def self.send_email(from, to, subject, message, attachmentFileName, mailServer)
filename = attachmentFileName
filecontent = File.read(filename)
fileNameOnly = File.basename(attachmentFileName)
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: DataOne Build Master <[email protected]>
To: #{to}
Subject: #{subject}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF
# Define the message action
part2 =<<EOF
Content-Type: text/html
Content-Transfer-Encoding:8bit
#{message}
--#{marker}
EOF
# Define the attachment section
part3 =<<EOF
Content-Type: multipart/mixed; name=\"#{fileNameOnly}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{fileNameOnly}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
#http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/classes/Net/SMTP.html
Net::SMTP.start(mailServer) do |smtp|
smtp.send_message mailtext, from, to
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment