Another rails mailer not working issue - ruby-on-rails

First in my project, I can receive the registration confirmation email sent by devise gem and mandrill service. So I assume my email config settings are correct. However, my own send_mail function just cannot send email successfully.
My mailer settings:
config.action_mailer.smtp_settings = {
address: "smtp.mandrillapp.com",
port: 587,
domain: Rails.application.secrets.domain_name,
authentication: "plain",
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_controller.asset_host = 'localhost:3000'
config.action_mailer.asset_host = 'http://localhost:3000'
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true
Mailer Class:
class BidMailer < ActionMailer::Base
default from: "service#xxx.com"
def send_mail(bid)
#bid = bid
mail(to: 'xxxxx#gmail.com', subject: 'Bid Accepted', reply_to: 'service#xxx.com')
end
end
In the controller I simply call BidMailer.send_mail(bid).deliver. I also have the html file for in the view for the email body.
I can see this in my server console log:
Sent mail to xxx#gmail.com (24.0ms)
Date: Wed, 11 Mar 2015 22:39:45 +1100
From: service#xxx.com
Reply-To: service#xxx.com
To: xxx#gmail.com
Message-ID: <550029815f07_782a3fec29fa23c8413c#Shanes-MacBook-Pro.local.mail>
Subject: Bid Accepted
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
And also I can see similar email log when devise sends out registration confirm email.
But I totally dont get why I cannot get any email sent by my send_mail function?
Anyone could give some help? Thanks heaps!

Related

Rails app claims to successfully send email but never completes task

I'm working on a Rails app which should send a confirmation email when a new user registers.
My mailer looks like this:
def notify_email(volunteer)
#volunteer= volunteer
#admin = Volunteer.first
mail(to: #admin.email, subject: 'New Application from ' + volunteer.first_name+ '!', body:
'You have new applicants, please go check on your admin dashboard!
You can contact ' + #volunteer.first_name+ ' at: ' +#volunteer.email+ '.')
end
end
In my production.rb file I have email configurations set as follows(gmail username/password replaced with x's):
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:8080' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => 'xxxxxx',
:password => 'xxxxxxx',
:authentication => "plain",
:enable_starttls_auto => true
}
My controller has this code:
def create
#admin= Volunteer.first
#volunteer = Volunteer.create(volunteer_params)
#volunteer.save
if #volunteer.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(#volunteer).deliver_now
redirect_to '/volunteer'
end
And finally when I test the app on the local server the console gives me this information(emails x'ed out again):
NotifyMailer#notify_email: processed outbound mail in 1.5ms
Sent mail to xxxx#xxxx.org (2004.5ms)
Date: Sun, 06 Jan 2019 16:04:49 -0600
From: xxxxxxx#gmail.com
To: xxxxx#xxxxx.org
Message-ID: <5c327b817c32e_3de83ca21e89202#LAPTOP-N2MQ7EL0.mail>
Subject: New Application from Rachel!
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Can anyone give me insight into what I'm missing? I've checked several forums and threads and it seems like I'm following the right steps.
Thanks in advance.
Unable to comment, but what happens if you call
NotifyMailer.notify_email(#volunteer).deliver
instead of
NotifyMailer.notify_email(#volunteer).deliver_now
I believe deliver_now relays on a background worker like sidekiq,
plus try the
gem "letter_opener" which will open a new tab in your browser to check your emails.

Action mailer not sending/receiving email rails 5

I'm trying to create a simple blog (using rails 5) and want to send email updates when to users when I post something new. I'm trying to use actionmailer and I'm using devise to register users. I'm trying to set it up with just the first user initially.
When I look at my local server the email appears to be sending but it's not received. Any advice would be very welcome I've been stuck a while.
UserNotifierMailer#sample_email: processed outbound mail in 410.4ms
Sent mail to myemail#gmail.com (853.2ms)
Date: Wed, 27 Jul 2016 12:05:33 +0100
From: from#example.com
To:myemail#gmail.com
Message-ID: <5798957d9de31_ae813ff962abfe1466438#Unknown-7c-d1-c3-78-04-d2.home.mail>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5798957d951e2_ae813ff962abfe1466312";
charset=UTF-8
Content-Transfer-Encoding: 7bit
My posts controller looks like this:
def create
#user = User.first
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Your post has been created!"
UserNotifierMailer.sample_email(#user).deliver
redirect_to posts_path
else
flash[:alert] = "Your new post couldn't be created! Please check the form."
render :new
end
end
My mailer looks like this:
class UserNotifierMailer < ApplicationMailer
default from: "from#example.com"
def sample_email(user)
#user = user
#url = 'http://www.google.com'
mail(to: #user.email, subject: 'Sample Email')
end
end
and in my development.rb I have these settings:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: 'localhost', port: 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: "my_gmail_username",
password: "my_gmail_password"
}
In your configured Gmail account:
Go to Account Settings Than
Go to Sign-in & security, followed by Connected apps & sites
At last in Allow less secure apps ... Check it.
Check Once And Try
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
...
config.action_mailer.delivery_method = :smtp
I don't config any of the above and it still works.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
and have to .deliver_later or .deliver_now, .deliver not work

Actionmailer says email sent but it never gets there (Rails 3)

Actionmailer won't send emails in development, even though they say 'sent' in the console.
gemlist:
gem 'send_grid'
development.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => "localhost:3000" }
setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '587',
:domain => "heroku.com",
:user_name => "app123456789#heroku.com",
:password => "asdfasdfasdf",
:authentication => :plain,
:enable_starttls_auto => true
}
app/mailers/user_mailer.rb:
class UserMailer < ActionMailer::Base
include SendGrid
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered", :from => "media#myemail.com")
end
end
app/views/user_mailer/registration_confirmation.html.haml:
Confirm your email address please!
= accept_invitation_users_url(email_token: #user.email_token)
console:
Sent mail to alain#myemail.com (656ms)
Date: Tue, 11 Feb 2014 13:35:11 -0500
From: media#myemail.com
To: alain#myemail.com
Message-ID: <52fa6d5f667ea_27ce5564820279f3#Ice-box.mail>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {}
Confirm your email address please!
http://localhost:3000/users/accept_invitation?email_token=_JL8DQ2GItxl
Redirected to http://localhost:3000/
Completed 302 Found in 1396ms (ActiveRecord: 192.2ms)
you can try setting:
config.action_mailer.raise_delivery_errors = true
to see if it raises any error on delivery. Otherwise try using other email or check it in you junk folder
Or try this in development mode to check if there is any issues there(it seems fine as you can see the confirmation in the console)
https://github.com/ryanb/letter_opener
Add config.action_mailer.raise_delivery_errors = true to production.rb or development.rb and restart your app to make the changes working
Put this before your smtp settings:
config.action_mailer.raise_delivery_errors = true
Add ".deliver" at the end where you are calling the email method i.e.:
UserMailer.welcome_email(#user).deliver
It will raise exception that will help you to find the failure cause.
If you are using background job to send email, You can put this snippet in exception block and use ExceptionNotification gem to send email with details which will help you to catch and find the reason of failure easily.

How to send and get mail using Mandrill?

I'm using Ruby on Rails, could you teach me how to use Mandrill to send and receive mail.
My problem is when user input text and press submit, I want message to be sent to my mail.
I have read documentation, but I don't understand it.
This is my development.rb, same as production.rb
ActionMailer::Base.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => 'myemail#gmail.com',
:password => 's82SlRM5dPiKL8vjrJfj4w',
:domain => 'heroku.com',
:authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp
user_mailer.rb:
class UserMailer < ActionMailer::Base
default from: "from#example.com"
def welcome
mail(to: "morumotto26#gmail.com", subject: "Welcome", body: "Have a nice day")
end
end
and in controller:
def index
UserMailer.welcome.deliver
end
My logs file look like this:
Started GET "/users/" for 127.0.0.1 at 2014-01-21 16:14:10 +0700
Processing by UsersController#index as HTML
Sent mail to morumotto26#gmail.com (2008.0ms)
Date: Tue, 21 Jan 2014 16:14:10 +0700
From: myemail#gmail.com
To: morumotto26#gmail.com
Message-ID: <52de3a62c89b2_5e8c9ea1881631#tnkjapan10.mail>
Subject: Welcome
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Have a nice day
Rendered text template (0.0ms)
Completed 200 OK in 2018ms (Views: 1.0ms | ActiveRecord: 0.0ms)
First of all you will need to create account on the mandrill.com.
After log in, select type of integration: SMTP or API. SMTP will suite you in most of the cases.
Click SMTP, and create you API key.
Then in your development.rb or production.rb file
add these lines:
config.action_mailer.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => 'domain.com',
:authentication => :plain
}
That's basically all. Now you can send email with Mandrill.
EDIT
Also try to add to your to your environment files these line to perform deliveries and raise delivery errors if any. Also add your default_url_options - in development localhost:3000, in production heroku.com
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
Restart your app before testing
EDIT 2
If you want ActionMailer to send email on submit button click, then you will need to move UserMailer.welcome.deliver to create action of the respective controller.
def create
if #object.save
UserMailer.welcome.deliver
else
render "new"
end
end

devise forgot password email not received

I am facing issue with devise forgot password.I am not receiving any email though devise displays messsage that "you will receive an email with instructions about how to reset your password in a few minutes".
Ruby-1.9.3 Rails 3.2 devise 2.2.4
My environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:domain => 'xyz#gmail.com',
:user_name => 'xyz#gmail.com',
:password => 'abcde',
}
My environment.rb
ActionMailer::Base.delivery_method = :smtp
My intializers/devise.rb
config.mailer_sender = "xyz#gmail.com"
And development.log shows
Sent mail to xyz#gmail.com (3205ms)
Date: Wed, 26 Jun 2013 23:33:01 +1000
From: xyz#gmail.com
Reply-To: x#gyzmail.com
To: xyz#gmail.com
Message-ID: <51caed8dd99a5_15365823b9268927#nbnco-U01.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello xyz#gmail.com!</p>
<p>Someone has requested a link to change your password. You can do this the link below.</p>
<p>Change my password</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 3423ms (ActiveRecord: 0.0ms)
As have been mentioned above in development mode mails aren't being send for real, if you're curious how that's obtained (and maybe want to override that behavior) look into the following file
config/environments/development.rb:
#don't send emails in development
config.action_mailer.perform_deliveries = false
Of course it would be better to use some mailtrap but if you want to just quickly see mail in your mailbox just change that false to true and here it goes if you've got properly configured mailer of course.
Edit:
That's my config for gmail and it works, there is no domain param so probably it's making trouble:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<login>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
Edit2: And don't forget to restart your server ;)
In development with your configuration emails are not sent but are just visible in the logs. You can try and use a service such as:
http://mailtrap.io/pages/ruby_landing
https://github.com/ryanb/letter_opener
https://github.com/37signals/mail_view

Resources