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.
Related
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!
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
I am sending emails with the mail gem, using the very simple code below. When I'm sending an email, the smtp_account.user_name and the smtp_account.from could be e.g. support#thebestcompany.com. This causes the recipient to see that he has received an email from support (you know, in his inbox where he can get an overview of his last x emails). How can I specify this, so the recipient gets to see something more interesting, like e.g. The Best Company?
require 'mail'
class SmtpMails
def self.send_mails(smtp_account, to, subject, text)
options = {
:address => smtp_account.address,
:port => smtp_account.port,
:domain => smtp_account.domain,
:user_name => smtp_account.user_name,
:password => smtp_account.password,
:authentication => smtp_account.authentication,
:enable_starttls_auto => smtp_account.enable_starttls_auto
}
Mail.defaults do
elivery_method :smtp, options
end
mail = Mail.deliver do
to to
from smtp_account.from
subject subject
end
end
end
You'll need to add some additional information when specifying the sender of the email. The following code snippet should do what you want:
mail = Mail.deliver do
to to
from "The Best Company <#{smtp_account.from}>"
subject subject
end
Putting the email address between the < and > brackets is the standard practice for adding a friendly name for the email being sent.
mail.from.addresses
mail.sender.address
Try this.
I am trying to send email with attachments using Ruby on Rails.
I followed the instructions from the ActionMailer site.
def welcome(recipient)
#account = recipient
attachments['file.csv'] = File.read('/path/to/users.csv')
mail(:to => recipient,
:bcc => ["email#example.com", "email2#example.com"],
:subject => "Sending attachment")
end
I am able to receive emails but without the attachment, I am trying to attach csv file but I am getting a file called "noname" as attachment
I just had this problem. I was not providing a body for the email, and was sending the attachment only. Unfortunately, it appears that SendGrid gets confused by this, sending an empty email (which is expected) with an empty attachment (which is neither expected nor desired).
Therefore, the solution: provide a body for the email. In your specific case, an /app/views/application_mailer/welcome.text.erb template with a simple text, saying "See attached" or whatever appropriate.
SendGrid is an SMTP service, and thus should function just as any other outbound SMTP service. Are you sure your syntax and filepaths are correct?
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
Verify correct syntax
Verify correct filepath
Verify permissions on file are set correctly
Check your logs
I want send newsletter to all registered users, but only the last user (in database) receive the mail.
def letter(nletter)
#nletter = nletter
#users=Newsletter.all
#users.each do |users|
mail(:to => users.email, :subject => #nletter.subject)
end
end
Whats wrong?
If the code you showed us is in your Mailer class, it's because the mail method just sets various attributes on a Mail object. When you loop through all your users and call mail for each one, you're just resetting the subject and (more importantly) the recipient on a single Mail object. So when the function finishes and the Mail is sent out, the current recipient is the last user you set it to - the last user in your database.
You can get around this in two different ways. If it's a generic e-mail (everybody gets the same exact message), you can just pass an array of e-mail addresses as :to:
Note: As Josh points out in the comments, you'll pretty much always want to use :bcc instead of :to, so you don't end up broadcasting your entire mailing list.
def letter(nletter)
#nletter = nletter
#users=Newsletter.all
mail(:to => #users.map(&:email), :subject => #nletter.subject)
end
But if each user gets a custom e-mail ("Hi, #{username}!" or somesuch), you'll have to create a new Mail object for each user. You can do this by calling Mailer.deliver_letter(nletter, user) for each user:
# Somewhere outside of your Mailer:
nletter = "...whatever..."
Newsletter.all.each do |user|
Mailer.deliver_letter(nletter, user)
end
# And your mailer function would look like:
def letter(nletter, user)
#nletter = nletter
mail(:to => user.email, :subject => #nletter.subject)
end
Hope this helps!