I'm using actionmailer to send mails in rails. I want to attach multiple attachments:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
attachments = self.prepare_attachments(languages)
mail(
:to => recipient,
:subject => 'Test'
)
end
The delivered mail doesn't contain any attachment.
This is working:
def distribution_email(recipient, languages)
attachments['test.pdf'] = File.read("/tmp/test.pdf")
mail(
:to => recipient,
:subject => 'Welcome to My Awesome Site'
)
end
What am i doing wrong?
I fount the solution, one must not override attachment:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
self.prepare_attachments(languages).each do |filename, content|
attachments[filename] = content
end
mail(
:to => recipient,
:subject => 'Test'
)
end
Related
I am using Rails with Devise and I would like to use Mandrill to send out the different email messages.
I have found examples on subsclassing the Devise Mailer and using Mandrill, but the example uses Mandrills templates, which i don't want to use.
I would rather use Devise's email templates for this. My question is when I subclass the devise mailer, like this:
class MyMailer< Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
def confirmation_instructions(record, token, opts={})
options = {
:subject => "Password Reset",
:email => record.email,
:name => record.name,
:global_merge_vars => [
{
name: "password_reset_link",
content: new_conformation_path(record)
}
],
}
mandrill_send options
end
def reset_password_instructions(record, token, opts={})
options = {
:subject => "Password Reset",
:email => record.email,
:name => record.name,
:global_merge_vars => [
{
name: "password_reset_link",
content: new_password_path(record)
}
],
}
mandrill_send options
end
def mandrill_send(opts={})
message = {
:subject => "#{opts[:subject]}",
:from_name => "Accounts",
:from_email => "accounts#example.com",
:to =>
[{"name" => "#{opts[:name]}",
"email" => "#{opts[:email]}",
"type" => "to"}],
:global_merge_vars => opts[:global_merge_vars]
}
begin
sending = MANDRILL.messages.send_template opts[:template], [], message
rescue Mandrill::Error => e
Rails.logger.debug("#{e.class}: #{e.message}")
raise
end
end
end
How do i generate the email from the devise templates to send to mandrill?
I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:
class UserMailer < ActionMailer::Base
default :from => "example#example.com"
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello")
end
end
I tried this:
class UserMailer < ActionMailer::Base
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order", :from => "abc#xyz.com")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.", :from => "qwe#asd.com")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello", :from => "zyx#asd.com")
end
end
But still it is sending email from "example#example.com"
Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?
In config/environments/development.rb and config/environments/production.rb i have this:
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "example#example.com",
:password => "example",
:enable_starttls_auto => true
}
I guess, i should not change anything here.
You can pass it as a parameter to the mail method:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end
I think you want to send mail with three different emails of the for-each action. Because you use gmail, you need Sending mail from a different address.
No single vendor is optimal for all three types of email; you likely
will use several vendors.
For “company email,” that is, sending individual email to customers or
business associates, you’ll probably use Gmail or Google Apps for
Business. For a single address, you can set up a single Gmail account
to receive and send email from a different address. More likely,
you’ll want several email addresses for your company mail. For that,
use Google Apps for Business.
Send Email with Rails
I found that, this can't be done using smtp. Need to use amazon SES which allows multi sender support.
Here's what i use, it allows to make a "title" different.
class UserMailer < ActionMailer::Base
default :from => '"example" <example#domain.com>'
def send_signup_email(user)
#user = user
mail(to: #user.email, subject: 'example')
end
end
I have a simple script for my contact_mailer.rb which has a simple form as the front end with one dropdown to select purpose and has a switch-case block to choose the email ID accordingly. Now, somehow mail is always being sent to the first one i.e. when 'localization'.
Only this gets fired whatever be selected. Please explain why this may be happening. Am pasting code snippet below for reference:
class ContactUsMailer < ActionMailer::Base
default :from => "bot#mydomain.com"
def contact_us_email(name, message, purpose, email)
#name = name
#message = message
#purpose = purpose
#email = email
content_type "text/html"
case #purpose
when 'localization'
mail(:to => 'me#mydomain.com', :subject => purpose)
when 'marketing'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com, me#mydomain.com', :subject => purpose)
when 'network'
mail(:to => 'me.agm#gmail.com', :subject => purpose)
when 'recruitment'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com', :subject => purpose)
when 'general'
mail(:to => 'me#mydomain.com', :subject => purpose)
else
mail(:to => 'me#mydomain.com', :subject => purpose)
end
end
end
Thanks and Regards
Is there a exception_notification-like gem for delayed_job?
Preferably that works with REE-1.8.7 and Rails 2.3.10.
I've done something like this in the past for delayed job rake tasks:
require 'action_mailer'
class ExceptionMailer < ActionMailer::Base
def setup_mail
#from = ExceptionNotifier.sender_address
#sent_on = Time.now
#content_type = "text/plain"
end
def exception_message(subject, message)
setup_mail
#subject = subject
#recipients = ExceptionNotifier.exception_recipients
#body = message
end
end
namespace :jobs do
desc "sync the local database with the remote CMS"
task(:sync_cms => :environment) do
Resort.sync_all!
result = Delayed::Job.work_off
unless result[1].zero?
ExceptionMailer.deliver_exception_message("[SYNC CMS] Error syncing CMS id: #{Delayed::Job.last.id}", Delayed::Job.last.last_error)
end
end
end
Include this module in classes which are to be delayed:
require 'exception_notifier'
module Delayed
module ExceptionNotifier
# Send error via exception notifier
def error(job, e)
env = {}
env['exception_notifier.options'] = {
:sections => %w(backtrace delayed_job),
:email_prefix => '[Delayed Job ERROR] ',
:exception_recipients => %w(some#email.com),
:sender_address => %(other#email.com)
}
env['exception_notifier.exception_data'] = {:job => job}
::ExceptionNotifier::Notifier.exception_notification(env, e).deliver
end
end
end
and create a template for the notification in app/views/exception_notifier/_delayed_job.text.erb:
Job name: <%= #job.name %>
Job: <%= raw #job.inspect %>
* Process: <%= raw $$ %>
* Server : <%= raw `hostname -s`.chomp %>
I'm trying to convert this code
def password_reset_instructions(user)
subject "Registered"
recipients user.email
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
to this code
def password_reset_instructions(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
My problem is i don't know where to put the code below.
:edit_password_reset_url => edit_password_reset_url(user.perishable_token)"
I am using authlogic on rails 3.
In Rails 3, Mailers work just like controllers. You can use the instance variable of the user in the accompanying view.
Not tested, but try this:
def password_reset_instructions(user)
#edit_password_reset_url = edit_password_reset_path(user.perishable_token)
mail(
:subject => "Password Reset Instructions",
:recipients => user.email
)
end