How to receive an email from users in rails? - ruby-on-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

Related

Rails mailer does not send email

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

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>'
}

In production, contact form: ActionMailer does not send email. ArgumentError (At least one recipient (To, Cc or Bcc) is required to send a message)

I created simple contact form (similar to: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/) and in my development, it works fine (using letter opener)
but in my production, it does not send the mail.
I searched some solutions but I could not find solution.
Why not sending email even though to: is defined in models/mailers/contact_mailer.rb ?
in heroku logs the parameters looked fine but it could not find to: ... Please help me.
entire repo
https://github.com/yhagio/yhagio
actual contact form
http://yhagio.herokuapp.com/contact
config/environments/production.rb
config.action_mailer.default_url_options = { :host => "yhagio.herokuapp.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:domain => 'yhagio.herokuapp.com',
:port => 587,
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true,
}
models/mailers/contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: ENV['MYGMAIL']
def contact_message(message)
#message = message
mail from: message.email, subject: message.subject
end
end
message.rb
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
validates_presence_of :subject, :body, :name
validates :email, presence: true, format: { with: /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
attr_accessor :name, :email, :body, :subject
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
contact_mailer.rb
class ContactController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
ContactMailer.contact_message(#message).deliver
redirect_to root_path
flash[:success] = "Message was successfully sent."
else
flash[:error] = "Please fill all fields."
render :new
end
end
end
heroku logs
app[web.1]: Started POST "/contact" for 173.176.46.53 at 2013-07-22 23:14:50 +0000
Parameters: {"utf8"=>"✓", "authenticity_token"=>"czYfbExDwXdHvzKP7fa3LMV50CbucTGwxwV/sZ22E/c=", "message"=>{"name"=>"test", "email"=>"test#test.com", "subject"=>"hello", "body"=>"https://github.com/yhagio/yhagio/commit/c8a09d769df43a58f82959fc1e72e3c88b79821d"}, "commit"=>"Send"}
Processing by ContactController#create as HTML
Rendered contact_mailer/contact_message.html.haml (2.4ms)
at=info method=POST path=/contact host=yhagio.herokuapp.com fwd="173.176.46.53" dyno=web.1 connect=8ms service=76ms status=500 bytes=643
Sent mail to (10ms)
Completed 500 Internal Server Error in 31ms
app/controllers/contact_controller.rb:11:in `create'
ArgumentError (At least one recipient (To, Cc or Bcc) is required to send a message):
UPDATE: July 24
I tried Mailgun as modified in config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => 'smtp.mailgun.org',
:domain => 'yhagio.herokuapp.com',
:port => 587,
:user_name => ENV['MAILGUN_SMTP_LOGIN'],
:password => ENV['MAILGUN_SMTP_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true,
}
I think the domain should be gmail.com instead of yhagio.herokuapp.com.

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.

Rails 3 - Action Mailer not sending message

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

Resources