I have a lot of mailings (*Mailer). Everything works on SMTP.
I understand how to set a header for a particular *Mailer method. But how to set the header globally? That is, I need all the letters that my application sends to have my header. And so that this does not conflict with the individual mailing settings.
I tried to find in the documentation (and in google) but did not find anything.
You could do something like this.
class ApplicationMailer < ActionMailer::Base
default from: "email#company.com", "HEADER_KEY" => "VALUE"
end
All your mailers should inherit from ApplicationMailer, which itself inherits from ActionMailer::Base.
In ApplicationMailer you can define default smtp headers, default layout etc.
Here's my application_mailer.rb, to give you some ideas what you can include:
#application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "Site Admin<#{NO_REPLY_EMAIL}>"
layout 'mailer'
def mail
super(options)
end
private
def options
{:'List-Unsubscribe-Post' => :'List-Unsubscribe=One-Click',
:'List-Unsubscribe' => unsubscribe_url,
:subject => t('.subject', org_name: ORGANIZATION_NAME, app_name: APPLICATION_NAME),
:to => "#{#recipient.email}",
:date => Time.now }
end
def unsubscribe_url
params = { :locale => I18n.locale,
:user_id => #recipient.id,
:unsubscribe_code => #recipient.refresh_unsubscribe_code,
:protocol => :https }
#unsubscribe_url = admin_unsubscribe_url( params )
end
For Rails 5:
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
def mail(args)
headers('X-MyCorp-customer' => #customer&.name)
headers('X-MyCorp-env' => Rails.env)
headers('X-MyCorp-app' => 'XRay Insights')
super(args)
end
end
Related
In my Rails 5 app I am trying to send emails with ActiveJob and Sucker Punch:
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def activate
user = User.find_by(:activation_token => params[:id])
user.activate
SendFollowUpEmailJob.perform_in(30, user)
sign_in user
end
end
# app/jobs/send_follow_up_email.rb
class SendFollowUpEmailJob < ActiveJob::Base
include SuckerPunch::Job
queue_as :default
def perform(user)
SupportMailer.follow_up(user).deliver_later
end
end
# app/mailers/support_mailer.rb
class SupportMailer < ApplicationMailer
layout nil
default :from => "admin#app.com"
def follow_up(user)
#user = user
mail :to => #user.email, :subject => t('.subject')
end
end
# app/views/user_mailer/activation.html.erb
<%= link_to(t('views.activate'), activate_user_url(#user.activation_token, :locale => "#{I18n.locale}")) %>
Everything works except that the email is always sent in the default locale rather than the locale that was passed into the activate action.
All emails throughout the application are always sent in the correct locale, so it must down to either ActiveJob or SuckerPunch.
If you can help, please let me know. Thanks.
I'm familiar with I18n within the application, but not so much in mailers. Remember that the mailer is being sent outside the request (ActiveJob), so it has no knowledge of anything that happened there. It looks like you haven't done anything to tell the mailer that it should be sending in a specific locale....
OK maybe this has the answer for you (and me!) https://niallburkley.com/blog/localize-rails-emails/
I18n.with_locale(#user.locale) do
mail(
to: #user.email,
subject: I18n.t('user_mailer.new_follower.subject')
)
end
Here i need to attach one image with mail, the passing image like this
**imageurl** = "https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%22hai%22&choe=UTF-8"
class UserMailer < ActionMailer::Base
default :from => "mail#example.com"
def welcome_email(**imageurl**,bname,mailid)
**attachments['image.png'] = File.read(imageURL)**
mail(:to => mailid,
:subject => "Code for "+bname+"",
:body => "code for bname" )
end
end
end
here i got some attachment error. Have any changes in this attachment?
thanks
i think you have a URL i.e a string which File.read cannot read.
require 'open-uri'
class UserMailer < ActionMailer::Base
def welcome_email(image_url,bname,mailid)
attachments['image.png'] = open(URI.parse(image_url))
...
end
end
The above should do the trick i think.
require 'open-uri'
class UserMailer < ActionMailer::Base
def welcome_email(image_url,bname,mailid)
mail.attachments[image.png] = { :mime_type => type*, :content => open(URI.parse(image_url)}
...
end
end
where type* is the type of attached file in your case it will be('image/png')
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
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
How do I reuse the same action mailer template for multiple mailer "actions"?
In ActionController, you can do
...
render :action => 'another_action'
I'd imagine the same thing can be done in ActionMailer, but I couldn't seem to find the right method. If it's relevant, I'm on Rails 2.3.2.
Thanks!
You're looking for render_message, there is a good example in the API Docs Multipart Message section - pasted below.
class ApplicationMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "system#example.com"
content_type "multipart/alternative"
part :content_type => "text/html",
:body => render_message("signup-as-html", :account => recipient)
part "text/plain" do |p|
p.body = render_message("signup-as-plain", :account => recipient)
p.transfer_encoding = "base64"
end
end
end