ArgumentError SMTP To address may not be blank - ruby-on-rails

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

Related

ActionMailer Not Sending Emails

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?

Cannot sent reset password mails in rails application with a timeout situation

I'm very new to Rails. Now I have a problem with sending reset password emails on Rails website.
Right now the problem is when I enter a registered email to reset the password, the email cannot be sent.
Net::OpenTimeout in PasswordResetsController#create
I think the problem is about the stmp setting.
Here is some of my code. By the way, I'm doing all these in the development environment.
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host =>
"http://localhost:3000" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "Here I put my intern company",
# :domain => "gmail.com",
:authentication => :plain,
:user_name => "Here I put my actual email",
:password => "The password of my actual email",
:enable_starttls_auto => true
}
password_resets_controller.rb
def create
# #user = User.find_by(email: params[:password_resets]
[:email].downcase)
#user = User.find_by(email: params[:password_reset]
[:email].downcase)
if #user
#user.create_reset_digest
#user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
user.rb
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "naomi<noreply#MYINTERNCOMPANY.com>"
def password_reset(user)
#user = user
mail(to: #user.email, subject: 'Password reset')
end
end
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'noreply#MYINTERNCOMPANY.com'
layout 'mailer'
end
Can anyone help with this?
And also, after this how can I set the production environment so that the website can be deployed on Heroku?

action mailer error to deliver email

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.

Can not send email by Mailer Rails

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.

Sending confirmation emails to registered users in ROR app via localhost

In my ROR app, I am trying to send confirmation emails to my registered users when they signup, my website is on localhost currently. I am getting this error:
"undefined method `recipients' for #<UserMailer:0x3d841a0>"
Here is my code;
development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "localhost:3000"}
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:authentication => :login,
:user_name => "myemailid#gmail.com",
:password => "myrealpassword"
}
Users_controller.rb
def new
UserMailer.registration_confirmation(#user).deliver
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
recipients user.email
from "myemailid#gmail.com"
subject "Thank you for registration"
body :user => user
end
end
** development.rb**
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
** Users_controller.rb **
def new
UserMailer.registration_confirmation(#user).deliver
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
** User_mailer.rb **
def registration_confirmation(user)
#message = 'whatever you want to say here!'
mail(:from => "myemailid#gmail.com", :to => user.email, :subject => "Thank you for registration")
end
**/app/views/user_mailer/registration_confirmation.text.erb *
<%= #message %>
That's what I've done in my development mode, and it works
What version of Rails are you using?
Sending of email changed in version 3.2 (I believe)
http://api.rubyonrails.org/classes/ActionMailer/Base.html
Try:
UserMailer.registration_confirmation(#user).deliver
This is your user_mailer.rb
user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
recipients user.email
from "myemailid#gmail.com"
subject "Thank you for registration"
body :user => user
end
Try instead:
class UserMailer < ActionMailer::Base
default from: "myemailid#gmail.com"
def registration_confirmation(user)
mail(to: user.email, subject: "Thank you for registration")
end
end
And set up the appropriate view in views/user_mailer e.g. registration_confirmation.html.erb

Resources