How to include a link in your email to your attached csv? - ruby-on-rails

I want to include a link at the bottom of the email that a user can click and the attached csv will automatically be downloaded. Something like this:
Best,
Nick
orders.csv
In my mailer I have this:
def order_summary_sent(params)
attachments.inline["orders.csv"] = { mime_type: "text/csv", content: params[:csv_content] }
mail(subject: "Order summary", to: params["recipient"])
The file is successfully shown as an attachment but what erb code should I write to also include it directly in the email body?
I tried the following:
<%= link_to "orders.csv", attachments["orders.csv"].url %>
However, when I click on the link I get cid:635a746039b8b_213558301d83608a#nick.mail

Related

How can I add a download link to files in my Digitalocean Space in my Rails app?

I'm trying to create a download link/button in my Rails app so that users can download files from my Digitalocean Space. I want to be able to click a download button on my show page and download the file. Here is what I've tried s far:
samples_controller.rb
def show
#sample = Sample.find(params[:id])
data = open(#sample.audio_url)
#sample_download = send_data data.read, filename: #sample.audio.metadata["filename"], type: #sample.audio.metadata["mime_type"], disposition: 'inline'
end
show.html.erb
<%= link_to "Download Sample", #sample_download %>
When I load the show page this opens a page that plays the audio file. What I want is to see my show template as normal and be bale to click a link to download the file. Maybe I should have a separate download_sample method, I'm not sure.
You can trigger download on browser side using the download attribute on an <a> tag.
All you need is something like <a href="http://linktofile.com/audio.mp3" download>Download</a> and user can click on the link to download.
Using the Rails helper, you can do <%= link_to "Download", #sample.audio_url, download: true %>
Hi for content from another website make sure the link is a direct link.
If the data you want to download is from active storage take a look at rails_blob_path (here)
.
In your model
Class Sample < ApplicationRecord
has_one_attached :data
end
In your controller
class SampleController < ApplicationController
def show
#sample = Sample.find(params[:id])
#sample_download = rails_blob_path(#sample.data, disposition: 'attachment')
end
end
In your views
<%=link_to "Download", #sample_download %>

Add link to `Add to Calendar` for ics file in erb email

I've created a confirmation email using ActionMailer in Rails. My method for creating the email is as follows:
def session_confirmation_email_client(user, coach, date, session_ics, session_outlook, google_cal)
#google_cal = google_cal
#user = user
#date = format_time(date.getlocal)
#sender = "#{SENDER}"
mail.attachments["session.ics"] = { mime_type: 'text/calendar', content: session_ics }
mail(to: user.email, subject: 'Session Confirmation')
end
This properly adds the session.ics attachment to the email but now I want to include an Add To Calendar button which will download the attachment on click.
My erb template for the same looks as follows, at the moment:
<td align="right" valign="top">
<%= link_to image_tag("https://s3.amazonaws.com/ical.svg"), LINK_TO_ATTACHMENT %>
</td>
I'm not sure what link to add over here? Is this way wrong? Basically I'm not sure how to add a download link for an email attachment
When you attach an event in an email, you can't create a button inside the email that invokes the attached event.
When an event is attached to the email, most email clients do a great job detecting the attached event and provides an "Add to Calendar" link themselves.
In case you still want a backup "Add to Calendar" link in the email, then I would recommend that you use a solution like the "Direct URL Method" from AddEvent.com - https://www.addevent.com/api/direct-url-method. (I'm behind AddEvent btw).
Most of our users who attach an event in emails use a mix. They attach an event using their Mail API and include "Add to Calendar" links in the email as well. Both can be accomplished using the "Direct URL Method".

Rails send_data doesn't save file

I'm using Nokogiri to generate an XML file in my app. I want to save this file, and I want to display a dialog box in which the user can select the folder in which download this file.
This is the action in my controller:
def download
require 'nokogiri'
if owner_signed_in?
#slips = current_owner.slips
builder = Nokogiri::XML::Builder.new do |xml|
xml.cedolini{
#slips.each do |slip|
xml.cedolino{
xml.codicecliente_ slip.client_code
xml.data_ slip.day.to_s
xml.ordini{
slip.product_slips.each do |order|
xml.ordine {
xml.codicearticolo_ order.product_code
xml.descrizionearticolo_ order.product_description
xml.ammontare_ order.amount.to_s
}
end
}
}
end
}
end
file = builder.to_xml
send_data file, :type => 'text/xml; charset=UTF-8;', :disposition => "attachment; filename=db.xml"
end
end
I have the route defined in this way:
get '/dbsinc/download'
When I call the action from the view, it doesn't save the XML, I see a new page with the url of my action, and in the page I see the XML file rendered on the page, but it doesn't open any dialog box to save the file.
Where am I getting wrong? Thanks
I think I figured out, route: `post 'dbsinc/download'
And in my view I defined the link in this way:
<%= link_to 'Download ', {controller:'dbsinc', action:'download'}, method: :post %>
And it works, the download dialog opens when I click on the link.

Action Mailer attachments.inline showing as normal attachment, not inline

def simple_mail(to, subject, body)
attachments.inline['Logo.png'] = File.read( Rails.root.join("public", "Logo.png"))
mail(:to => to, :subject => subject, :body => body)
end
I receive an attachment to the message, but it is not an "inline" image. How can I attach inline, and how can I choose where it goes?
I'm seeing examples that have views for their mailer.. looks like I may need to set those up. If so, how do I link Action Mailer views to a message?
As you see in guides.rubyonrails, you can link a view to your mailer the same way you link a view to a controller's action. if your mailer is called my_mailer, and your mailer action is called simple_mail, then create a view in app/views/my_mailer/ and call it "simple_mail.html.erb". As well, you can create a "simple_mail.text.erb" to make sure that if the recipient can't receive HTML e-mails, they get the plaintext version (see action_mailer_basics.html below).
As for inline attachments, a different question was raised previously regarding views (see this link) and scaney answered with a couple links. Check out this link and search the text for "inline" and you should find the answer you're looking for!
http://guides.rubyonrails.org/action_mailer_basics.html
api.rubyonrails.org/classes/ActionMailer/Base.html
1) To Setup action-mailer views for the mails, you can do it as follows:
Create a view file in app/views/mailer_name/ with the same name as your mailer action (which in this case is "simple_mail" )
Alternatively if you want to use a different view altogether you can also do it as :
def simple_mail(to, subject, body)
attachments.inline['Logo.png']=File.read( Rails.root.join("public", "Logo.png"))
mail(:to => to, :subject => subject, :body => body) do |format|
format.html { render :partial => "/path/to/partial"}
end
end
In the above case, the content of the partial will be rendered as the body of the mail.
2) To attach a inline image you will also need to mention the attachment in the mailer action view file as follows:
<%= image_tag attachments['Logo.png'].url %>

What is the right way to embed image into email using Rails?

What is the right way to embed an image into email using Rails?
I combined the answer from Oksana with a custom helper approach and got the following to work quite nicely.
app/helpers/email_helper.rb
module EmailHelper
def email_image_tag(image, **options)
attachments[image] = File.read(Rails.root.join("app/assets/images/#{image}"))
image_tag attachments[image].url, **options
end
end
app/mailers/base_mailer.rb
class BaseMailer < ActionMailer::Base
helper(EmailHelper)
end
app/mailers/my_mailer.rb
class MyMailer < BaseMailer
def send_my_mail(email)
mail to: email, subject: "My Subject"
end
end
Then for example where I want to attach the company logo in my email layout file I would use
app/views/layouts/email.html.erb
<%= email_image_tag("company_logo.png") %>
Note the **options makes the tag more extensible but it will only work in ruby >=2. To make this work in ruby < 2 you will have to use the older way of handling key word options.
Update 03/25/2022: Rails 6 no longer supports add_template_helper, and now replaces it with helper, as explained by this answer that links to the commit that did so.
RAILS 5
In your mail method add your inline attachment pointing to your image:
class ConfirmationMailer < ActionMailer::Base
def confirmation_email
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
mail(to: email, subject: 'test subject')
end
end
Then in your mail html view an image_tag with the attachment url:
<%= image_tag(attachments['logo.png'].url) %>
Adding onto Oksana and tdubs' answers
The module tdubs wrote works on desktop, but for the mobile gmail client, the images appeared as attachments. To fix this, do this for the
app/helpers/email_helper.rb
module EmailHelper
def email_image_tag(image, **options)
attachments[image] = {
:data => File.read(Rails.root.join("app/assets/images/emails/#{image}")),
:mime_type => "image/png",
:encoding => "base64"
}
image_tag attachments[image].url, **options
end
end
For the rest, follow tdubs's answer.
After a lot of research i have found very cleaner way to embed image in email.
Just add following line in production.rb and development.rb
config.action_mailer.asset_host = 'YOUR HOST URL'
In your view embed image by using following code.
<%= image_tag('My Web Site Logo.png') %>
Note: Make sure to update YOUR HOST URL and My Web Site Logo.png in
above code.
For basic details of usage of Action Mailer, please refer to ActionMailer::Base.
Copy pasted from here
http://api.rubyonrails.org/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Inline+Attachments
Inline Attachments
You can also specify that a file should be displayed inline with other HTML. This is useful if you want to display a corporate logo or a photo.
class Notifier < ApplicationMailer
def welcome(recipient)
attachments.inline['photo.png'] = File.read('path/to/photo.png')
mail(to: recipient, subject: "Here is what we look like")
end
end
And then to reference the image in the view, you create a welcome.html.erb file and make a call to image_tag passing in the attachment you want to display and then call url on the attachment to get the relative content id path for the image source:
<h1>Please Don't Cringe</h1>
<%= image_tag attachments['photo.png'].url -%>
As we are using Action View's image_tag method, you can pass in any other options you want:
<h1>Please Don't Cringe</h1>
<%= image_tag attachments['photo.png'].url, alt: 'Our Photo', class: 'photo' -%>
In rails 6, if you use the solution of #Tyron, you'll need to replace add_template_helper with helper in the BaseMailer. So, it becomes:
class BaseMailer < ActionMailer::Base
helper(EmailHelper)
end
I don't know much about rails, but I've worked on projects in C# that create emails and then insert them in a users inbox via the Google APIs. To create the email, I had to generate the email string from scratch. If you enable multipart for the email, then the image bytes will be included in a multipart section using base64 encoding.
You may want to check out the TMail and RubyMail packages to see if they support these actions for you.

Resources