Rails 3.0.17 sending inline attachments instead of regular ones - ruby-on-rails

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

Related

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

Adding bcc malfunctioning - Rails

I just came across a problem, spent hours but couldn't found root cause.
Mail sending works fine if I don't add bcc option but when I add bcc,
def test_email
mail :to => 'recipient#xyz.com',
:subject => 'Sample test',
:bcc => ['bcc#xyz.com']
end
It sends email to none and adds recipient#xyz.com as bcc.
Here is console output,
Mailer.test_email.deliver
#<Mail::Message:63045380, Multipart: true, Headers: <Date: Mon, 02 Jun 2014 13:03:02 +0000>, <From: hostmail#sample.dk>, <To: >, <Cc: >, <Bcc: ["recipient#xyz.com"]>, <Message-ID: <538c760686568_45b9a1001872969#DANVA-test-NAGIOS-monitor.mail>>, <Subject: Sample test>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_538c76063914_45b9a1001872662"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <X-Auto-Response-Suppress: OOF>, <Auto-Submitted: auto-generated>>
I am using Rails 3.2.8 and ruby 1.9.3p545.
I ideas to fix this prblem?

Attachment not sent properly in rails using ActionMailer

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!

Rails 3 ActionMailer - no recipent address

i have a problem with my prod actionmailer. I'm hosted on site5 sharing cloud. The problem is i get this error message.
A message that you sent using the -t command line option contained no
addresses that were not also on the command line, and were therefore
suppressed. This left no recipient addresses, and so no delivery could
be attempted.
------ This is a copy of your message, including all the headers. ------
Date: Sat, 17 Sep 2011 21:06:42 +0000
From: noreply#mydomain.com
To: myemail#mydomain.com
Message-ID: <4e750be27b3ef_4a9c159402ee91b039763#themailserver.com>
Subject: test
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
i added default
default :recipient => 'noreply#mydomain.com'
but the mail return me the same error.
my ActionMailer controller
class Emailtousers < ActionMailer::Base
default :from => "noreply#mydomain.com"
default :recipient => "noreplay#mydomain.com"
def plan_notification(resource)
#resource = resource
puts resource
mail(:to => "myemail#mydomain.com", :subject => 'test')
end
end
What i missed?
i followed this to help me http://edgeguides.rubyonrails.org/action_mailer_basics.html
I got this problem using the :sendmail delivery method with exim. Adding this to config/application.rb fixed it for me:
config.action_mailer.sendmail_settings = { :arguments => "-i" }
I found the solution at https://github.com/mikel/mail/issues/70#issuecomment-2639987

Resources