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')
Related
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
Here is my mailer:
class MailIt < ActionMailer::Base
def funny(sender)
#sender = sender
attachments['funny.pdf'] = File.read("#{Rails.root}/app/assets/funny.pdf")
mail(:to => "amail#example.com",
:from => "amail#example.com",
:subject => "TESST")
end
end
I get the e-mail with the attached pdf but it is damaged and the file size is only 1/4 of the original file.
if you have images in your pdf, you have to send it as binary.
attachments['funny.pdf'] = File.read("#{Rails.root}/app/assets/funny.pdf", :mode => 'rb')
I did find a solution.
It seems like there is a problem with File.read
I tried this and it worked:
attachments['funny.pdf'] = File.open("#{Rails.root}/app/assets/funny.pdf").read
Notifier.rb
class Notifier < ActionMailer::Base
def inquiry_notification(inquiry)
recipients inquiry.respondent.email
from "#{#laz.email}"
subject "Survey"
content_type "text/html"
end
end
Part of Controller.rb
...
#laz = User.find(:all)
respondents.each do |r|
inquiry = Inquiry.create(:question_id => #question.id, :respondent_id => r.id, :is_answered => 0)
Notifier.deliver_inquiry_notification(inquiry)
end
....
I need to paste into "FROM" (notifier.rb) email that user has.
For example: session[:user].email <- paste this , because i work with sessions and i have many users (admins and auditors).
Why dont you just add additional parameter to your inquiry_notification method like this:
def inquiry_notification(inquiry, from_email)
recipients inquiry.respondent.email
from from_email
subject "Survey"
content_type "text/html"
end
If you dont want to do so, you can use for example Thread.current:
in controller
Thread.current[:email] = 'test#email.com'
in Notifier
def inquiry_notification(inquiry)
recipients inquiry.respondent.email
from Thread.current[:email]
subject "Survey"
content_type "text/html"
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