Sendgrid template - ruby-on-rails

I am trying to send emails through using sendgrid template. But still sent, standard template.
def send_test_email(user)
#user = user
sendgrid_unique_args "filters" => {
templates" => {
"settings" => {
"enable" => 1,
"template_id" => "5e4a1ef6-a948-455f-b194-cec87ef88b0e"
}
}
}
mail( :to => #user.email,
:subject => 'Thanks for signing up for our amazing app' )
end
After sending
Sent mail to test#ya.net (699.1ms)
Date: Thu, 13 Aug 2015 10:00:04 +0300
From: example#example.com
To: test#ya.net
Message-ID: <55cc407412764_23922fba2642131f#Vlad.mail>
Subject: Thanks for signing up for our amazing app
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {"unique_args": {"filters": {"templates": {"settings":
{"enable":1,"template_id": "5e4a1ef6-a948-455f-b194-cec87ef88b0e"}}}}}
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Thanks for signing up, Jovani!</h1>
<p>Thanks for joining and have a great day! Now sign in and do
awesome things!</p>
</body>
</html>
How do I make that poisoned sendgrid template?

Your question does not clearly state the problem, but one thing I did notice is that your HTML does not have <%body%> which send grid needs in order to work with templates properly.

Related

Rails not sending emails when using a view

I am having an issue sending some emails, while others seem to work fine.
it seems like when i am using my views they are not sending, here is an a mail i have defined that just passes a body that sending just fine
def send_contact_email params
mail({from: params[:email],
to: 'inquiries#gmail.ca',
body: params[:message],
content_type: "text/html",
subject: "Inquiry from [#{ params[:name] }]"})
end
UserMailer.send_contact_email(params).deliver_now
here is what welcome looks like..this one does not work..
def welcome user, token
#resource = user
#token = token
mail({ to: user.email,
content_type: "text/html",
subject: "Thankyou!"})
end
UserMailer.welcome(u,hashed_token).deliver_now
here is what i get in the logs
UserMailer#welcome: processed outbound mail in 13.9ms
Sent mail to email#gmail.com (10364.1ms)
Date: Sat, 25 Nov 2017 19:39:19 +1100
From: no-reply#gmail.cl
To: email#gmail.com
Message-ID: <5a192c37982fc_a9663ff3cec3e51845790#mac.local.mail>
Subject: Gracias!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Project | Email Template</title>
</head>
<body style="margin: 0; padding: 0;">
<p>Hola email#gmail.com!</p>
<p>Se ha solicitado cambiar tu contrasena. Puedes hacerlo a traves del siguiente link.</p>
<button style="color:white; background:#FF5722; border:0; border-radius:0; padding:15px; font-size:16px;"><a style="color:white; text-decoration: none;" href="http://localhost:3000/users/password/edit?reset_password_token=a9d57947ffb8c077a2b58239d0c6afec91e7c7bb9424c36cb362d6682341c086">Cambiar contrasena</a></button>
<p>Si no lo solicito, ignore este correo electronico.</p>
<p>Su contrasena no cambiara hasta que acceda al link de arriba y cree una nueva.</p>
</body>
</html>
=> #<Mail::Message:70316446670780, Multipart: false, Headers: <Date: Sat, 25 Nov 2017 19:39:19 +1100>, <From: no-reply#gmail.cl>, <To: email#gmail.com>, <Message-ID: <5a192c37982fc_a9663ff3cec3e51845790#mac.local.mail>>, <Subject: Gracias!>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>
it appears as though the mail gets generated, I am calling deliver_now on the response, but it never hits the inbox via mailjet...could it be the content of the view that is some how keeping it from delivering?
I am using rails 4.2.8 and ruby 2.4.0
Thanks in advance!
EDIT this is my action_mailer config from development.rb
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.perform_deliveries = true
config.action_mailer.preview_path = "#{Rails.root}/test/mailers/previews"
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost', :port => 3000 }
config.action_mailer.delivery_method = :mailjet
config/initializers/mailjet.rb is defined with the key and secret for the API and gem 'mailjet' is in the Gemfile

No Template Found for Mailer

