Routes:
post "/send_email_with_attachment", to: "articles#send_email_with_attachment"
-----------------------------------
Controller:
class ArticlesController < ApplicationController
def send_email_with_attachment
email = "arvind@gmail.com"
attachment = params[:attachment]
# Save the file temporarily
file_path = Rails.root.join('tmp', attachment.original_filename)
File.open(file_path, 'wb') do |file|
file.write(attachment.read)
end
MyMailer.send_email_with_attachment(email, file_path).deliver_now
# Delete the temporary file after sending the email
File.delete(file_path) if File.exist?(file_path)
redirect_to root_path, notice: 'Email sent successfully'
end
end
--------------------------
Mailer
class MyMailer < ApplicationMailer
def send_email_with_attachment(email, attachment_path)
@greeting = "Hi"
attachments[File.basename(attachment_path)] = File.read(attachment_path)
mail(to: email, subject: 'Email with Attachment')
end
end
-------------------------------
No comments:
Post a Comment