Attachment not sent properly in rails using ActionMailer - ruby-on-rails

I am trying to send xls file via ActionMailer.
mailer = ActionMailer::Base.mail(:to => "reciever#gmail.com", :from => "sender#gmail.com", :content_type=>"application/vnd.ms-excel", :body=> '')
mailer.attachments['filename.xls']= {mime_type: 'application/vnd.ms-excel', content: File.read("filePath.xls")}
mailer.deliver!
I am able to receive the mail as well.
But somehow the attachment is not correct, it shows up as noname and below is the content I get in the file (I am copy pasting the exact contents)
--
Date: Wed, 04 Jun 2014 23:33:48 +0530
Mime-Version: 1.0
Content-Type: application/vnd.ms-excel;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=filename.xls
Content-ID: <538f5f82836992#C02L2178FFT3.gmail>
PGgzIHN0eWxlPSJmb250LXdlaWdodDpib2xkIj4gCiAgICBTaG93aW5nCiAg
ICBvcGVuCiAgICByZXF1ZXN0cwogICAgZnJvbQogICAgTm92IDIxLCAyMDEz
....
I am sure I am missing something simple, I am unable to figure out what. Can someone help?

Try this:--
mailer = ActionMailer::Base.mail(:to => "reciever#gmail.com", :from => "sender#gmail.com", :content_type=>"application/vnd.ms-excel", :body=> '')
mailer.attachments["filename.xls"]= File.read("filePath.xls")
mailer.deliver!

Related

Attempt to send an email via Rails ActionMailer results in a failed call to `normalize_name` inside `lookup_context.rb`

