I set up ActionMailer on an example rails app and action-mailer seems to not be delivering emails.
It's a fairly simple setup, when a user creates an account, they automatically receive a welcome email from the site!
production.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password => ENV['gmail_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
config.action_mailer.raise_delivery_errors = true
user_controller.rb
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(#user).deliver
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
My gmail username and password are successfully set using Figaro.
Also, my Gmail settings are set to allow less secure apps.
What's going on here?
Related
I'm getting ArgumentError SMTP "To address" may not be blank from the Mailer that I created for the test.
My code is almost same with an example in the Rails Guide that you can find in here:
https://guides.rubyonrails.org/action_mailer_basics.html
Or Ruby on Rails tutorial:
https://3rd-edition.railstutorial.org/book/account_activation_password_reset#sec-account_activation_mailer
Server log
Error performing ActionMailer::MailDeliveryJob (Job ID: fa1eac9b-a2e0-4b4b-aca7-c785141fd7ce) from Async(mailers) in 214.6ms: ArgumentError (SMTP To address may not be blank: []):
My Code
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'sendish.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
# app/mailers/appliction_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'from#example.com'
layout 'mailer'
end
# app/mailer/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'notifications#example.com'
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
attachments.inline['helloworld.gif'] = File.read(Rails.root + 'public/helloworld.gif')
mail to: user.email, subject: 'Welcome to My Awesome Site'
end
end
# app/controllers/users_controller.rb
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# Tell the UserMailer to send a welcome email after save
UserMailer.welcome_email(#user).deliver_now
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def user_params
params.require(:user).permit(:name, :email, :login)
end
I see Mailer is passing To address in my development
UserMailer#welcome_email: processed outbound mail in 26.0ms
Date: Thu, 10 Sep 2020 15:01:52 -0700
From: notifications#example.com
To: user#example.com
Subject: Welcome to My Awesome Site
Addtional Information
The issue is happening in Heroku production
I built an example app to try to make the mailer work. The app is hosted on https://blooming-brushlands-80122.herokuapp.com,
and the source code is here. Here's my config associated with action mailer:
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply#example.com'}
and for sending the mailer
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(#user).deliver
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
The app does not send emails to users.
Does anyone have any idea why?
This answer assumes you're using the free sendgrid Addon from Heroku since I see you're using sendmail. To add it just log into heroku and add it as a free resource. Since you didn't specify whether or not your trying to send emails locally or in production I added configs for both. In your config/environment/development.rb file you should do the following to send from your local:
config.action_mailer.delivery_method = :smtp #Yours used sendmail, change to this.
config.action_mailer.perform_deliveries = true #Needed if this is dev env. file
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['KUNZIG_SENDGRID_USERNAME'],
:password => ENV['KUNZIG_SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
Obviously replace the username and password with your own env. variables assigned by Heroku. Heroku's config. vars can be found under the settings tab. just click the "reveal" button.
Your environment/production.rb file would be something like:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'kunzig.herokuapp.com',
:enable_starttls_auto => true
}
Notice the only change is the domain name and environment variables since these are set automatically by Heroku when you add Sengrid as an addon. Also, change your deliver call in your controller to deliver_now so we know it's not a problem with your background worker config.
I am trying implementing send mail through action mailer in rails related code are..
my mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "debasish#thejaingroup.com"
def registration_confirmation(user)
mail(:to=>user.email, :subject =>"Registered")
end
end
users.controller is
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
UserMailer.registration_confirmation(#user).deliver
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Here, Your initializer\setup_mail.rb setting will go to the development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 9292 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address =>"smtp.thejaingroup.com",
:domain =>"thejaingroup.com",
:port => 587,
:user_name =>"debasish#thejaingroup.com",
:password =>"************"
:authentication =>"plain"
}
and my view is .. user_registration.text.erb ---is
Hi sir you successfully Completed signed..........!
my have a error msg after running this apps..
SocketError in UsersController#create
getaddrinfo: The requested name is valid, but no data of the requested type was found.
I don't think the address you've used here 'smtp.thejaingroup.com' is the address of the valid email service provider. If you are trying to use your gmail, then it should be 'smtp.gmail.com'.
This is the normal gmail configuration.
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password => ENV['gmail_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
In this case, all the mails would be delivered to your gmail. . If you don't want this to happen, you can try the gem letter_opener for development environment. For production env, you may have to use email provider such as mandrill or sendgrid.
I have tried to configure like the tutorials but still can not send email. At the console, it displays the image below:
I have configured in config file \ environments \ development.rb and production.rb as follows:
config.active_record.dump_schema_after_migration = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'gmail username',
password: 'password',
authentication: 'plain',
enable_starttls_auto: true }
app\mailers\user_mailer.rb:
class UserMailer < ApplicationMailer
def registration_confirmation(user)
mail(:to => user.email,:subject => "registered")
end
end
app\mailers\application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "testonebig#gmail.com"
layout 'mailer'
end
app\controllers\users_controller.rb
def create
#user = User.new(user_params)
UserMailer.registration_confirmation(#user).deliver
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
It runs without any mistakes have been displayed but when I checked gmail, it's not receive any mail.
Are you providing the correct email and password in your development.rb file?
One more thing:
You are sending email from this id "testonebig#gmail.com", please login with this email in gmail. Just to make sure that everything is ok with your account. May be some kind of issue in authentication with your account.
I have an app where when users register they get a confirmation email set to them.
here's the users controller create action:
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
UserMailer.registration_confirmation(#user).deliver
log_in(#user)
format.html { redirect_to #user, notice: "Welcome to Pholder, #{#user.name}!" }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: 'new' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
as you can see after #user.save there is a mailer. However, a person just told me he got an error ("we're sorry, something went wrong") on heroku after trying to register, so I tried it myself and also got an error
2012-11-16T17:21:28+00:00 app[web.1]: Net::SMTPAuthenticationError (535-5.7.1 Please log in with your web browser and then try again. Learn more at
2012-11-16T17:21:28+00:00 app[web.1]: ):
after looking around my code, I tried making another user but this time it worked. Does anyone know why? I read in another post that this could be because it'll fail if too many users register at once (since too many emails are sent), but I don't think anyone was registering at that point since not many people know about my app.
smtp settings:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'pacific-ravine-3563.herokuapp.com',
:user_name => ENV["EMAIL"],
:password => ENV["PASSWORD"],
:authentication => "plain",
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = { :host => 'pacific-ravine-3563.herokuapp.com' }
Google might be blocking you until you prove you’re human. Try logging in to this Gmail account through a web browser. If it doesn’t work then, try visiting this link while logged in and completing the test.
If it helps anyone, I had to go here and "recognize" the activity as my own (it was coming from a server hosted by Heroku): https://security.google.com/settings/security/activity