amazon ses ActionMailer ruby on rails debug - ruby-on-rails

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.

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

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

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!

Rails + SendGrid Unauthenticated senders not allowed

I'm currently working on sending emails via sendgrid and currently sending emails to new users is working. ...But strangely, very similar emailing options aren't working - the full email including the recipient is displayed in the log but is never received.
Here's what I have:
In a mail.rb file in initializers:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => configatron.sendgrid.username,
:password => configatron.sendgrid.password,
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
with the username and password being defined elsewhere - they are confirmed to have their correct values
When a new user registers, I call my UserMailer class and do the following:
def welcome_email(user)
#email = user.email
#name = user.full_name
mail(to: #email, subject: "Welcome!")
end
And the worker calls it by simply doing the following:
UserMailer.welcome_email(user).deliver
This much works; I receive the email and that's that.
But when I try to send an email from a different method, magically things break down.
In the SAME UserMailer class:
def request(org, recipient, item)
#org = org
#recipient = recipient
#item = item
mail(to: #org.email, subject: "A new request has been posted!")
end
Using the SAME function (although this time outside of a worker so I can debug easily):
UserMailer.request(org, recipient, item).deliver
The email appears in the terminal perfectly:
Sent mail to mypersonalemail#test.com (4717ms)
Date: Mon, 31 Dec 2012 01:03:35 -0800
From: mysendingemail#test.com
To: mypersonalemail#test.com
Message-ID: <[longhexstring]#localhost.localdomain.mail>
Subject: A new request has been posted!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_50e154e769903_3c41bba7ec576d5";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.mail>
Hello world!
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Hello world!
</p>
</body>
</html>
But, I check my inbox on my personal email, the junk, the trash, and received nothing. Even sendgrid has no logs of me ever requesting to send request emails! But the welcome emails are working perfectly fine, and even are logged through sendgrid. I don't see what could cause one group of emails to send but not another and this is really baffling. No obvious errors in syntax jump out at me, and the values I use are of the correct datatype and not nil.
Any help with this strange issue would be extremely appreciated.
The solution ended up being remarkably simple, so for those who find this thread and need help I'll at least post what I did.
In development.rb I did the following to see the error being raised:
config.action_mailer.raise_delivery_errors = true
And got this message:
Net::SMTPFatalError - 550 Cannot receive from specified address <mypersonalemail#test.com>: Unauthenticated senders not allowed
Which is strange considering I thought I was authenticated with the info I put into .env Outputting my configatron.sendgrid.username sure enough is nil. Still trying to figure out why that would be and I'm still not sure how the welcome emails were sent.
Sure enough, changing my smtp settings to be hardcoded values for authentication worked like a charm. Now the question becomes how to get the ENV to not come out nil...
The end solution was simply me being on the wrong localhost. I was using rails's default rails server at port 3000, but foreman start (not sure if by default but in my project's case) was using port 5000 instead.
I had this issue when running code via rake. The problem was that the SENDGRID_USERNAME and SENDGRID_PASSWORD environment variables stored in my .env were not loaded automatically via rake. The solution was to run rake through foreman:
foreman run bundle exec rake namespace:task

Resources