I've tried every possibility I can think of but just keep getting the same error when sending data through a form to a rails mailer. Any idea why I'm getting the "No template found for MailerController#sign_up" response at the bottom?
routes.rb
scope '/mailer' do
post '/signup' => 'mailer#sign_up'
end
app/controller/mailer_controller.rb
class MailerController < ApplicationController
def sign_up
#Create user object
#user = { first_name: params[:first_name],
last_name: params[:last_name],
email: params[:email],
package: params[:package_type],
username: params[:username],
followers: params[:followers],
age: params[:age],
hashtags: params[:hashtags],
comments: params[:comments]
}
AppMailer.sign_up(#user).deliver_now
end
end
app/mailers/app_mailer.rb
class AppMailer < ActionMailer::Base
default from: 'redacted_email'
def sign_up(user)
#user = user
mail(to: "redacted_email", from: #user['email'], subject: 'Sign Up | IAS')
end
end
app/views/app_mailer/sign_up.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<strong>First Name: </strong><p><%= #user[:first_name] %></p><br>
<strong>Last Name: </strong><p><%= #user[:last_name] %></p><br>
<strong>Email: </strong><p><%= #user[:email] %></p><br>
<strong>Package: </strong><p><%= #user[:package] %></p><br>
<strong>Username: </strong><p><%= #user[:username] %></p><br>
<strong>Current Followers: </strong><p><%= #user[:followers] %></p><br>
<strong>Age: </strong><p><%= #user[:age] %></p><br><br>
<strong>Hashtags: </strong><p><%= #user[:hashtags] %></p><br><br>
<strong>Comments: </strong><p><%= #user[:comments] %></p>
</body>
</html>
server response
Started POST "/mailer/signup" for ::1 at 2017-10-23 15:15:48 -0400
Processing by MailerController#sign_up as HTML
Parameters: {redacted but all going through successfully}
Rendering app_mailer/sign_up.html.erb
Rendered app_mailer/sign_up.html.erb (1.7ms)
Rendering app_mailer/sign_up.text.erb
Rendered app_mailer/sign_up.text.erb (0.6ms)
AppMailer#sign_up: processed outbound mail in 395.7ms
Sent mail to redacted#email.com (25.0ms)
Date: Mon, 23 Oct 2017 15:15:49 -0400
To: redacted#email.com
Message-ID: <59ee3fe573c8_17c8e3fcc17436908788c#Sephs-MBP-5.fios-router.home.mail>
Subject: Sign Up | IAS
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Mail unable to send. Please contact IAS Support.
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<strong>First Name: </strong><p>redacted</p><br>
<strong>Last Name: </strong><p>redacted</p><br>
<strong>Email: </strong><p>redacted</p><br>
<strong>Package: </strong><p>$25/mo | Basic</p><br>
<strong>Username: </strong><p>redacted</p><br>
<strong>Current Followers: </strong><p>58.5k</p><br>
<strong>Age: </strong><p>1</p><br><br>
<strong>Hashtags: </strong><p>hash tagsss</p><br><br>
<strong>Comments: </strong><p>commentssss</p>
</body>
</html>
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7--
No template found for MailerController#sign_up, rendering head :no_content
Completed 204 No Content in 484ms
Your issue is that you are missing the view for the controller not for the mailer. The mailer is working wonderfully. The issue is that you are sending the mail through an HTTP POST to the controller. After sending the request, it is not sure what it needs to respond to.
Is this a JS / JSON request to the controller? Adding something like:
render json: { status: 'ok' }
to the end of your action should solve the issue.

amazon ses ActionMailer ruby on rails debug

I followed the recommendation here from Sujoy Gupta to use smtp to send emails on rails. I get no error on the console but the emails never seem to have reached ses: The statistics there show no email sent/received and I never got them in my inbox either.
I added this to my config/environments/development.rb/test.rb/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-west-2.amazonaws.com",
:port => 587,
:user_name => "...", # My SMTP user here.
:password => "...", # My SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
and app/mailers/UserMailer.rb:
class UserMailer < ApplicationMailer
default from: "user#hotmail.com"
def test(email)
mail(to: email, subject: 'ses test')
end
end
Here is what printed in the console:
UserMailer.test("user#hotmail.com").deliver_now
Rendered user_mailer/test.html.erb within layouts/mailer (0.1ms)
UserMailer#test: processed outbound mail in 16.2ms
Sent mail to user#hotmail.com (769.1ms)
Date: Wed, 11 Nov 2015 10:26:33 -0800
From: user#hotmail.com
To: user#hotmail.com
Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>
Subject: ses test
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
A quick brown fox jumped over the lazy dog.
</body>
</html>
</body>
</html>
=> #<Mail::Message:70263775568080, Multipart: false, Headers: <Date: Wed, 11 Nov 2015 10:26:33 -0800>, <From: user#hotmail.com>, <To: user#hotmail.com>, <Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>>, <Subject: ses test>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>
Following the java documentation here, I was able to send an email successfully using the same host and smtp username/password.
Any idea what I might have wrong? Where can I find more logs to dig more into it?
Thanks!
Turns out I just needed to close and reopen the rails console. For some reason reloading it is not enough.

Mail to all user

I want to send a mail to all my users when an article is published. I have this code :
Mailer class
class AdminMailer < ApplicationMailer
def notification_mail(user)
#user = user
mail(to: #user.email, subject: '**TEST**')
end
end
Article.rb
after_create :send_email_to_users
def send_email_to_users
User.all.each do |user|
AdminMailer.notification_mail(user).deliver_now
end
end
Configuration
ActionMailer::Base.smtp_settings = {
:user_name => 'simon.m#********.com',
:password => '********',
:address => 'smtp.*******.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
This code seems to work because when I post an article I can see on my console that e-mail are send but I didn't receive them into my mailbox.
CONSOLE
AdminMailer#notification_mail: processed outbound mail in 23.1ms
Sent mail to simon.m#*******.fr (30014.1ms)
Date: Mon, 08 Jun 2015 05:21:05 -0700
From: simon.m#******.com
To: simon.m#*********.fr
Message-ID: <557588b117179_ae53f7fcf56e0d0328b8#localhost.localdomain.mail>
Subject: **TEST**
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_557588b114e80_ae53f7fcf56e0d0327be";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_557588b114e80_ae53f7fcf56e0d0327be
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
VW5lIGlkw6llIHZpZW50IGQnw6p0cmUgcHVibGnDqWUKPT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0K
----==_mimepart_557588b114e80_ae53f7fcf56e0d0327be
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content=3D'text/html; charset=3DUTF-8' http-equiv=3D'Content-Ty=
pe' />
</head>
<body>
<h1>Une id=C3=A9e vient d'=C3=AAtre publi=C3=A9e </h1>
</body>
</html>
</body>
</html>
Anyone know how to make it works ?
Make sure you have deliveries and delivery errors enabled for the environment your app is running in.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
Last time I did this I had to enable less secure apps on my google apps account, it might be the same for you. It's hard to say without any errors. Visit https://www.google.com/settings/security/lesssecureapps to enable that. More info
Make sure Google hasn't blocked your IP. Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.
Source
Seeing some sort of error would help to narrow this down but hopefully this helps!

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