Sending mail with embedded image in Ruby - ruby-on-rails

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' }

Related

Sendgrid GEM bug while sending plain/text e-mail

Im tring to send an e-mail via the sendgrid gem for ruby on rails.
This is my code for the mailer:
def send_form(params)
#params=params
mail( :to => 'my_email',
:subject => 'my_subject',:content_type => "text/plain")
end
It is loading my view which is send_form.json.erb and in the view there is this:
<%= raw #params.to_json %>
Now, the problem here is that the source code from the e-mail has 2 emails(?), one with plain text and one with HTML.
This is a multi-part message in MIME format...
------------=_1448758618-20748-809
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
{"versao":"cee'd SW - 1.6 CRDi ISG EX","concessionario":"Sotabi","extraEmail":"gerencia#sotabi.p","nome":"Miguel Rebola","email":"miguelrebola21#gmail.com","telefone":"968823502","comentarios":"fdfd","autorizo":"yes"}
------------=_1448758618-20748-809
Content-Type: text/html; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
<html><body>
<p>{"versao":"cee'd SW – 1.6 CRDi ISG EX","concessionario":"Sotabi","extraEmail":"gerencia#sotabi.p","nome":"Miguel Rebola","email":"miguelrebola21#gmail.com","telefone":"968823502","comentarios":"fdfd","autorizo":"yes"}</p>
<img src="https://u1373375.ct.sendgrid.net/wf/open?upn=u0x3fSBqEx-2Fl-2BmgujqmE8BQomXBLbogF-2Bc6F-2BtlRZ6DF0HBXi526k5pl8ZUC-2FxtIp9ic36mMF2aQ4d6eJ5Lqq9panqdI3Ye7hmz4ajbhDO44ePVyM2SMDDBBWWMpJ5l50gYvbwVmo7S80MmO-2BlSAaFwiQpqgTXdMFRmz0vHxy-2BdXYgxbqCs5K3msoeQ4SQS-2BZqoqDpo2wVLTeQrEbKzxE2cQn1u-2FORF6fYLTNqw560g-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;"/>
</body></html>
I need to send only the plain/text. Any idea??
Needed to change the confgiuration on Sendrigd website.
Go to Settings https://app.sendgrid.com/settings/mail_settings
find Plain Content parameter and switch it on.

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.

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.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