Email attachments in ruby & rails

1. Add ActionMailer configuration in environment.
This configuration can be different for development and production.
# Include your application configuration below# You can set two configurations sendmail as well as smtp# To use SMTP you need to provide your email account credentials# Sendmail is a unix package that needs to be installed and configured while # using sendmail settings# Chances of getting emails into recipient's inbox are 100% for smtp settings# whereas sendmail needs some other configurations to be done before using.ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.default_content_type = "text/html"2. Create Mailer Model and add method to deliver emailclass EmailMailer 'text/html'} @sent_on = @recipients = email.recipients @from = email.from @cc = FEEDBACK_RECIPIENT @subject = email.subject @body = email.message # attach files files.each do |file| attachment "application/octet-stream" do |a| a.body = a.filename = file.original_filename end unless file.blank? end end3. Email Model# This is the virtual model in rails which has no database table associated with itclass Email < ActiveRecord::Base # It uses has_no_table plugin to create virtual model # This can also be done using following lines of code # # def self.columns() @columns ||= []; end # def self.column(name, sql_type = nil, default = nil, null = true) # columns < "You dont have Email ID, you cannot continue!", :unless => "call_id.blank?" validates_format_of :from, :with => /^([^@]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :unless => "from.blank?" validates_presence_of :recipients validates_format_of :recipients, :with => /^([^@]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :message => "Invalid email format", :unless => "recipients.blank?" validates_presence_of :subject, :messageend4. Usage from console or controller# Here email is the valid object of email model# attachments is the area of files to be attached with email# In my case attachments are of kind of pdf files# you can specify type of attachment in your mailer methodEmailMailer.deliver_email_with_attachements(email, attachments) if email.valid?Sending data stream as email attachments in rails.
In some cases, We need to dynamically generate files and you don’t want to store them locally on file system instead you always like to email them from memory itself.
Here is the way to do that.
1. Mailer data stream as attachment methodclass EmailMailer < ActionMailer::Base def email_with_data_stream(email, data_stream=[]) @headers = {} @sent_on = @recipients = email.recipients @from = email.from @subject = email.subject @body = email.message # attach files data_stream.each do |data| attachment "application/octet-stream" do |a| a.body = data[0] a.filename = data[1] end unless data_stream.blank? end end end2. Lets take example of pdf renderer.# Assume we have object of pdfattachements = []data = pdf.render# Attach as many files you wanted. Be careful about email maximum size ;)attachments << dataEmailMailer.deliver_email_with_data_stream(email, attachments)


About sandipransing

Web Developer #ruby #rails #JS
This entry was posted in Ruby. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s