I am building a job application in Rails and I need to attach resumes(cv) that are uploaded in a job application as an email attachment. All files in the application are uploaded to Cloudinary using CarrierWave. Emails are handled by Action Mailer. In ActionMailer, I have
class ApplicantMailer < ApplicationMailer
def applicant_info(applicant)
#applicant = applicant
mail(to: #applicant.job.email, subject: 'Applicant Details')
end
end
In my mailer view template, I have
<p>Hi Recruiter, in this email, you would find the resume of the latest applicant</p>
<p>Resume: <%= cl_image_tag(#applicant.resume, :attachment=>true) %></p>
At the moment, this does not render the cloudinary file as an attachment in the email. I need to be able to render the resume as a readable attachment file.
Something like this should work. I don't think you need that helper
class ApplicantMailer < ApplicationMailer
def applicant_info(applicant)
#applicant = applicant
attachments[ open(applicant.resume) ]
mail(to: #applicant.job.email, subject: 'Applicant Details')
end
end
template
<p>Hi Recruiter, in this email, you would find the resume of the latest applicant</p>
<p>Resume: <%= image_tag #applicant.resume %></p>
Related
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
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
I have a cab booking platform created in Rails. I am using the Mandrill smtp settings in production to send booking confirmation mails to users of my platform. Then I generated a mailer called user_mailer with the following code:
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
Then I created booking_confirmation.html.erb page in user_mailer views with some generic content inside. Finally I called the user_mailer in one of my controllers as follows:
UserMailer.booking_confirmation(current_user).deliver
My problem is that when I want to include more details (such as Travel date, Travel time, etc.) within the mail delivered to my customer. I am trying this within my booking_confirmation.html.erb page: <%= #user.bookings.last.date %> to display the Travel Date to the customer. But this doesn't get displayed. Why is it so?
I would pass in the booking like
UserMailer.booking_confirmation(current_user, booking.id).deliver
then in the class UserMailer I would do
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user, booking)
#user = user
#booking = Booking.find(booking)
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
now on your erb you should have #booking.date to use
Its not a mandrill issue.something wrong with application code.
Check whether your following code has the date value present for last bookings.
#user.bookings.last.date
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
I filled the pdf using pdf_forms gem , now i want to convert that pdf into binary and want to send via email.
you dont need to convert a pdf to binary to attach it to an email. just add this line inside the method of your mailer
attachments["filename.pdf"] = File.read("/path/to/filename.pdf")
You probably just want to send the pdf as an attachment. The official Rails ActionMailer has an example on how to do this
class UserMailer < ActionMailer::Base
def welcome_email(user)
#user = user
#url = user_url(#user)
attachments['terms.pdf'] = File.read('/path/terms.pdf')
mail(:to => user.email,
:subject => "Please see the Terms and Conditions attached")
end
end
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments