need help with rails actionmailer - *edited* - ruby-on-rails

Ok, i'm a total newbie so please forgive me in advance.
I want to create a very simple rails application. I've created a button that's supposed to send an email to myself. I've had no previous Rails experience so any help you can give is much appreciated.
Here's what I've done so far:
config/environment.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'baci.lindsaar.net',
:user_name => 'myUsernameHere',
:password => 'myPassHere',
:authentication => 'plain',
:enable_starttls_auto => true }
controllers/posts_controller.rb:
def sendMessage
UserMailer.welcome_email().deliver
respond_to do |format|
format.html { render :nothing => true, :status => :ok }
end
end
app/mailers/user_mailer.rb:
class UserMailer < ActionMailer::Base
def welcome_email()
mail(:to => '<my email address here>',
:subject => "Welcome to My Site")
end
end
I've also created the email template in views/user_mailer/welcome_email.html.erb
The problem: I click on the button and I don't receive an email.
Thank you very much in advance.
Matt

In MycontrollerController send_mail is action so this is wrong def send_email(address) just define
def send_email(address)
#address = params[:address]
PostMailer.test_email(#address).deliver
end
I think that is what the above error is

nevermind, i was forgettting
default :from => "<email address here>"
in app/mailers/user_mailer.rb
it works now.

Related

sending email not working with rails

I built an example app to try to make the mailer work. The app is hosted on https://blooming-brushlands-80122.herokuapp.com,
and the source code is here. Here's my config associated with action mailer:
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: 'no-reply#example.com'}
and for sending the mailer
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(#user).deliver
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
The app does not send emails to users.
Does anyone have any idea why?
This answer assumes you're using the free sendgrid Addon from Heroku since I see you're using sendmail. To add it just log into heroku and add it as a free resource. Since you didn't specify whether or not your trying to send emails locally or in production I added configs for both. In your config/environment/development.rb file you should do the following to send from your local:
config.action_mailer.delivery_method = :smtp #Yours used sendmail, change to this.
config.action_mailer.perform_deliveries = true #Needed if this is dev env. file
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['KUNZIG_SENDGRID_USERNAME'],
:password => ENV['KUNZIG_SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
Obviously replace the username and password with your own env. variables assigned by Heroku. Heroku's config. vars can be found under the settings tab. just click the "reveal" button.
Your environment/production.rb file would be something like:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'kunzig.herokuapp.com',
:enable_starttls_auto => true
}
Notice the only change is the domain name and environment variables since these are set automatically by Heroku when you add Sengrid as an addon. Also, change your deliver call in your controller to deliver_now so we know it's not a problem with your background worker config.

Cannot sent reset password mails in rails application with a timeout situation

I'm very new to Rails. Now I have a problem with sending reset password emails on Rails website.
Right now the problem is when I enter a registered email to reset the password, the email cannot be sent.
Net::OpenTimeout in PasswordResetsController#create
I think the problem is about the stmp setting.
Here is some of my code. By the way, I'm doing all these in the development environment.
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host =>
"http://localhost:3000" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "Here I put my intern company",
# :domain => "gmail.com",
:authentication => :plain,
:user_name => "Here I put my actual email",
:password => "The password of my actual email",
:enable_starttls_auto => true
}
password_resets_controller.rb
def create
# #user = User.find_by(email: params[:password_resets]
[:email].downcase)
#user = User.find_by(email: params[:password_reset]
[:email].downcase)
if #user
#user.create_reset_digest
#user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
user.rb
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "naomi<noreply#MYINTERNCOMPANY.com>"
def password_reset(user)
#user = user
mail(to: #user.email, subject: 'Password reset')
end
end
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'noreply#MYINTERNCOMPANY.com'
layout 'mailer'
end
Can anyone help with this?
And also, after this how can I set the production environment so that the website can be deployed on Heroku?

How to setup Action Mailer using SendGrid to send emails to Devise users?

Hi I'm trying to create simple app. After devise user is logged in can create event as soon as create this email will be send to him.
def create
#event = Event.new(event_params)
#event.user = current_user
#event.save
#user= current_user
UserMailer.welcome_email(#user).deliver
respond_with(#event)
end
I've change on production and development files
config.action_mailer.default_url_options = { :host => 'https://mydomiantest.io' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:user_name => 'username',
:password => 'mypassword',
:domain => 'mydomiantest.io',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I've also created user_mailer.rb in mailers
class UserMailer < ActionMailer::Base
default from: 'no-replay#mytestsite.io'
def welcome_email(user)
#user = user
#url = 'mytestsite.io'
mail(to: #user.email, subject: 'Welcome to My Awesome Site')
end
end
It is takes some time after creating event and then I don't have any email, I have created account on site grid what else do I need to do?

Rails 3 Contact Form, undefined method?

I'm building a contact form in Rails 3 using this Railscast: http://railscasts.com/episodes/326-activeattr?view=asciicast
I know that the only thing I'm doing wrong is filling out where the message should be sent. I used the following code to send the message:
mail(:to => "me#myemail.com")
However, this doesn't seem to work, because everytime I submit the form, I get this error:
NoMethodError in MessagesController#create
undefined method `mail' for #<MessagesController:0x00000103734bd8>
Application Trace | Framework Trace | Full Trace
app/controllers/messages_controller.rb:10:in `create'
What should I replace this line with to send the message?
messages_controller.rb
class MessagesController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
UserMailer.contact_message(#message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
def contact_message(message)
#message = message
mail(:to => "myemail#mymail.com", :subject => "New Message")
end
end
setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mywebsite.com",
:user_name => "myemail",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
there's no object created on the mail variable on your controller. Actualy to user Mailer on rails you don't have to instantiate a variable just call you mailer with the method. Like:
YourMailer.send(params)
You can always go to rails guides to see how some stuff works:
http://guides.rubyonrails.org/action_mailer_basics.html
The Message object you're creating is just an object that can be used in a form with validations. You still need to actually create a Mailer object, and fill it with the name/email/message that you get from the #message object.
If you create a Mailer (see Episode 206 - Action Mailer), and you'd do something like this in your controller after creating a MessageMailer
def create
if #message.valid?
MessageMailer.contact_message(#message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
I just answered a similar question here: https://stackoverflow.com/a/17883328/307308
You are missing the from attribute in your mail method.
mail(:from => 'system#mymail.com', :to => "myemail#mymail.com", :subject => "New Message")

Rails SMTPAuthenticationError Action Mailer

I've developed a Rails 3 app and I've recently added an action mailer for when a user signs up.
Testing it locally and it sends an email perfectly, but when I move it to the server at test it for a new user signing up, I get the following error.
I am running Nginx on the server with the original configurations, both local machine and server are running ubuntu 11.10
Net::SMTPAuthenticationError in UsersController#create
535-5.7.1 Username and Password not accepted.
app/controllers/users_controller.rb:26:in 'create'
In users_controller.rb - create - Line 26 is UserMailer.welcome_email(#user).deliver
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to University Sports!"
redirect_to #user
UserMailer.welcome_email(#user).deliver
else
#title = "Sign up"
render 'new'
end
end
In development.rb I have the following code:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'www.mydomain.co.uk',
:user_name => 'email#mydomain.co.uk',
:password => 'email_password',
:authentication => 'plain',
:enable_starttls_auto => true }
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "univeristy.sports#louisgriffiths.co.uk"
def welcome_email(user)
#user = user
#url = "http://www.mydomain:3000"
mail(:to => user.email, :subject => "Welcome to University Sports")
end
end
I had the exact same error and found that my application.yml file was overriding it so I deleted that and now it works fine!

Resources