Rails not sending emails when using a view - ruby-on-rails

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

Related

Trying to send email with Gmail STP (Rails)

I'm trying to send some emails from my web application. Here is my configuration:
//config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'domain.com',
user_name: 'myusername',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
// mailers/contact_mailer
class ContactMailer < ApplicationMailer
default from: 'from#gmail.com'
def send_email()
mail(to: 'to#gmail.com', subject: 'Welcome to My Awesome Site')
end
end
// views/contact_mailer/send_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, name</h1>
<p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
So to test it, I ran this command from the Rails command:
ContactMailer.send_email
And I'm getting this result:
irb(main):003:0> ContactMailer.send_email
Rendered contact_mailer/send_email.html.erb within layouts/mailer (0.1ms)
ContactMailer#send_email: processed outbound mail in 10.4ms
=> #<Mail::Message:69980432504260, Multipart: false, Headers: <From: adib.elaraki#gmail.com>, <To: adib.amine.mdr#gmail.com>, <Subject: Welcome to My Awesome Site>, <Mime-Version: 1.0>, <Content-Type: text/html>>
irb(main):004:0>
Unfortunately, after checking my email, I don't receive the email
mail only generates a mailer object. You need to append something like deliver_now for it to actually send. For example:
ContactMailer.send_email.deliver_now

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.

Sendgrid template

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.

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!

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

Resources