Trying to send email with Gmail STP (Rails) - ruby-on-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

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

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.

Cloud9 and ActionMailer / Mailgun?

I hope you can lend me a hand with this!
I used to develop locally, but I'm abroad and I'm using Cloud9 to work with some projects. Currently, It's giving me a hard time with Action Mailer.
My ActionMailer Initializer:
ActionMailer::Base.smtp_settings = {
port: '2525',
address: 'smtp.mailgun.org',
user_name: ENV['MAILGUN_SMTP_LOGIN'],
password: ENV['MAILGUN_SMTP_PASSWORD'],
domain: 'app07ad98bdda3b4c469a24228512cffe5c.mailgun.org',
authentication: :plain,
content_type: 'text/html'
}
ActionMailer::Base.delivery_method = :smtp
mailers/gun_mailer.rb
class GunMailer < ActionMailer::Base
default from: "from#example.com"
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
mail(to: #user.email, subject: 'Welcome to My Awesome Site')
end
end
views/gun_mailer/welcome_email.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= #user.email %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= #user.email %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= #url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Console
u = User.first
GunMailer.welcome_email(u).deliver
I've set up the environment variables with figaro, and evrything seems to be correct... But the mail is never sent! I've heard that C9 has some ports blocked (587 being one of them), I've tried with 2587, 2525 (as other posters recommended), but It doesn't work!
First, I would make sure that the following variables are actually being set within your mail.rb using the Rails console:
ENV['MAILGUN_SMTP_LOGIN']
ENV['MAILGUN_SMTP_PASSWORD']
Second, you should verify that the ports are not blocked by doing the following:
$ telnet smtp.mailgun.org 2525
$ telnet smtp.mailgun.org 587
Note: The port isn't blocked if you see something similar to the following:
Trying 104.130.177.23...
Connected to smtp.mailgun.org.
Escape character is '^]'.
220 ak47 ESMTP ready
If one of the above ports are not blocked, then I would stop here, update mail initializer to use the unblocked port, and recheck your code. Otherwise, I would continue reading.
Third, if you have determined that both ports are blocked, then you'll need to use the HTTP API and I would read the documentation here:
https://documentation.mailgun.com/api-sending.html#sending
or
using a different hosting company like Heroku.com or DigitalOcean.com.
Well, I wish that the information above assists you in sorting out your issue.

Can a rails application send emails from the local host?

I think I've set up ActionMailer successfully, however emails do not arrive in their inbox, despite the server stating differently:
Sent mail to julian#designimperial.com (2003ms)
Date: Fri, 30 Aug 2013 22:26:41 +0100
From: from#example.com
To: julian#gmail.com
Message-ID: <52210e11bedc9_16501d84f64257a#Julian-PC.mail>
Subject: Welcome to my awesome site!
Mime-Version: 1.0
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>
<h1>Welcome to example.com, Julian</h1>
<p>
You have successfully signed up to example.com,
your username is: gogo.<br/>
</p>
<p>
To login to the site, just follow this link: http://example.com/login.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Do I need to upload my rails application to a server or heroku inorder for emails to be sent? I know this is the case with PHP mailing and MAMP/XAMPP
Having incorrect settings can result in email failing to send without raising any exceptions. Assuming you're using the SMTP protocol to deliver main, you should ensure that you've properly setup your ActionMailer configuration. Here's a basic configuration for utilizing Gmail (via the Rails Guides):
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
Enabling delivery errors regarding the action mailer to be raised is absurdly helpful in getting the action mailer working. It's also disabled by default.
To get it working, open your development environment file, located at:
yourappname/config/environments/development.rb
around line 17, there is a method that enables and disables error reporting by taking a boolean:
config.action_mailer.raise_delivery_errors = false
Change it to 'true', and restart the rails server. You now have excellent error reporting from rails.
I also like to put my smtp settings directly below in the same file like this:
#SMTP settings
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
user_name: "mrexample",
password: "01password02"
}
Remember to put your smtp settins in your production.rb and test.rb environment configuration files as well. This example hash is also 100% correct if you're using gmail as the sender address of the emails. The only strings you need to change are the password and username.
Another thing that isn't glaringly obvious with gmail is that your username is everything to left of the # symbol, and the domain everything to the right of the #.
e.g
mrexample#gmail.com
==
config.action_mailer.smtp_settings = {
# address: "smtp.gmail.com",
# port: 587,
domain: "gmail.com",
user_name: "mrexample",
# password: "01password02"
}
Finally, if you're getting the error message
"Errno::ECONNREFUSED (No connection could be made because the target machine actively refused it. - connect(2))"
Chances are the only thing wrong with your application is your smtp settings. Probably your password.

Rails 3 ActionMailer - Gmail Classifies Message As Spam

My ActionMailer is set up to send an email once a user fills out an application. After upgrading to Rails 3 and ActionMailer, gmail now seems to be classifying the response email as spam.
I use google apps for the domain (hosted by dreamhost) and have it set up to send as smtp; and I am able to directly send emails to the same users from the google apps web account, and not have it classified as spam.
My question is: are there settings or values that I should have set in the ActionMailer (etc) that might circumvent this?
One recommendation I got was to set up and SPF, but I wasn't sure about this as I was using smtp via gmail.
Here is my configuration:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "email#domain.org",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
The email is rendered as html (have a plain text one too), with attachments of the files the user uploaded to the application.
<!DOCTYPE html>
<HTML>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Dear <%= #f.first_name + " " + #f.last_name %>,<br>
<br>
etc....
Here is how my mailer is formatted
class EmploymentMailer < ActionMailer::Base
default :from => "email#domain.org"
def employment_app_email(f, files)
#f = f
#files = files
mail(:to => ['email#domain.org', f.email], :subject => "Subject")
files.each do |file|
attachments[file[1].original_filename] = File.open(file[1].path, 'rb'){|a| a.read}
end
end
end

Resources