Set up mandril with mail catcher in rails - ruby-on-rails

I am using mandril with action mailer.
On my mandril config
ActionMailer::Base.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => ENV['MANDRILL_DOMAIN'],
:authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_API_KEY']
end
Rails.application.config do |config|
config.mandrill_mailer.default_url_options = { host: ENV['DEFAULT_URL_HOST'] }
config.mandrill_mailer.default_url_options['protocol'] = ENV['DEFAULT_URL_PROTOCOL'] unless ENV['DEFAULT_URL_PROTOCOL'].blank?
end
On the action_mailer.rb
Rails.application.configure do
config.action_mailer.default_url_options = { host: ENV['DEFAULT_URL_HOST'] }
config.action_mailer.default_url_options['protocol'] = ENV['DEFAULT_URL_PROTOCOL'] unless ENV['DEFAULT_URL_PROTOCOL'].blank?
end
On the development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
I am running mail catcher and visiting http://localhost:1080/ but it doesn't work,
do I need to add more configurations ?

I'm using the default smpt_settings with mailcatcher in dev mode. so I didnt specify the
config.action_mailer.smtp_settings = ..
at all.
But I have to launch mailcatcher in the foreground with
mailcatcher -f
for it to work (it's somehow buggy as deamon). This has the advantage, that you can see if it receives the mail requests.

Related

Rails Net::OpenTimeout Mailer Namecheap Heroku Unable to send mailer 500 error

I attempted to send reset password email through a namecheap domain. I have reviewed every solution offered within StackOverflow and have not been able to get a viable solution. Let me know if I am missing any details below.
My Rails application is an API only.
It was working via gmail connection/smtp and when I switched it over to the namecheap/privateemail smtp it worked once.
After it worked locally I uploaded the code to heroku and that's when it started to fail.
# config/environments/development.rb
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: 'noreply#domainemail.com'}
config.action_mailer.default_url_options = { :host => '587'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none',
ssl: true
}
Production:
config.cache_classes = true
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: 'noreply#domainname.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none'
}
NotifierMailer class
class NotifierMailer < ApplicationMailer
default_url_options[:host] = ENV['BACKEND_URL']
default from: 'noreply#domainemail.com'
def create
admin = Admin.find_by(email: params[:email])
admin.generate_password_reset_token!
Notifier.password_reset(admin).deliver
end
def password_reset(admin)
#admin = admin
#url = "#{ENV['BACKEND_URL']}/password/reset?token=#{#admin.reset_password_token}&email=#{#admin.email}"
mail(to: "#{#admin.first_name} #{#admin.last_name} <#{#admin.email}>",
subject: "Ion Portal - Password Reset")
end
end
Password controller
class PasswordController < ApplicationController
protect_from_forgery with: :null_session
# include ActionController::RequestForgeryProtection
# protect_from_forgery with: :exception, unless: -> { request.format.json? }
def forgot
puts params
if params[:email].blank? # check if email is present
render json: {
error: "Email not present"
}
end
admin = Admin.find_by(email: params[:email]) # if present find admin by email
if admin.present?
admin.generate_password_token! #generate pass token
NotifierMailer.password_reset(admin).deliver_now
render json: { status: 'ok' }
else
render json: { error: ["Email address not found. Please check and try again."]}, status: :not_found
end
end
def reset
token = params[:token].to_s
if params[:email].blank?
return render json: {error: "Token not present"}
end
admin = Admin.find_by(reset_password_token: token)
if admin.present? && admin.password_token_valid?
if admin.reset_password!(params[:password])
redirect_to "#{ENV['ION_URL']}"
else
render json: {error: admin.errors.full_messages}, status: :unprocessable_entity
end
else
render json: { error: ["Link not valid or expired. Try generating a new link."]}, status: :not_found
end
end
def update
if !params[:password].present?
render json: {error: 'Password not present'}, status: :unprocessable_entity
return
end
if current_user.reset_password(params[:password])
render json: {status: 'ok'}, status: :ok
else
render json: {errors: current_user.errors.full_messages}, status: :unprocessable_entity
end
end
def successful_reset
render success_path
end
end
These settings worked for me. Turned out I also had left the settings for MailCatcher as well, that was my initial problem. Double check as well that what the domain setting and server address match, which in development, would mean setting the domain to 'localhost:3000'. Good luck!
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'mail.privateemail.com',
:port => 587,
:domain => 'localhost:3000',
:user_name => 'email#email.com',
:password => 'xxxxxxxxxxxxxxx',
:authentication => :plain,
:enable_starttls_auto => true,
}
After 24 hours of trial and error I had to remove a couple of lines of code to make it work.
config.action_mailer.smtp_settings = {
# For Gmail
# :address => "smtp.gmail.com",
# :port => "587",
# :domain => "gmail.com",
# :user_name => "noreply#gmail.com",
# :password => "pasword!",
# :authentication => "plain",
# :enable_starttls_auto => true
# For Namecheap
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.privateemail.com',
:port => 587,
:domain => 'privateemail.com',
:authentication => :plain,
:user_name => 'noreply#domainname.com',
:password => 'password!'
}
I removed the following:
enable_starttls_auto: true,
openss
l_verify_mode: 'none'

Rails devise not sending an email confirmation in development

