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.
Related
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>'
}
I am fighting with setting up the "from" information that is shown in the email in header.
It still shows incorrect email address.
In the mailer class, I manually set the default email address:
class NotificationMailer < ActionMailer::Base
default content_type: "text/html", :from => 'default#email.com'
I also specified this in the method for sending the email(s):
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome!", from: 'default#email.com')
end
But when I receive this email, it's from my-personal-email#gmail.com. I searched through the Rails app where is this email address set up and it's here:
config/initializers/setup_email.rb:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "logistadvise.com",
:user_name => "my-personal-email#gmail.com",
:password => "mypassword",
:authentication => "plain",
:enable_starttls_auto => true
}
Emails are sent out well, but worries me that I am unable to properly set the "from" header.
How to do that?
Thank you.
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
I have a Rails application which uses a form to collect message details and emails the message to my gmail/yahoo account.
I have added this to setup_mail.rb in config/initializers:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "my_user",
:password => "my_pass",
:authentication => "plain",
:enable_starttls_auto => true
}
and use the controller like so for the POST route:
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver!
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
redirect_to('#')
end
end
The Message Parameters are accepted from the form cleanly like so:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4xgV0umvaTPzYxd18qdJq/GT7QCdXjrPTGR7D9R3AC4=", "message"=>{"name"=>"Deepak", "email"=>"deepakm.ccx#gmail.com", "subject"=>"Hi", "body"=>"Hi"}, "commit"=>"Send"}
No matter if I use Yahoo credentials or Gmail Credentials in the setup, I keep getting the same error message. I commented all the settings in ActionMailer::Base.smtp_settings and still get this issue. This leads me to believe the issue could be related to something else outside this code but I have no clue whatsoever.
-Deepak
You mention your code is in config/initializers - we have ActionMailer working, and it sends through Gmail, except we put this into our config/environments/developer.rb file:
#Send Email In Development (Use Gmail's Servers)
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "*****.co.uk",
:user_name => "******#gmail.com",
:password => "******",
:authentication => :plain,
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
:host => "localhost:3000"
}
Maybe you could try copy-and-pasting the above code?
While I am still unable to send emails via ActionMailer still, I thought I would post an alternative.
https://github.com/nu7hatch/gmail
Works well and is easier because you dont need to use any config files.
Thanks.
I'm trying to send out "Welcome Emails" to my users, however, the default :from is not working. It is using the user_name I specify in the config/application.rb file.
This is the code I currently have.
config/application.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:user_name => "myemail#gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => 'email_i_want_to_send_from#gmail.com'
def welcome_email
mail(:to => "myemail#gmail.com", :subject => "welcome")
end
end
Instead of receiving an email from email_i_want_to_send_from#gmail.com, a user would receive an email from myemail#gmail.com. (Yes, I am using actual email addresses and passwords when testing).
Anyone have any ideas as to why this is happening? Thanks.
Google doesn't allow you to "spoof" a from address. I used Send Grid to send out my emails.