Rails mailer does not send email - ruby-on-rails

When I try to use rails (5.1.4) mailer, I don't receive any email like I should do. I followed the official rails guide to make it. My config/environment/development.rb :
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'username#gmail.com',
password: 'app-password-from-google',
authentication: 'plain',
enable_starttls_auto: true
}
With this config I made a new mailer in mailers/appointement_mailer.rb :
class AppointementMailer < ApplicationMailer
default :from => "username#gmail.com"
def appointement_information(user_email, data)
#email = user_email
#body = data
mail(:to => "username#gmail.com", :subject => "xxx")
end
end
This mailer is triggered by a form controlled by a controller, it is located under controllers/contacts_controller.rb :
def new
end
def create
#appointement = appointement_params
AppointementMailer.appointement_information(#appointement['email'], #appointement['body'])
flash[:notice] = "Some message"
redirect_to articles_path
end
private
def appointement_params
params.require('appointement').permit(:email, :body)
end
The form correctly display the flash "Some Message" and no error is written in the console.

You need to call deliver_now on your call to the mailer.
AppointementMailer.appointement_information(#appointement['email'], #appointement['body']).deliver_now

Related

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.

How to receive an email from users in rails?

I have a form that allows users to enter their names, email add, subject and message. When the user hits SEND, the message should be sent to me(admin).
I have this code under my development config...
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => #user.email,
:port => 587,
:user_name => ENV['sys.questdentalusa#gmail.com'],
:password => ENV['passwordhere'],
:authentication => 'plain',
:enable_starttls_auto => true
}
and this code under my user_mailer
def welcome_email(user)
#user = user
mg_client = Mailgun::Client.new ENV['api_key']
message_params = {:from => ENV[#user.email],
:to => 'sys.questdentalusa#gmail.com',
:subject => #user.subject,
:text => #user.text}
mg_client.send_message ENV['domain'], message_params
end
It won't send the message. It's as if it did not execute.
The rule is, no model should be involved.
Example, you have an existing gmail account and wrote a message sent to me. I should receive your message from your entered gmail account.
Two things your developer config and message_params looks wrong,
in message_params : :from => ENV[#user.email] is should be like #user.email
in smtp_settings : :address => #user.email, is like "smtp.mailgun.org". checkout more smtp_settings at here
I got the answer for quite a while now and I just decided to might as well share it here. This is what I did in my development.rb
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
user_name: "sys.questdentalusa#gmail.com",
password: "passwordhere",
authentication: :plain,
enable_starttls_auto: true
}
This is what I got under my Mailer
class MessageMailer < ActionMailer::Base
default from: "sys.questdentalusa#gmail.com"
default to: "questdentalusa#gmail.com"
def new_message(contact)
#contact = contact
mail subject: 'Inquiry from website: ' + #contact[:subject]
end
end
I got this under new_message.text.erb
Name: <%= #contact[:name] %>
Email: <%= #contact[:email] %>
Message: <%= #contact[:content] %>
And this is under my controller
class HomeController < ApplicationController
skip_before_filter :verify_authenticity_token
def send_mail
if MessageMailer.new_message(contact_params).deliver
redirect_to contact_path
flash[:notice] = 'Your messages has been sent.'
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :subject, :content)
end
end

Rails app with 2 domains: Define 2 SMTP's in initializers/devise.rb & environments/development.rb?

I have a Rails app that handles two domains. The app is set up like described in this blogpost. In my controllers and views I am using request.domain to determine which app a visitor visits.
When someone signs up for an account, Devise sends out a confirmation email. This process depends on the following lines:
# config/environments/development.rb
MyApp::Application.configure do
...
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'localhost',
:user_name => 'my#email.com',
:password => 'MyPassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
...
end
# config/initializers/devise.rb
Devise.setup do |config|
...
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class with default "from" parameter.
config.mailer_sender = "my#email.com"
...
end
I need the :usernamein development.rb and the config.mailer_sender in devise.rb to depend on request.domain, because the user should of course receive an email from the domain he signs up for.
I use SiteSetting model to specify different mailboxes for each Site
SiteSetting contains configs for each mailboxes.
A code for mailer is:
# app/mailers/invitation_mailer.rb
class InvitationMailer < ActionMailer::Base
def invitation(invitation)
#site = invitation.site
#invitation = invitation
email_settings = #site.email_settings.first
mail(to: invitation.email,
from: email_settings.try(:from) || 'notifier#example.com',
subject: "Invitation",
delivery_method_options: delivery_options(email_settings))
end
private
def delivery_options(email_settings)
#_delivery_options ||=
if email_settings
{
address: email_settings.address,
port: email_settings.port,
domain: email_settings.domain,
user_name: email_settings.user_name,
password: email_settings.password,
authentication: email_settings.authentication,
enable_starttls_auto: email_settings.enable_startls_auto
}
else
{
address: 'smtp.google.com',
port: 587,
domain: 'example.com',
user_name: 'notifier#example.com',
password: 'secret',
authentication: 'plain',
enable_starttls_auto: true
}
end
end
end
Update
You can pass username like invitation in this examle
and then you can get acsess to username in your Mailer class (for example InvitationMailer)
# app/controller/users_controller.rb
class UsersController < ApplicationController
def invitation
invitation = Invitation.find(params[:invitation_id])
InvitationMailer.invitation(invitation).deliver
end
end

Rails Action Mailer not sending email

I am a complete beginner in Rails and I'm trying to send an email after someone signs up using Action Mailer.
My logs say that the email is sending, but Gmail never gets it.
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "asciicasts.com",
:user_name => "asciicasts",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "eifion#asciicasts.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
controllers/users_controller.rb
...
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
...
Thanks!
Make sure you have this option set in your config/environments/development.rb :
config.action_mailer.delivery_method = :smtp
Also, in ActionMailer::Base.smtp_settings you need to specify a valid gmail account. Copy-pasting (asciicasts) is not gonna cut it here.
See this question for reference: Sending mail with Rails 3 in development environment
Instead of 'smtp' you can use 'sendmail'
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
:port => "587", :domain => "gmail.com", :user_name => "xxx#gmail.com",
:password => "yyy", :authentication => "plain", :enable_starttls_auto => true }
I ran into this same problem for a new mailer I had setup. I couldn't figure out for the life of me why this new mailer couldn't send emails, or even get to the method in the mailer when I stepped through it.
Solution
It ended up being that if you put the deliver_now or deliver* code within the mailer, it does not send the email.
Example Broken
def email_message()
message = mail(to: User.first, subject: 'test', body: "body text for mail")
message.deliver_now
end
Corrected
#Different class; in my case a service
def caller
message = MyMailer.email_message
message.deliver_now
end
def email_message()
mail(to: User.first, subject: 'test', body: "body text for mail")
end
This solved the problem for me, I hope it solves it for someone else.

Resources