I generated a action mailer from the ruby guide. However, when I try doing a test run on deployed site through Heroku, I get an error saying something went wrong and to check the logs.
I commented out the method that calls for the delivery of the email and sign up works fine. The email is practically a welcome email. I'm not sure where to go from here.
this is from my ActionMailer
default from: "from#example.com"
def welcome_email(user)
#user = user
#url ='https://inyourshoes.herokuapp.com/signup'
mail(to: #user.email, subject: 'Welcome to InYourShoes!')
end
end
This is from my usercontroller
def create
#user = User.new(user_params)
if #user.save
UserMailer.welcome_email(#user).deliver
sign_in #user
flash[:success] = "Welcome to InYourShoes!"
redirect_to #user
else
render'new'
end
end
Basically,i want a email to be sent once the user successfully signs up on the website. Your help is greatly appreciated
Assuming you're using Ruby >= 2.0, try:
default({from: "from#example.com"})
Otherwise:
default({ :from => "from#example.com" })
Related
I am new to ruby on rails and I follow the book Learn-ruby-on-rails by Daniel Kehoe. I have set up my sengrid login details correctly on the Ubuntu enviroment. echo $ SENDGRID_USERNAME returns my username correctly.
However, I still get "SMTP-AUTH requested but missing user name" error when I submit the contact form. I have tried to hardcode the login details and I still get the same errors. my configuration settings i smtp.sendgrid.net on port 587 and I allow send mails in development.
Please what am I not doing right.
Thanks a lot.
My user_mailer.rb is as shown:
class UserMailer < ActionMailer::Base
#default :from => "do-not-reply#example.com"
def contact_email(contact)
#contact = contact
mail( to: => Rails.application.secrets.owner_email, from: => #contact.email, subject: => "Website Visit")
end
end
while the contacts_controller.rb is shown below:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(secure_params)
if #contact.valid?
UserMailer.contact_email(#contact).deliver_now
#TODO send message
flash[:notice] = "Message sent from #{#contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
The problem arose from an omission in the config/enviroments/development.rb file
I replaced
user_name: Rails.application.secrets.email_provider
with:
user_name: Rails.application.secrets.email_provider_username
and the problem was solved
I'm following the railscast http://railscasts.com/episodes/61-sending-email-revised which states that I should add
UserMailer.signup_confirmation(#user).deliver
to
def create
#user = User.new(params[:user])
if #user.save
redirect_to #user, notice: "Signed up successfully."
else
render :new
end
end
after the line if #user.save ... But what if I am using devise? Do I need to add UserMailer.signup_confirmation(#user).deliver
to another place where the Devise gem is hiding the equivalent of a users_controller ?
Without Devise
Since, you are following the RailsCasts episode for Sending Email then in that case all you need to do is update UsersController#create as below:
def create
#user = User.new(params[:user])
if #user.save
UserMailer.signup_confirmation(#user).deliver ## Add this
redirect_to #user, notice: "Signed up successfully."
else
render :new
end
end
With Devise
Option #1 Sending a welcome confirmation mail
What you can do here is, override the after_sign_in_path_for method in ApplicationController as below:
class ApplicationController < ActionController::Base
## ...
protected
def after_sign_in_path_for(resource)
UserMailer.signup_confirmation(#user).deliver ## Add this
your_path(resource) ## Replace your_path with the path name where you want to send the user after sign in
end
end
Option #2 Sending a welcome confirmation mail with confirmation token
For this you will need to use Devise built-in Confirmable module.
Confirmable: sends emails with confirmation instructions and verifies
whether an account is already confirmed during sign in.
Refer to Confirmable Documentation
I am attempting to send a confirmation email to a newly registered user using Rails 4.0.1 and Ruby 2.0.0.
I am not getting any errors but the mail just is not sending. Here is the relevant code:
config/environments/development.rb
...
config.action_mailer.smtp_settings = {
:authenication=>:plain,
:address=>"smpt.mailgun.org",
:port=>587,
:domain=>"sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:user_name=>"postmaster#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:password=>"6j3c9l35tu33"
}
app/model/user.rb
...
def create
#user=User.new(user_params)
if #user.save
ModelMailer.account_activation(#user).deliver
redirect_to lessons_url
else
render :new
end
end
mailers/model_mailer.rb
class ModelMailer < ActionMailer::Base
default from: "me#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org"
def account_activation(user)
#user = user
mail to: "myemail#gmail.com", subject: "Account Activation"
end
end
Any help would be appreciated.
I'm trying to build a contact form using the approach I saw in the Railscast about ActiveAttr (which gives me a Message model that isn't backed by a database).
This code is in the controller for Messages:
class MessagesController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
# TODO send message here
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
I don't know what kind of code I need to write in order for the contact form input (the message) to be sent to a specific email address during the create action. Would someone please give me a general overview of what I need to know or need to set up in order to make this work? Thank you.
You can add something like this.
def create
#message = Message.new(params[:message])
if #message.valid?
UserMailer.send_message(#message).deliver
redirect_to whatever_path, notice: "Message sent."
else
render "new"
end
end
You will need to create a Mailer. It's pretty easy and Rails already has a great guide on how to do this. http://guides.rubyonrails.org/action_mailer_basics.html
Create a mailer
rails generate mailer UserMailer
Then in your UserMailer app\mailers\user_mailer.rb
def send_message(message)
#message = message
email = "myemail#example.com"
mail to: email, subject: "You have received a message"
end
Then create your email view app\views\user_mailer.txt.erb
You have a new message
Your message says
<%= #message.whatever_attribute %>
I am attempting to create beta invitations using the structure from railscasts episode 124, updated for rails 3.2.8.
Currently, the invitation email gets sent, but does not contain the url (which includes the invitation token) for users to follow to sign up because the instance variable I am creating in ActionMailer (#invitation_link) is nil in the view. Inspecting #invitation_link in the ActionMailer controller shows that it is pointing to the correct url, but it is nil in the view.
I have also checked out the following questions and none of the solutions have worked for me:
How do you use an instance variable with mailer in Ruby on Rails?
https://stackoverflow.com/questions/5831038/unable-to-access-instance-variable-in-mailer-view
Actionmailer instance variable problem Ruby on Rails
ActionMailer pass local variables to the erb template
Relevant code snippets below:
invitations_controller.rb
class InvitationsController < ApplicationController
def new
#invitation = Invitation.new
end
def create
#invitation = Invitation.new(params[:invitation])
#invitation.sender = current_user
if #invitation.save
if signed_in?
InvitationMailer.invitation(#invitation).deliver
flash[:notice] = "Thank you, invitation sent."
redirect_to current_user
else
flash[:notice] = "Thank you, we will notify when we are ready."
redirect_to root_url
end
else
render :action => 'new'
end
end
end
in invitation_mailer.rb file
class InvitationMailer < ActionMailer::Base
default from: "holler#thesite.com", content_type: "text/html"
def invitation(invitation)
mail to: invitation.recipient_email, subject: "Invitation"
#invitation_link = invited_url(invitation.token)
invitation.update_attribute(:sent_at, Time.now)
end
end
views/invitation_mailer/invitation.text.erb
You are invited to join the site!
<%= #invitation_link %> # INSTANCE VARIABLE THAT IS NIL IN VIEW
routes.rb (only showing relevant line)
match '/invited/:invitation_token', to: 'users#new_invitee', as: 'invited'
try this way
This is your InvitationMailer
def invitation(invitation)
#invitation = invitation
mail(:to => #invitation.recipient_email, :subject => "Invitation")
end
now, in your InvitationsController
if signed_in?
#invitation.update_attribute(:sent_at, Time.now)
InvitationMailer.invitation(#invitation).deliver
...
else
...
end
now, views/invitation_mailer/invitation.text.erb
You are invited to join the site!
<%= invited_url(#invitation.token) %> # INSTANCE VARIABLE THAT IS NIL IN VIEW
try this...
#invitation_link = invited_url(invitation.token, :host => "localhost:3000")