I'm configuring email confirmation to be sent out after the user signs up using devise. I did everything what this says (https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users) but it still does not work.
Here are some codes:
//development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
//devise.rb
config.mailer_sender = 'myEmail#gmail.com'
# Configure the class responsible to send e-mails.
config.mailer = "Mailer"
//Mailer.rb
class Mailer < Devise::Mailer
helper :application
include Devise::Controllers::UrlHelpers
default template_path: 'devise/mailer'
end
Do I need to configure something more in order to send email confirmation letter in development environment??
Yes, You have to configure smtp settings for emails to be sent from like :
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "YOUR_EMAIL",
:password => 'PASSWORD',
:authentication => "plain",
:enable_starttls_auto => true
}
Add above code to your development.rb in order to configure smtp settings. Do add your email and password in the code where required.
Hopefully It will work fine!
In addition to #Muhammad's answer, also include these line of codes on your development.rb
config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000
config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup
config.action_mailer.default :charset => "utf-8"
In initializers/devise.rb
config.mailer_sender = 'your email'
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: 'your email',
password: 'your password',
authentication: 'plain',
enable_starttls_auto: true,
ssl: true
}
You need to add email settings to development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: '--your-gmail-id--#gmail.com',
password: '--your-password--',
authentication: 'plain',
enable_starttls_auto: true,
ssl: true
}
then also you need to change your gmail setting.
google account settings > security > less secure app access > ON

Sending mail with Rails 4 in development environment

I'm new in Rails. I'm trying to make a email notification about new messages of contact form using ActionMailer. I followed different tutorials, tried to apply different advices, but it didnt help.
My setup_mail.rb file is:
config.action_mailer.delivery_method = :sendmail
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'my_user_name',
password: 'my_password',
authentication: 'plain',
enable_starttls_auto: true
}
I tried tp add this code to the development.rb file, my development.rb also has next code:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
My contact_forms_controllers.rb has next code:
def create
#contact_form = ContactForm.new contact_form_params
#contact_form.save
redirect_to root_path
AdminMailer.notification(#contact_form).deliver
end
My admin_mailer.rb file is:
class AdminMailer < ApplicationMailer
default from: "my_address#gmail.com"
def notification(contact_form)
mail(to: "my_address#gmail.com", subject: 'New message on your web-site')
end
end
Everywhere my_address#gmail.com is my actual mail and my_password is my actual password.
I can create a message by help contact form without any errors.
Has anybody idea, where can be a problem?
You are almost there but your development.rb should look like this :
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
domain: 'gmail.com',
user_name: 'xxx#gmail.com',
password: 'xxxxxx',
authentication: 'login',
enable_starttls_auto: true
}
Have you added this in development.rb:-
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
And change sendmail to smtp:-
config.action_mailer.delivery_method = :smtp

530-5.5.1 Authentication Required. Learn more at

In order_mailer.rb:
default from: 'notifications#example.com'
def welcome_email(order)
#user = "Uday kumar das"
#url = 'http://example.com/login'
mail(to: 'dasudaykumar017#gmail.com', subject: 'Welcome to My Awesome Site')
end
In orders_conroller:
def delivery
#order1 = Order.where(:orderId=>params[:orderId])
#order = Order.find(#order1)
OrderMailer.welcome_email(#order).deliver
end
In environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
I am new to mails in rails.I am referring http://guides.rubyonrails.org/action_mailer_basics.html to learn. I am getting error like:
Net::SMTPAuthenticationError in OrdersController#delivery`
530-5.5.1 Authentication Required. Learn more at`
I did the same using my gmail, following are my configurations, try and see it if works
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:user_name => "<my gmail>#gmail.com",
:password => "<my gmail password>",
:openssl_verify_mode => 'none' }
Please note the:
:openssl_verify_mode => 'none'
section to skip the ssl errors

emails: server sends but a user doesn't receive

I'm facing a weird bug.
Trying to set up welcome email after a user's registration.
According to server logs, my code sends an email but a user doesn't receive it.
Here is my code:
models/user.rb
after_create :welcome_message
private
def welcome_message
UserMailer.welcome_email(self).deliver
end
and mailers/user_mailer.rb
def welcome_email(user)
#user = user
mail to: #user.email, subject: t("mailer.welcome_email.welcome")
end
here are environments. The error occurs in both of them
config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
and config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'mysite.com',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
I'm using Devise to register users. So, also tried to do that via :confirmable. Outcome is the same – test user doesn't get a welcome email.
Gemfile
gem 'rails', '3.2.12'
gem 'devise', '3.0.3'
gem "mail"
and here are server logs
Sent mail to user#hismail.com (1920ms)
Date: Sat, 10 May 2014
From: welcome#mysite.com
Reply-To: welcome#mysite.com
To: user#hismail.com
Message-ID: <aaa#aaa.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome user#hismail.com!</p>
Yet eventually there is no welcome email at user#hismail.com
Any ideas?
UPD:
on the internet I see that nobody else faces the same problem
So, I guess I'm just missing something. But cannot figure out what and where. Please help me.
Try this:-
Create a file named as setup_mail.rb in config/initializers/ folder so that it looks like
config/initializers/setup_mail.rb
and put the below code in that file:-
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:domain => "mysite.com",
:port => 80,
:user_name => "username",
:password => "blahblahblah",
:authentication => :plain
}
ActionMailer::Base.default_url_options[:host] = "mysite.com"
Delete this from config/environments/development.rb and config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}

Resources