Edit email views in shoppe - ruby-on-rails

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

Related

ActionMailer Observer: pass extra attributes possible?

I have an ActionMailer Observer that gets triggered on each email and writes some information to a log database table to keep track of who sends emails. I want to add some metadata to this like logged in user, type of email, etc.
class MailObserver
def self.delivered_email(message)
if message.header[:client_id]
EmailLog.create!(:client_id => message.header[:client_id].to_s,
:to => message.to ? message.to.join(',') : nil,
:cc => message.cc ? message.cc.join(',') : nil,
:bcc => message.bcc ? message.bcc.join(',') : nil,
:subject => message.subject.to_s,
:content => message.multipart? ? message.text_part.body.decoded : message.body.decoded,
:reference_type => message.header[:reference_type].to_s,
:reference => message.header[:reference].to_s,
:user_id => message.header[:user_id].to_s)
end
end
end
ActionMailer::Base.register_observer(MailObserver)
All this information is available the moment the mail is created.
I currently pass this data via the message.header, but then the values show up in the actual email that is delivered
Is there a better way to pass information from the ActionMailers to the Observers while preventing this data from actually being sent?
Looking through the mail gem, there isn't a lot you can do with meta data on the Mail object besides the headers.
One approach I considered was persisting the message_id along with user details, etc and then retrieving it again in the observer and perform logging after that.
Another I was toying with was to set the message id myself and load it with the user id, etc but that was much the same as setting headers.
Possibly, you could set the header then unset it again in an interceptor.
You can consider creating new key-value pair inside mail method in the specific mailer class itself.
Example:
class UserMailer
.....
mail(to: email, subject: 'Your subject', user_id: user.id, client_id: client.id)
end
I tried this and it worked. But it appends extra key-values to the response header.
Have you considered using a callback on the mailer itself?
class MyMailer < ApplicationMailer
after_action :log_email!
...
private
def log_email!
EmailLog.create! # should be able to access instance variables from your mailer method here to reference what you need
end
end
Hope that helps!

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.

Rails 3: Contact form to send messages from User Profiles

I worked through some basic tutorials on Rails 3. The goal is a community-website on abilities and activities. I am using Devise for authentication. The creation of user profiles with avatars worked well (thanks to paperclip).
As a next step, I want to enable registered users to send an e-mail to a user from his (or her) profile page. I found a great tutorial on creating a contact form using Google Apps:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
The mailer class in this tutorial looks like:
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
My question: What is the best way to replace you#youremail.dev with the receivers E-Mail-Address? (from the User-Model)
Thanks in advance!
You can modify the new_message to accept the user (or list of users) to whom you want to send the email. Or an array of email addresses if you want to. Then pass the receiver's email address to the mail method as the :to option.
def new_message(receiver, message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}",
:to => receiver.email_address) # or something similar
end
Then you can invoke your mailer like this
NotificationEmail.new_message(a_user, a_message).deliver
To read the API see here or here (I prefer APIdock).
Also a more comprehensive guide on ActionMailer is available here. If you are new to Rails, you can find more guides here.

How to send 'Mark as Important' mail using rails

I need your views as i don't know is it possible or not.
I want some emails send by my application should 'Mark as Important' so that when end user receive this mail in there Evolution/Outlook they should know the importance of email.
Currently when i marked any email using evolution as 'Mark as Important' it changes the colour of mail subject and other fields to red.
Both other answers are correct, but the thing is, Outlook uses a non-standard header for signaling importance. It's called X-Priority, so you have to include it in your outgoing mail. You can also include "X-MSMail-Priority: High" for older Outlooks.
def notification
mail({
:to => 'email#example.com',
:subject => 'My subject',
:from => 'me#somewhere.com',
'Importance' => 'high',
'X-Priority' => '1'}) do |format|
format.text
format.html
end
end
class Notifier < ActionMailer::Base
default :from => 'no-reply#example.com',
:return_path => 'system#example.com'
def welcome(recipient)
#account = recipient
mail(:to => recipient.email_address_with_name,
:bcc => ["bcc#example.com", "Order Watcher <watcher#example.com>"],
:subject => "No way!",
:importance => "High") # <======
end
end
The MIME RFC lists importance as a header that can be sent with MIME email. The values that can be used are high, normal or low. To send an email with an adjusted importance, use an API that allows you to either set the importance via an API method, or one that allows you to set individual headers (such as TMail).
I don't know Ruby, so I can't give you an example, but hopefully this points you in the right direction.

Resources