Email 'email_calendar' not found in CalendarMailerPreview Rails Mailer Preview - ruby-on-rails

I've created a basic mailer and am not able to see the preview using the following path: http://localhost:3000/rails/mailers/calendar_mailer/calendar_email
Here is my mailer, preview, and email body code:
calendar_mailer.rb:
class CalendarMailer < ActionMailer::Base
default from: "notifications#cogsmart.com"
def calendar_email(user)
#user = user
mail(to: #user.email, subject: 'Your Cogsmart To Do List')
end
end
calendar_mailer_preview.rb:
class CalendarMailerPreview < ActionMailer::Preview
def calendar_email
CalendarMailer.calendar_email(User.first)
end
end
calendar_email.html.erb:
<h1>Thanks #user.name for using Cogsmart, here's your calendar</h1>

As described here
The preview needs to go in the test/mailers/previews folder

Related

Why are emails not sending in my rails app? Internal Server Error) Ch 20. Learn Rails tutorial by Daniel Kehoe

I am on Ch 20 of the Learn Rails tutorial 'Send Mail'. There is a create contact form and a notification email is supposed to be sent to my email address. I have set up configuration as per the book and the email is being generated correctly i the terminal log. however the email never sends to my email inbox. I am getting an internal server error. see below for details.
Completed 500 Internal Server Error in 30831ms
Net::OpenTimeout (execution expired):
app/controllers/contacts_controller.rb:10:in `create'
contacts_controller.rb
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
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
user_mailer.rb
class UserMailer < ApplicationMailer
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 Contact")
end
end
i also have my email address set in the config/secrets.yml file like so :
owner_email: <%= ENV["my_email_address#*******.com"] %>
I have also added the following to my .bashrc file as per the early chater of the book about configuration:
export SENDGRID_USERNAME="user_name"
export SENDGRID_PASSWORD="password"
export MAILCHIMP_API_KEY="long_random_api_key_here"
export MAILCHIMP_LIST_ID="list_id_here"
export OWNER_EMAIL="**********#*********.com"
so as far as i see i have set everything up according to the tutorial but the mail is not sending. any ideas why?
From the above share code description and log trace, it seems like mailer settings is not configured properly.
Note: You need to set the mailer settings based on the environment on which you are working(eg: development.rb)
Follow the below given link for the configuration of mailer settings:
http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration

Sending Email Messages exposes the email addresses of every user to the recipients. How to fix?

I like to send mails such that my JobNotifier/Mailer iterates through the Subscriber's Email List and call deliver "n" times, if that could be the solution to my problem.
Unfortunately, all I have done sends Emails Messages and expose the email addresses of every user to the recipients, which is not suppose to be.
Here are my codes
create method right inside my jobs_controller.rb
def create
#job = Job.new(job_params)
if #job.save
# Deliver the Posted Job
JobNotifier.send_post_email(#job).deliver
redirect_to preview_job_path(#job)
else
render :new
end
end
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default to: Proc.new { User.pluck(:email).uniq },
from: 'FarFlungJobs <no-reply#farflungjobs.com>'
layout 'mailer'
end
app/mailers/job_notifier.rb
class JobNotifier < ApplicationMailer
def send_post_email(job)
#jobs = job
mail( :subject => 'New job posted on FarFlungJobs'
)
end
end
test/mailers/preview/job_notifier_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/job_notifier
class JobNotifierPreview < ActionMailer::Preview
def send_post_email
user = User.all
JobNotifier.send_post_email(user)
end
end
Tried to hop on my browser to test my Mailer using the URL shown below to preview/test my mailer:
http://localhost:3000/rails/mailers/job_notifier/send_post_email
Outcome of my test is this image below (at least if needed to help me with my problem):
Am using Rails
4.2.1
You have to send the email to each user separately. It will take much longer but it won't show other user's emails.
So in your controller, you will have something like this:
def create
#job = Job.new(job_params)
if #job.save
User.pluck(:email).uniq.each do |email|
# Deliver the Posted Job
JobNotifier.send_post_email(#job, email).deliver
end
redirect_to preview_job_path(#job)
else
render :new
end
end
Or you could put the loop inside the mailer
Edit:
You'll need to change your mailer to be able to handle extra argument:
class JobNotifier < ApplicationMailer
def send_post_email(job, email)
#jobs = job
mail(:to => email :subject => 'New job posted on FarFlungJobs')
end
end

Define_method rails mailer

Is it possible to write mailer methods with define_method,because i need almost similar methods,like
notify_user_approved & notify_user_rejected
%w(approved rejected).each do|meth|
define_method("notify_user_#{meth}") do |user,subject|
#url = user.email
#user = user
mail(to: #url, subject: subject)
end
end
Notification is my mailer class name
Notification.notify_user_rejected(User.last,'approved').deliver_now
i having NoMethodError: undefined methodmail' for Notification:Class
when to try run my mailer
whole class
class Notification < ActionMailer::Base
%w(approved rejected).each do|meth|
define_method("notify_user_#{meth}") do |user,subject|
#url = user.email
#user = user
mail(to: #url, subject: subject)
end
end
end

Is it possible to replace default from: with an email from database in Rails Action Mailer?

I am trying to pass on an email from a user to the default from: field so that it looks like it's coming directly from them. Here is what I have right now. Is there any way of bringing in a dynamic variable into the default from field?
class IntroMailer < ActionMailer::Base
default from: "Me#gmail.com"
def intro_email(intro)
#intro = intro
mail(to: #intro.person1_email, subject: 'Testing Intro Email')
end
end
You can override this in the Mailer action's mail method:
class IntroMailer < ActionMailer::Base
default from: "Me#gmail.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', from: current_user.email)
end
end
but a WARNING. Email clients, like Google, are pretty smart at detecting spam. If they see that a specific SMTP server is sending out emails with lots of different 'from' attributes, your spam rating will go up and your emails will be filtered out by spam filters. To get around this, choose one or two default from emails (e.g. support#mywebsite.com & jobs#mywebsite.com) that fit the email's type, and then add a dynamic reply_to attribute instead.
class IntroMailer < ActionMailer::Base
default from: "ourteam#oursite.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', reply_to: full_from(current_user))
end
private
def full_from(user)
address = Mail::Address.new user.email
address.display_name = user.full_name
address.format
end
end
Actually not, it doesn't work at all in Rails 5.XX

Choose specific email template with ActionMailer

I have a problem with choosing specific email template. I have the following mailer:
class NewsletterMailer < ActionMailer::Base
def confirmation_email(subscriber)
#subscriber = subscriber
mail(to: #subscriber.email,
subject: t('.confirmation_subject'))
end
end
And two emails templates that are stored in app/views/newsletter_mailer:
confirmation_email.html.erb
confirmation_email.en.html.erb
Is there any way to set in this mailer action to use this: "confirmation_email.en.html.erb"?
Thanks in advance for any help.
Try this:
mail(to: #subscriber.email,
template_name: 'confirmation_email.en.html.erb',
subject: t('.confirmation_subject'))
You may read more at Mailer Views

Resources