Rails devise with gmail errors - ruby-on-rails

I'm currently working on an application with a mailer system. It was working fine, sending a welcome email and sending instructions to reset password, but now and only when I try to send reset instructions I have this error.
ArgumentError (SMTP From address may not be blank: nil):
I'm using a custom domain like so noreply#mycustomdomain.com
And here is my configuration
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
domain: 'gmail.com',
authentication: :plain,
enable_starttls_auto: true,
user_name: Rails.application.secrets.mailer_username,
password: Rails.application.secrets.mailer_password
}
Any idea ?
Edit
class UserMailer < ApplicationMailer
default from: 'noreply#mycustomdomain.com'
def welcome_email(user)
#user = user
#url = 'http://localhost:3000/users/sign_in'
mail(to: #user.email, subject: 'Bienvenue')
end
def generate_new_password_email
user = User.find(params[:user_id])
user.send_reset_password_instructions
end
def reset_password; end
end

You could try setting :from in your config, using the default_option like this,
config.action_mailer.default_options = { from: 'noreply#mycustomdomain.com' }

It looks like you don't have a From header in your email. A good practice would be to put the following line into your ApplicationMailer:
class ApplicationMailer
default from: 'noreply#mycustomdomain.com'
# ...
end
To override this in your inheriting mailers, simply declare the same statement. To override it in individual mail methods, put it into the mail call like so:
def new_message(user, message)
mail(
to: user.email,
subject: "New message from #{message.sender.name}",
from: message.sender.email
)
end
Hope that helps

In devise.rb config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com' had been commented.
Config.mailer_sender was never initialized and so always nil even if I set it with default from:

I had the same problem, and the reason behind that was i working in development but my mailer was searching the smtp_settings in the production, so to solve the issue you can change the mailer settings or you can copy the same smtp_settings into production.

I stumbled upon the same problem. My solution was, I edited config/initializers/devise.rb changed a line from config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com'
to config.mailer_sender = ENV["default_from_email"]

Related

actionmailer not sending emails even after setting all the possible configurations

This is the method inside ApplicationMailer
class CancelTrip < ApplicationMailer
default from: 'xyz#gmail.com'
def cancel_trip
#recvr= "ssdd#gmail.com"
mail(to: #recvr, subject: 'Your trip has been cancelled as per your request' )
end
end
And the environmental variables as follows:
SMTP_ADDRESS: 'smtp.gmail.com'
SMTP_PORT: 587
SMTP_DOMAIN: 'localhost:3000'
SMTP_USERNAME: 'xyz#gmail.com'
SMTP_PASSWORD: 'gggh2354'
And I am call the mailer method in my controller as follows:
def cancel
xxxxx
CancelTrip.cancel_trip.deliver_now
end
developement.rb has following
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = {host: 'localhost', port:3000}
config.action_mailer.perform_deliveries = true
Log shows the email being sent. But I dont see any email in inbox.
My rails version is 4.2.6.
Add following smtp setting on config/application.rb file:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxxxxxx#gmail.com",
:password => "xxxxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}
Check in junk mail... I had this problem a little while back
use letter opener in development for be sure your actionmailer delivered your mail. after that you can finding deliver error to gmail or other.
Make sure that your ApplicationMailer is inheriting from ActionMailer::Base.
class ApplicationMailer < ActionMailer::Base
default from: 'from#exmaple.com'
layout 'mailer'
end
One possibility may be your firewall not allows to send email. Try connect different network.

Rails 5 Action Mailer jpg is not transmitted properly

I'm trying to send an email using ActionMailer on my local development environment. Everything's working fine, except the attachments.
In the following code block I attach the JPG file to an email, "Testbild.jpg", which has a size of 6,19KB. However, the receiver sees an email with a file of the same name that has a size of only 96 bytes.
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
attachments['Testbild.jpg'] = File.read("#{Rails.root}/public/images/Testbild.jpg")
mail(to: #user.kontakt.email, subject: 'Welcome to My Awesome Site')
end
Here is my ActionMailer Config in the development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost', port: 3001 }
config.action_mailer.smtp_settings = {
user_name: "christian.henschel#xclirion.de",
password: "************",
domain: "xclirion.de",
authentication: :plain,
address: "smtp.xclirion.de",
openssl_verify_mode: OpenSSL::SSL::VERIFY_NONE,
enable_starttls_auto: true,
port: 587
}
I found out that the 96 bytes of the received file are exactly the same as the first 96 bytes of my source file. The rest seems to be lost.
What goes wrong here? How can I force RoR to send the whole file?
Try to change your attachments hash as shown below:
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
attachments['Testbild.jpg'] = File.open("#{Rails.root}/public/images/Testbild.jpg", 'rb'){|f| f.read}
mail(to: #user.kontakt.email, subject: 'Welcome to My Awesome Site')
end

How to send email in rails

The following is my controller file:
class BiodataController < ApplicationController
def store
#user=params[:username]
#age=params[:age]
#gender=params[:gender]
#mstatus=params[:mstatus]
#language=params[:language]
#email=params[:email]
#mobile=params[:mobile]
if params[:username].present? && params[:age].present? && params[:gender].present? && params[:mstatus].present? && params[:language].present? && params[:email].present? && params[:mobile].present?
Biodatum.create(name: #user, age: #age, gender: #gender, mstatus: #mstatus, language: #language, email: #email, mobile: #mobile)
Infomail.sendmail(#email)
render 'store'
else
render 'Error'
end
end
end
My requirement is to send email to the address stored in #email. So I created the mailer as 'Infomail'. The following is my mailer file.
class Infomail < ApplicationMailer
default from: 'abc#xyz.co.in'
def sendmail(user)
#user = user
mail(to: #user.email, subject: 'sample mail')
end
end
And I also have html file under 'app/views/infomail/sendmail.html.erb'. But it doesn't work. Can any one explain me what is the bug in
my code.
I'll recommend that you take a look at http://guides.rubyonrails.org/action_mailer_basics.html for starters.
For sending mails, you'll have to use the deliver_now (Immediate) or deliver_later (Queue for worker) methods, like so:
InfoMailer.sendmail(#user).deliver_now
Note that I renamed it to InfoMailer, because that's the standard naming, of mailers.
If you want to test you mailer into development environment just open your development.rb file and write below code inside for smtp configuration.
config.action_mailer.default_url_options = { host: 'url of your application' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'address of your mailer',
port: 465,
domain: ENV['YOUR_DOMAIN_NAME'],
user_name: ENV['YOUR_USER_NAME'],
password: ENV['YOUR_PASSWORD'],
authentication: :plain,
ssl: true,
tls: true,
enable_starttls_auto: true
}
please change above configuration as per your mailer smtp settings.
Now create tamp let of your mail as per you requirements. file path as per below:
app -> view -> common_mailer inside of this please make sendmail.html.erb file.
You are sending #email which is a String in params while you are expecting Object on which you can call .email
#bio = Biodatum.create(name: #user, age: #age, gender: #gender, mstatus: #mstatus, language: #language, email: #email, mobile: #mobile)
Infomail.sendmail(#bio)
This will pass Biodatum instance on which you can call .email
Also to avoid confusion you can change your mailer method
def sendmail(bio)
#bio = bio
mail(to: #bio.email, subject: 'sample mail')
end
If you are making these changes, you will also need to change them in sendmail.html
EDIT
Make sure you have configured the settings in /config/environments/development.rb and /config/environments/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
}

Rails send mail with gmail development mode

I didn't figure out how I send mail from gmail in development environment.It didn't send email. I didn't understand the rails guide, and also I wonder if the production env is the same ?
config/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'something.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mail.google.com',
user_name: 'myusername#gmail.com',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
mailer/user_mailer.rb
default :from => 'something.com'
def welcome_email(user)
#user = user
#url = 'http://something.com'
mail(to: #user.email, subject: 'Welcome')
end
edit
where I call, in users create method,
UserMailer.welcome_email(#user).deliver_now
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Try this in development.rb It will either send mail or raise delivery error in console.
You can call your mailer methods in two way to send email,
In UsersController's create action
def create
#your codes and logics to save user...
UserMailer.welcome_email(#user).deliver_now
#your codes goes here ...
end
In User model after_create callback
class User < ActiveRecord::Base
after_create :send_email
def send_email
UserMailer.welcome_email(self).deliver_now
end
end
I would prefer second one for sending email, use model callback rather in controller.

mailer authentication error in Rails 3.2 with gmail or SendGrid

I'm trying to set up mailing from a very simple Rails 3.2 app. Tried Gmail, tried SendGrid. Getting same error.
Net::SMTPAuthenticationError in UsersController#create
530-5.5.1 Authentication Required
Here's my section of environments/development.rb
# Care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
# Change mail delivery to either :smtp, :sendmail, :file, :test
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "signaldesign.net",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["gmailusername"],
password: ENV["gmailpassword"]
}
Here's my users_controller
def create
#user = User.new(params[:user])
if #user.save
UserMailer.signup_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
I'm stumped. None of the suggestions I've found online are making a difference.
I figured it out. I finally resorted to just deleting things and trying again.
I changed my smtp_settings username and password from:
user_name: ENV["gmailusername"],
password: ENV["gmailpassword"]
to
user_name: "gmailusername",
password: "gmailpassword"
Meanwhile the gem dotenv changed its implementation. You can now use your ENV settings to initialize the ActionMailer. I verified this with dotenv v.0.7.0. Make sure you load the environment variables. Autoloading has been removed from the gem. Read here.
Try changing the domain to localhost.localdomain

Resources