How to add references header to an email in actionmailer? - ruby-on-rails

I have tried to add it using the headers as
headers "References" => "<message_id>"
But it is not found in the generated email. Moreover i have tried to add the In-Reply-To header and it dosen't show up either. I am using rails 2.3.8.

Try this.
class UserMailer < ActionMailer::Base
def notification(user)
recipients user.email
from "notifications#example.com"
headers {"References" => "<message_id>"}
# code ...
end
end
Check the documentation here in guides for Rails for the syntax how to specify the headers.

Related

How to I create a preview for devise_intiable mailer?

I hooked up devise_invitable gem
And everything is working correctly in my dev environment. I can view the mailer html in the terminal, I can copy the URL with the token it sends. And when I put that in browser and go to that path it works perfectly.
I would however like to create a preview for this so I can preview the mailer as I get that part how I want it.
Here's what I've tried so far.
In test/mailers/previews
and my code is something like this:
class InviteAdminPreview < ActionMailer::Preview
def invite
InviteAdminMailer.invite(Admin.last)
end
end
but this isn't really working because I don't know what the actual invite mailer is called. Because in app/mailers where I would have expected devise_invitable to place this mailer, there is nothing there.
So should I create my own mailer here called InviteAdminMailer? Or is there some other way to do it?
Also will i have to create this .invite method myself in this mailer so this preview will work?
First of all, to create a customized mail for devise_invitable usage, you can reference the article "Customizing for different Invite use cases (emails etc.)" https://github.com/scambra/devise_invitable/wiki/Customizing-for-different-Invite-use-cases-(emails-etc.) from devise_invitable wiki.
According to devise_invitable source code ( https://github.com/scambra/devise_invitable/blob/cf77519ca9d02c112d99fd176a8852272c1b8d99/lib/devise_invitable/models.rb#L210 ), it uses the mailer provided by devise, so there is no devise_inviatble mailer. If you want to preview devise mails, you can do something as below:
From: https://gist.github.com/ThawanFidelis/ac4a215b841619eae7d8
# config/enviroments/development.rb
config.action_mailer.preview_path = "#{Rails.root}/app/mailer_previews"
# app/mailer_previews/devise_mailer_preview.rb
class Devise::MailerPreview < ActionMailer::Preview
def confirmation_instructions
Devise::Mailer.confirmation_instructions(User.first, {})
end
def unlock_instructions
Devise::Mailer.unlock_instructions(User.first, "faketoken")
end
def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end
end
This worked for me
def invitation_instructions
Devise::Mailer.invitation_instructions(User.all.sample, 'faketoken')
end

Edit email views in shoppe

I can't seem to find the shoppe tag in stackoverflow.
I'm using Shoppe gem for rails.
I want to know if there is a way to edit the views for the emails that are being sent when an order is placed in shoppe.
I would like to add an attachment to the email when you accept the order.
Thanks!
It seems that you can just re-define Shoppe's mailer method:
module Shoppe
class OrderMailer < ActionMailer::Base
def received(order)
#order = order
attachment(content_type: 'image/jpeg', body: File.read('image.jpg'))
mail :from => Shoppe.settings.outbound_email_address, :to => order.email_address, :subject => I18n.t('shoppe.order_mailer.received.subject', :default => "Order Confirmation")
end
end
end
Puts that somewhere into your app/initializers. Remember to set your content-type properly.
This is the best and fastest solution for anyone who just wanting to change the actual text like i did.
You can just go directly to the file of the email and edit it. all i did was visit The exact File Here and recreate that file and its path into my own application and changed the words to my pleasing.
https://github.com/tryshoppe/shoppe/blob/master/app/views/shoppe/order_mailer/accepted.text.erb

How can I change the header info of "from"? when using ActionMailer?

I'm using ActionMailer. It's working fine.
But, I've noticed that the owner is clearly stated in header of email sent.
Just like this
Received: (from ftpuser#localhost)
What is it reffered to?
Indeed, some of my rails files's are ftpuser and rest are root.
How should it be?
This is an example and I hope that this will help, in your app/mailers/notifications_mailer.rb you can set default email address.
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#yourdomain.net"
default :to => "info#yourdomain.net"
def new_message(message)
#message = message
mail(:subject => "[Message Confirmation] #{message.subject}")
end
end
Also check you config/environments directory. Mailers can also be configured there.

rails 3 upgrade - You're using the old API in a mailer class

I'm trying to upgrade an application from rails 2.3 to 3.0.6
which has the following code in rails 2.3
class MessageSender < ActionMailer::Base
def send_message(subject,to,from,body,respondent = nil, content_type = 'text/plain')
#content_type = content_type
#subject = subject
#recipients = to
#from = from
# #sent_on = Time.now
#body = {:body_text => body}
end
end
In the upgrade process the code is modified as below
class MessageSender < ActionMailer::Base
def send_message(subjet,to,from,body,respondent = nil,content_type='text/plain')
mail(:to => to, :subject => subject, :from => from, :body => body, :content_type => content_type)
end
end
by referring to this famous blog about using ActionMailer in rails 3.0
And finally ran rake rails:upgrade:check(checks for rails 3 incompatible functions) and it shows
Old ActionMailer class API
You're using the old API in a mailer class.
More information: http://lindsaar.net/2010/1/26/new-actionmailer-api
The culprits:
- app/models/message_sender.rb
(i.e) It says I'm still using Old API
Can somebody explain what am I missing here ?
Or is there any other way to eliminate "You're using the old API in a mailer class" bug ?
FYI: the gems are updated and environment is ruby 1.8.7,rails 3.0.6
Try throwing your code away and write it again using ActionMailer guide. The reason can be as Frederick suggested but also your code doesn't look very rails 3 way ;).
The first thing that comes to my mind is how you pass body and content type. Body can be just variable which you will use in view and content type will be set automatically based on what views are defined.
I would write something like:
class MessageSender < ActionMailer::Base
def send_message(subject, to, from, body)
# #sent_on = Time.now
#body = body
mail(:to => to, :subject => subject, :from => from)
end
end
Then to render a view:
# app/views/message_sender/send_message.text.erb
My nice text email.
And my body is <%= #body %>
As you can read in the guide, you can also create html version as app/views/message_sender/send_message.text.erb.
Rails upgrade checks your code by running some regular expressions against it. This isn't foolproof, for example it's trying to guard against the old style way to set the subject:
subject "foo"
But the tests used to check for that will catch any instance of the word subject followed by a space (except when used as a symbol). Since you've got an argument call subject this could easily happen. The same is true of from.

How can I make my mailer deliver a message to an address containing umlauts under Rails 3?

This is my mailer:
class MyUserMailer < ActionMailer::Base
default from: "me#mail.com",
:content_transfer_encoding => "7bit"
def build_email(user)
mail(:to => user.email,:subject => "Welcome")
end
end
I'm testing with an address containing umlauts: äardvark#mail.com, but I keep receiving the following error, when I try to deliver the email:
'to' parameter is not a valid address. please check documentation
Documentation says that the mailer will use UTF-8 to encode all the fields. How can I make this work?
EDIT: I'm using Rails 3.2.5
Problem was caused by my usage of the mailgun Rails gem. After I switched to using smtp directly, problem went away.

Resources