Rails 3 - Action Mailer not sending message - ruby-on-rails

I have a button 'buy' which links to the 'review' page like so:
<%= button_to 'Buy', review_hvacs_path(:b => true, :h => hvac, :a => params[:a], :s => params[:s]) %>
This calls the review action in the controller, 'hvacs_controller' which contains..
#buy = params[:b]
if !#buy.nil?
#currentHvac = Hvac.find(params[:h])
#supplier = HvacSupplier.find(#currentHvac.hvac_supplier_id)
Notifier.gmail_message(#supplier)
end
I am trying to send the message if the user presses the buy button.
My development environment looks like this:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_smarttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:domain => 'gmail.com',
:username => '<my email address>#gmail.com',
:password => '<my password>'
}
...and my mailer looks like this:
class Notifier < ActionMailer::Base
default from: "user#address.com"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.notifier.gmail_message.subject
#
def gmail_message(supplier)
#greeting = "HVAC Equipment Purchase"
#supplier = supplier
mail(:to => supplier.email, :subject => "HVAC Equipment Enquiry")
end
end
Message:
Notifier#gmail_message
<%= #greeting %>, I am interesting in purchasing replacement equipment, and would like an evaluation.
Would anyone have any insight? I am missing something? If I left out any details, I will post them.

Notifier.gmail_message(#supplier).deliver

Related

Override ActionMailer::Base.smtp_settings on runtime, only for the current session?

In certain situations I'm overriding the ActionMailer::Base.smtp_settings on runtime:
class RegistrationsController < Devise::RegistrationsController
def create
ActionMailer::Base.smtp_settings = {
:address => "address.example.com",
:port => "123",
:domain => "example.com",
:authentication => "login",
:user_name => "foo",
:password => "bar"
}
super
end
end
It seems that I'm not just setting it for that particular session, but also for sessions to come. How can I avoid that? How can I assure that I'm only changing settings for this session?
I don't know if you can change ActionMailer settings for particular session as a whole, but you can change SMTP settings for one email delivery.
In your mailer you can do something like this:
class UserMailer < ApplicationMailer
def registration(params)
smtp_settings = {
:address => "address.example.com",
:port => "123",
:domain => "example.com",
:authentication => "login",
:user_name => "foo",
:password => "bar"
}
mail(
to: params[:to],
subject: "Welcome",
delivery_method: :smtp,
delivery_method_options: smtp_settings
)
end
end
delivery_method: :smtp param doesn't have to be included if your default email delivery method is SMTP.
This should work as expected. It will override smtp settings for just one mailer in this case. You can send the smtp settings through the params if you want to change it only for one particular email.

Ruby on Rails End of File with SMTP

Apologies if the answer is out there, but in the many similar posts I've browsed, I haven't found the answer I'm looking for.
I've inherited a Ruby on Rails application, and it recently began failing to send emails. From what I can gather, this is due to an smtp failure.
I want to send emails from "do_not_reply#mydomain.com" using "myaccount#gmail.com" for the SMTP settings.
In .../config/environments/production.rb I have
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => '<myaccount#gmail.com>'
:password => '<mygmailpassword>'
}
and in .../app/models/ I have a file called user_notifier.rb which contains
class UserNotifier < ActionMailer::Base
def signup_notification(user)
setup_email(user)
#subject += 'Please activate your new account'
#body[:url] = "<mydomain.com>:8080/activate/#{user.activation_code}"
end
def activation(user)
setup_email(user)
#subject += 'Your account has been activated'
#body[:url] = "<mydomain.com>:8080"
end
def reset_notification(user)
setup_email(user)
#subject += 'Link to reset your password'
#body[:url] = "<mydomain.com>:8080/reset_password/#{user.reset_password_code}"
end
def login_reminder(user)
setup_email(user)
#subject += 'Login Reminder'
#body[:url] = "<mydomain.com>:8080"
end
protected
def setup_email(user)
#recipients = "#{user.email}"
#from = "<do_not_reply#mydomain.com>"
#subject = "<subject>"
#sent_on = Time.now
#body[:user] = user
bcc ["<myaccount#gmail.com>"]
end
end
All of this code once worked, so I'm not sure what has changed. As I write this, I'm realizing that the sudden failure might have corresponded to some maintenance on the network, so I don't know how that might affect things.
EDIT: Added the entire UserNotifier class as requested in the comments
Well, I actually managed to solve this one myself.
I needed to add the :domain option in .../config/environments/production.rb
Why it once worked without :domain I still don't know, but I'll take just having the functional product.
The working setup was
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:domain => "gmail.com",
:user_name => '<myaccount#gmail.com>'
:password => '<mygmailpassword>'
}

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

Action Mailer not sending mails although the logs say it is

I have the following code in my registration controller
def new
Rails.logger.debug "#{params[:email]}"
if !params[:email].blank? && !params[:invoke].blank? && params[:invoke].casecmp('Send') == 0
MyMailer.send_email(params[:email]).deliver
end
end
My params[:email] gets populated correctly.
in my_mailer.rb i have the following code
class MyMailer < ActionMailer::Base
default from: 'someaddress#yahoo.com'
def send_email(emailaddress)
#emailaddress=emailaddress
Rails.logger.debug "{#{emailaddress}}"
mail(to: #emailaddress ,subject: 'asdasd')
end
end
I have set the following properties in development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
I have also done the smtp configurations in setup_mail.rb under the initializers
ActionMailer::Base.smtp_settings = {
:address => "smtp.yahoo.com",
:port => 587,
:domain => "yahoo.com",
:user_name => "someaddress#yahoo.com",
:password => "somepass",
:authentication => "plain",
:enable_starttls_auto => true
}
My logs say that the mail has been sent to the respective person but my gmail shows no mail has arrived . Not even in the spams. Is there any configuration settings i am missing out on ?

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