I have a Mailer class which inherits from ApplicationMailer, which in turn inherits from ActionMailer::Base. Ruby version is ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15].
The Mailer class looks like the following:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['me#example.com'],
RM: ['me#example.com']
}
def daily_report_email(facility)
#facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
#delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
#partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
#late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
#expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
ApplicationMailer looks like the following:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'notify#example.com'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
The view is located at app/views/purchase_order_status_mailer/daily_report_email.html.erb.
When I open my Rails console and type PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver, I see the following error:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/me/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionview-5.0.4/lib/action_view/lookup_context.rb:215:in `normalize_name'
I tried passing a format block to the call to the mail helper with the same call, like so:
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}") do |format|
format.text { render plain: "Hey!" }
end
The above produced the following response, which appears to represent a successful email send:
Rendering text template
Rendered text template (0.0ms)
Sent mail to me#example.com (8.8ms)
Date: Mon, 25 Sep 2017 12:55:11 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hey!
=> #<Mail::Message:70191029273300, Multipart: false, Headers: <Date: Mon, 25 Sep 2017 12:55:11 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
I didn't actually receive an email, and I assume that means I don't have SMTP set up on my local machine, but the above response is encouraging. There was no stack trace apart from the error I posted above, so I tried digging into the Rails source code, and I saw that normalize_name inside lookup_context.rb gets called from within the args_for_lookup protected method, which in turn gets called by the ViewPaths module's find_all method. But beyond that it was hard to trace the call stack, as I couldn't find who the caller of find_all is.
My question is: what is wrong with the first call to mail?
EDIT 1: I also tried format.html { render html: "<h1>Hello Mikel!</h1>".html_safe } instead of the format.text option, as per the example here, and I got a similar success message.
I then tried adding a byebug statement inside normalize_name, to try and identify what the values of the parameters were on the successful email sends, but it looks like this method isn't getting called when a block is passed. That makes me suspect even more strongly that the problem is somehow related to my views. But I can't confirm that yet.
The issue is here
PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver
remove the new i.e
PurchaseOrderStatusMailer.daily_report_email('JC').deliver
Because of the wrong object of its not able to deduce the template path/file
I was able to generate a success response (including my mailer's rendered template file) by passing template_path and template_name options to the original call to the mail helper, like so:
mail(
to: CONTACTS[facility.to_sym],
subject: "#{facility} Daily Receipt Status - #{Date.today}",
template_path: 'purchase_order_status_mailer',
template_name: 'daily_report_email')
This generated the following success response:
Rendering purchase_order_status_mailer/daily_report_email.text.erb
Rendered purchase_order_status_mailer/daily_report_email.text.erb (0.3ms)
Sent mail to me#example.com (12.1ms)
Date: Mon, 25 Sep 2017 15:18:25 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59c9568120033_11e943fee8503f8282341e
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Please find the receipt status for JC as of 09-25-2017:
=================================================================================================
...<lots more content, including ERB rendered into HTML>...
=> #<Mail::Message:70293683934580, Multipart: true, Headers: <Date: Mon, 25 Sep 2017 15:18:25 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
I'm a little confused about why I had to add these options, as the documentation doesn't make it clear that these are required and my templates and folders appear to be named correctly, but it worked and I have deadlines so I'm moving on. :-)

Sending mail with embedded image in Ruby

I want to send a mail with an attached image and embed this image in html. As a source for tag , What should I write and is there any missing header parts such a content_type ?
When I am doing real test, I send to a mail to my gmail account. I can see there is an attachment with name "unnamed" and I could not display image even when I download.
Gem: Pony
require 'pony'
Pony.override_options = { :via => :test }
Pony.mail(
:to => 'foo#bar',
:from => 'abc#abc.com',
:subject => 'test123',
:body => "<h1><strong>Dear X, check the newsletter ,<br></strong></h1> <img src='attached image' />",
:attachments => {"happy_image.jpg" => File.read("happy_image.jpg")}
)
puts Mail::TestMailer.deliveries
output:
Date: Tue, 06 Dec 2016 17:23:05 +0300
From: abc#abc.com
To: foo#bar
Message-ID: <5846c9ca183d6_5983c9fd899060#MW7BIQ12TKYHQC.mail>
Subject: test123
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="--==_mimepart_5846c9caf15a_5983c9fd8989e5";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5846c9caf15a_5983c9fd8989e5
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<h1><strong>Dear X, check the newsletter ,<br></strong></h1> <img src='' />
----==_mimepart_5846c9caf15a_5983c9fd8989e5
Content-Type: image/jpeg;
filename=happy_image.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=happy_image.jpg
Content-ID: <happy_image.jpg#MW7BIQ12TKYHQC>
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQE
BQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/
2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU
FBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAHiAuMDASIAAhEBAxEB/8QA
HwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUF
BAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK
FhcYGQ==
----==_mimepart_5846c9caf15a_5983c9fd8989e5--
You should be able to BASE64 encode the image, and use the resulting string as the src of the img tag.
For example:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWQAAAD8CAYAAAB..."/>
Also make sure that your content type is set as text/html instead of text/plain. Looking at the mail, it seems that it's set to the latter, hence why images don't render and tags render as text. Add the following to your arguments to use text/html content type:
:headers => { 'Content-Type' => 'text/html' }

Rails Mailer issue with sending attchemnt over mail

I am migrating an application from Rails 2.3 to Rails 3.1, the emails are not working, when i send an email with attachment i see a plain/text email with the encoded pdf content in the email instead of as an attachment.
here is the command i used to send email
ret = UserMailer.return_forms(#customer[:email], #store, id, #customer[:document]).deliver
here is the definition of returns_form, the return_forms is a method in UserMailer class (class UserMailer < ActionMailer::Base)
def return_forms(email, store, order, pdf_document_path)
load_smtp_settings("noreply")
#recipients = email
#subject = "#{business_name}: Return forms"
#body = "Please follow the instructions within the forms to return your merchandise. Thank You."
attachments['free_book.pdf'] = {mime_type: 'application/pdf',content: File.read(pdf_document_path) }
#from = email
#date = Time.zone.now
#headers = {}
end
I see the content as
--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <55c881028af96_48d43fe94782e9a0658aa#amol-desktop.mail>
Please follow the instructions within the forms to return your merchandise. Thank You.
--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: application/pdf;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=free_book.pdf
Content-ID: <55c88102872ba_48d43fe94782e9a0657a0#amol-desktop.mail>
JVBERi0xLjQKJcfsj6IKNyAwIG9iago8PC9MZW5ndGggOCAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nH1W227bOBB991cM0AWqABZLUvc8
bTZNivTiuLUX2GKzD6xE22pk0aWktOnX75CSbUW+xEhkDufMGc4cjvIDKGFA
zad7puvRmy8JLKuRtYJejnyfghd5MQRBSIGjTY4W1srDkB+xeh4HHocQ92yM
clxzr7NFLD6w7fx6WOtn4gWhD2ewjEfeaZIeuHW0GUYxg3Nwn/rhGaI+vnVt
8wzhHN4PqX+GqQ+3njbTxIez8DhOzhD14dZzUM+j8MD3zxH14K3nIOZReEjp
The return_forms call should have a call for mail() method at the end. Something like:
mail(:from => your_from, :to => your_to, :subject => your_subject, :body => your_body)
It is also a good idea to enable mailer errors in your development.rb to see if any errors appear:
config.action_mailer.raise_delivery_errors = true

Amazon SES email delivery problems

I have a rails application that is using Amazon SES service to send information emails to customers. In development environment this email functionality is working ok. However, when I run my application in my EC2 instance the emails are not sent anymore.
I have checked the logs, and everything looks good:
Rendered email_service/send_booking_request_customer_notification.html.erb (7.6ms)
Sent mail to cliente.yanpy#gmail.com (25.8ms)
Date: Mon, 03 Nov 2014 11:17:57 +0000
From: Yanpy <business#yanpy.com>
To: cliente.yanpy#gmail.com
Message-ID: <5457646572252_1be3fd803f643b45382f#ip-172-31-20-213.mail>
Subject: Nueva solicitud de reserva [MMSGORDH].
Mime-Version: 1.0
Content-Type: multipart/related;
boundary="--==_mimepart_5457646554c90_1be3fd803f643b4536a1";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5457646554c90_1be3fd803f643b4536a1
Content-Type: multipart/alternative;
boundary="--==_mimepart_545764656fbba_1be3fd803f643b4537bb";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_545764656fbba_1be3fd803f643b4537bb
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
[text-content]
----==_mimepart_545764656fbba_1be3fd803f643b4537bb
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
[html-content]
----==_mimepart_545764656fbba_1be3fd803f643b4537bb--
----==_mimepart_5457646554c90_1be3fd803f643b4536a1
Content-Type: image/png;
charset=UTF-8;
filename=logo.png
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=logo.png
Content-ID: <545764654ea73_1be3fd803f643b4535ba#ip-172-31-20-213.mail>
iVBORw0KGgoAAAANSUhEUgAAAWAAAAB5CAYAAAAHz/urAAAACXBIWXMAAAsT
AAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVN3...
----==_mimepart_5457646554c90_1be3fd803f643b4536a1--
I have checked business#yanpy.com is a verified sender email address. I even have sent a test email from SES console from business#yanpy.com to cliente.yanpy#gmail.com and the email is sent and received.
However, when I send these emails programatically, everything looks good, but the emails are not received.
I fixed it. I have realized I was missing my smtp configuration for my test environment in Rails.
I added:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "email-smtp.eu-west-1.amazonaws.com",
:user_name => "************", # Your SMTP user here.
:password => "*************", # Your SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
to my environments/test.rb config file in Rails and it worked.

Rails 3.0.17 sending inline attachments instead of regular ones

So, we have just upgraded our project to Rails 3.0.17 (please don't ask why this particular version, just happened so) on top of Ruby 1.8.7 enterprise edition.
I have methods to generate CSV data and send it as a file via email.
# contact_mailer.rb
def send_payments_report
attachments["report.csv"] = {
:content => Payment.generate_csv_report,
:mime_type => "text/csv"
}
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
)
end
Here's what it returns:
=> #<Mail::Message:2231891440, Multipart: true, Headers: <Date: Wed, 19 Dec 2012 11:48:05 +0200>, <From: from#example.com>, <To: to#example.com>, <Message-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>>, <Subject: Payments report>, <Mime-Version: 1.0>, <Content-Type: multipart/mixed; >, <Content-Transfer-Encoding: 7bit>>
The email itself is displayed as plain text without attachments:
--
Date: Wed, 19 Dec 2012 11:48:05 +0200
Mime-Version: 1.0
Content-Type: text/csv;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=report.csv
Content-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>
"customer id","reference nr","invoice id","invoice type","invoice sum","sum paid","payment date"
"1035","010294","7113","Fine","250.00","100.00","2012-11-15"
"1035","010294","6132","Fine","250.00","250.00","2012-11-15"
"1035","010294","5241","Fine","850.00","650.00","2012-11-15"
--
I tried attaching existing file, but it displays it the same way. It worked perfectly on Rails 2.3.9. Can anyone help on that?
I seem to have solved this problem.
Here how I call the mail method:
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
) do |format|
format.text {render :text => ""}
end

Resources