Sendgrid GEM bug while sending plain/text e-mail - ruby-on-rails

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.

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

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!

Using Google Mail Actions

So I've been reading about the little action icons in Google Mail and I'm trying to set this up inside Rails 3. I have the template, I have the message itself, I have SMTP set up to Google Mail as well.
According to this article:
http://googleappsdeveloper.blogspot.com/2013/05/introducing-actions-in-inbox-powered-by.html
(read the comments) the only way to test this out is to send the messages from you to you (the sender and recipient have to be the same). Unfortunately, it looks like you still have to DKIM or SPF validate the message as well.
So here's the question: Has anybody been able to test this inside a dev environment? When I use GMail SMTP, I don't get a DKIM signature so it won't show the action icon on the far right.
Message Body:
Received: -
Date: Sat, 22 Feb 2014 15:25:45 -0800
From: me
To: me
Message-ID: <530931f945de6_17c563fc73085e6e86978c#BigMac.local.mail>
Subject: Testing Gmail action Schemas :
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_530931f943db6_17c563fc73085e6e869643";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_530931f943db6_17c563fc73085e6e869643
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
EMAIL CONTENT GOES HERE
----==_mimepart_530931f943db6_17c563fc73085e6e869643
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<header>
<div itemscope itemtype="http://schema.org/EmailMessage">
<meta itemprop="description" content="Click here to view your ticket."/>
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
<link itemprop="url" href="https://support.und0.com/tickets/1"/>
<meta itemprop="name" content="View ticket"/>
</div>
</div>
</header>
<body>
<div>EMAIL CONTENT GOES HERE</div>
</body>
</html>
----==_mimepart_530931f943db6_17c563fc73085e6e869643--

ActionMailer responds with "501 5.5.4 Invalid argument"

I'm working on an application with Rails 4 on Ruby 2.0.0. The application sends out an email after a registration in Devise.
This is the code that sends the email:
app/models/sponsor.rb:
after_create :send_email_to_admin
private
def send_email_to_admin
AdminMailer.new_sponsor_email(self).deliver
end
app/mailers/admin_mailer.rb
class AdminMailer < ActionMailer::Base
default to: '**removed**'
def new_sponsor_email(sponsor)
#sponsor = sponsor
p #sponsor
mail(subject: "New Sponsor Registration")
end
end
And this is the generated email from the log file:
Sent mail to **removed** (725.5ms)
Date: Mon, 02 Sep 2013 15:01:03 -0400
From: **removed**
To: **removed**
Message-ID: <5224e06f4dddd_2e5a3fa0452dcfd874597#centaur.mail>
Subject: New Sponsor Registration
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5224e06f4cca1_2e5a3fa0452dcfd87441a";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5224e06f4cca1_2e5a3fa0452dcfd87441a
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
A new sponsor has signed up!
==========================
----==_mimepart_5224e06f4cca1_2e5a3fa0452dcfd87441a
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html lang='en'>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
</head>
<body>
<h1>A new sponsor has signed up!</h1>
</body>
</html>
----==_mimepart_5224e06f4cca1_2e5a3fa0452dcfd87441a--
when I try to test the code by creating a sponsor, I just get this error:
Net::SMTPSyntaxError in Devise::RegistrationsController#create
501 5.5.4 Invalid argument
My understanding is that this is typically because the email is and invalid email, but all of my emails are very simple, in the format of name#domain.tld and no-reply#domain.tld.
The issue seemed to be that I was using "domain" in my smtp_settings. When I removed that, I was able to send emails from both Mailgun and Gmail

Rails mailer and links not working under Thunderbird

I want to send html email's with my Rails Mailer.
Could be multipart/alternative or only text/html.
Problem is, when I send an email with a link, it appears well on Gmail, but not so good in Thunderbird.
The problem is, that in Thunderbird, the link is 'not clickable' - it's highlighted like a link, but clicking on it does nothing.
Links on other mails (not sent from Rails Mailer) work flawlessly.
So my question is: how to properly send an html email, so it will be viewed properly?
Mailer code:
class MyMailer < ActionMailer::Base
def mailing_delivery(email, subject)
mail(:to => email, :subject => subject)
end
Mail view:
# mailing_delivery.html.haml
%p
Thanks! Its your mailer!
%p
= link_to "Google", "google.com"
Source of email (some parts cut-out):
Date: Thu, 18 Aug 2011 14:32:34 +0200
From: xxx
To: xxx
Message-ID: <4e4d0662bf96e_41024be957a57974#xxx.mail>
Subject: Super news!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-O2-Trust: 2, 64
X-O2-SPF: neutral
<p>
Thanks! It's your mailer!
</p>
<p>
Google
</p>
Of course Rails 3. Thunderbird 5.0, but that's not the issue - because somehow other mails show up good.. And I don't think it's app specific.
What should I do, to make this work?
Edit: I tried to change the 'content-transfer-encoding' header with no success, by doing mail(:to => email, :subject => subject, "Content-Transfer-Encoding" => value) or in class default "Content-Transfer-Encoding" => value with no luck.. how to change that?
Emails that works good under Thunderbird have Content-Transfer-Encoding: quoted-printable (or eventually base64) so maybe that's the issue? How do I change it?
Edit 2: I managed to change the Content-Transfer-Encoding to quoted-printable but it's cutting the content, like this:
Date: Thu, 18 Aug 2011 22:04:21 +0200
From: xxx
To: xxx
Message-ID: <4e4d704557839_151c4e4957c2132e#xxx.mail>
Subject: Super news!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-O2-Trust: 2, 63
X-O2-SPF: neutral
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ww=
w.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>
Thanks! It's your mailer!
</p>
<p>
<a href=
One other thing: why is there a newline in Content-Type ? I didn't see it in other (working good) mails. How I can get rid of it?
Maybe it'll work if you fix your html. So instead of just having:
<p>
Thanks! It's your mailer!
</p>
<p>
Google
</p>
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>
Thanks! It's your mailer!
</p>
<p>
Google
</p>
</body>
</html>

Resources