I want to show a pretty name in the e-mail clients
This should do the trick!
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email
# PRETTY NAMES
from "Prettiest Pony <prettypony#imaginarium.tld>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {:user => user, :url => "http://example.com/login"}
end
end
See ActionMailer basics for more information
Related
Im trying to pass the current_employer.id into action mailer. So i cans end an email to users that belong to the Employer.
my mailer looks like this.
class ScheduleMailer < ActionMailer::Base
default to: Proc.new { Employee.where(:employer_id => current_employer.id
).pluck(:email) },
from: 'noreply#scheduled.com'
def schedule_post_reg(employ)
mail( :subject => "Your schedule has been posted.")
end
end
I get this error
NameError (undefined local variable or method `current_employer' for #<ScheduleMailer:0x0000010cec0880>):
app/mailers/schedule_mailer.rb:3:in `block in <class:ScheduleMailer>'
app/mailers/schedule_mailer.rb:10:in `schedule_post_reg'
app/controllers/schedules_controller.rb:235:in `approve_shift'
Any suggestion on how to pass the current_employer.id not the actionmailer would be greatly appreciated.
The Mailer has no direct access to the request context. The reason is because an email is not necessarily a result of an HTTP request. If you deliver the email from the CLI, for instance, there is no HTTP request.
You need to pass the employer as argument of the mailer.
class ScheduleMailer < ActionMailer::Base
default from: 'noreply#usescheduled.com'
def schedule_post_reg(employee, employ)
mail({
:to => employee.email
:subject => "Your schedule has been posted.")
})
end
end
If you need to send the same email to different employees, given a single employer, you can either pass the employer and pluck all the emails into the "to:" field (but all the recipients will see all the other emails)
def schedule_post_reg(employer, employ)
mail({
:to => Employee.where(:employer_id => employer.id).pluck(:email)
:subject => "Your schedule has been posted.")
})
end
or use bcc, or loop all the emails and call the mailer once for every employee email to generate a different email for each employee.
In Controller write somethigng like this
#employer = Employer.find_by_id(params[:id])
ScheduleMailer.schedule_post_reg(employ, #employer).deliver
In Mailer
def schedule_post_reg(employ, employer)
employees_emails = Employee.where(:employer_id => employer.id
).map(&:email).join(",")
mail(:to => employees_emails, :subject => "Your schedule has been posted.")
end
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
This is a total newbie question, but I wonder if someone could assist with setting up a mailer. I have a model 'users' and nested underneath it a 'contacts' model, (with a has_many/belongs_to relationship).
I am now trying to create a mailer which will be triggered by a specific action (create post) on a user page, and will email all the contacts belonging to that user. But I can't crack the syntax required on the mailer - I've tried setting recipients to #user.contacts.all and I've tried looping through them as with this solution. Can anybody advise on the cleanest way to do it?
Here's the code I have so far:
Posts controller:
after_create :send_contact_email
private
def send_contact_email
ContactMailer.contact_email(self).deliver
end
contact_mailer (this is my latest attempt, taken from the RoR site - I suspect this is NOT the best way to do it...)
class ContactMailer < ActionMailer::Base
def contact_email(user)
recipients #user.contacts.all
from "My Awesome Site Notifications <notifications#example.com>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {}
end
end
And then a basic message for the contact_email.html.erb.
The current error is:
NoMethodError in UsersController#create_post
undefined method `contacts' for nil:NilClass.
Any advice you can offer would be really gratefully received!
* Update *
Following Baldrick's advice, the contact_email method is now:
class ContactMailer < ActionMailer::Base
default :to => Contact.all.map(&:contact_email),
:from => "notification#example.com"
def contact_email(user)
#user = user
mail(:subject => "Post added")
end
end
There's a typo: you are using #user instead of user in the contact_email method.
It may not be the only problem, but at least it's the cause of the error message "undefined method 'contacts' for nil:NilClass "
Update
So with the right syntax, remove the :to from default options, and set it in the contact_emailmethod, with the contacts of your user:
class ContactMailer < ActionMailer::Base
default :from => "notification#example.com"
def contact_email(user)
#user = user
mail(:subject => "Post added", :to => user.contacts.map(&:contact_email),)
end
end
class ContactMailer < ActionMailer::Base
default :to => Contact.all.map(&:contact_email),
:from => "notification#example.com"
def contact_email(user)
recipients = user.contacts.collect(&:contact_email).join(',')
mail(:subject => "Post added", :to => recipients)
end
end
class JobMailer < ActionMailer::Base
default :from => "emailer#email.com"
def new_job_email_for_client
#
#
#url = "http://simplesite.com/users/login"
mail(:to => #???,
:subject => "You have created a new case on simplesite.")
end
end
I would like each user to receive an email each and every time he/she creates a "job." In other parts of the application, I can access #user and user.email and such, but in the mailer I'm getting "undefined errors."
How can I access the current users email address in the mailer (taking into consideration that Devise is in control of Users)?
I'm not sure if this is a great way of doing it, but this is how I got it working:
def new_job_email_for_client(user_email)
#url = "http://simplesite.com/users/login"
mail(:to => user_email,
:subject => "You have created a new case.")
end
In rails3 w ActionMailer, I want to send a .txt file attachment. The challenge is this txt file does not exist but rather I want to create the txt file given a large block of text that I have.
Possible? Ideas? Thanks
It's described for files in the API documentation of ActionMailer::Base
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
But that doesn't have to be a File, it can be a string too. So you could do something like (I'm also using the longer Hash-based form where you can specify your own mimetype too, you can find documentation for this in ActionMailer::Base#attachments):
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['filename.jpg'] = {:mime_type => 'application/mymimetype',
:content => some_string }
mail(:to => recipient, :subject => "New account information")
end
end
First the method to send email
class ApplicationMailer < ActionMailer::Base
def welcome(user, filename, path)
attachments[filename] = File.read(path)
mail(:to => user.email, :subject => "New account information")
end
end
Call the method with the params
UserMailer.welcome(user, filename, path).deliver