I'm trying to use ActionMailer without Rails in a project, and I want to use Haml for the HTML email templates. Anyone have any luck getting this configured and initialized so that the templates will be found and rendered? I'm currently getting errors like:
ActionView::MissingTemplate: Missing template new_reg/daily_stats/full with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en]} in view paths "/home/petersen/new_reg/lib/new_reg/mailers/views"
To clarify, this is ActionMailer 3.0.4
Looks like the issue is that without the full Rails stack, Haml doesn't completely load, specifically the Haml::Plugin class. Adding require 'haml/template/plugin' after the normal require 'haml' line seems to solve the problems.
require 'haml/template/plugin' in the "configure do" block together with ActionMailer::Base.view_paths = "./views/" did it for me (Sinatra)
Not necessary in Rails -- but since you're using ActionMailer without Rails -- did you specify ActionMailer::Base.register_template_extension('haml')?
I'm seeing a similar issue and am using ActionMailer 3.0.3. register_template_extension does not exist in ActionMailer 3.
I'm using Sinatra. I've got mailer.rb (below) in APP_ROOT/lib and the views are located in APP_ROOT/views/mailer. This sends an email with a subject, the body is blank though.
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.view_paths = File.dirname(__FILE__)+"/../views/"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'exmaple.com',
:user_name => 'user#exmaple.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true }
class Mailer < ActionMailer::Base
def new_comment_notifier(post,comment)
#post = post
#comment = comment
mail(:to => "user#example.com",
:subject => "new comment on: #{post.title}")
end
end
Related
I'm getting an error when attempting to change the :filename of my paperclip attachment to equal an attribute on the class I'm attaching the paperclip file to.
When I use "#{self.company_name}" it errors out. Apparently in this scope, "self" is not Company. When I wrote this line I assumed that self is the instance of Company that I'm uploading this attachment to. Any idea how I can fix this? The Paperclip docs say to use ":filename" but I'd like to use the value of Company.company_name instead.
class Company < ActiveRecord::Base
include AliasAttrs
has_attached_file :company_logo, {
:storage => :ftp,
:path => "/logos/#{self.company_name}",
:url => FTP_CONFIG[:access_host]+"logos/:filename",
:ftp_servers => [
{
:host => FTP_CONFIG[:host],
:user => FTP_CONFIG[:user],
:password => FTP_CONFIG[:pass],
:port => 21 # optional, 21 by default
}
]
}
end
Update
I tried using the advice found in this post: https://robots.thoughtbot.com/paperclip-tips-and-updates
But now I am getting the following error when starting my server:
undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)
It looks like the syntax for interpolations has changed. Updated it and it worked. Add the following to your model or create a paperclip.rb file in config/initializers
Paperclip.interpolates :company_name do |attachment, style|
attachment.instance.company_name
end
I am building my first portfolio page with Sinatra.
I have a 'textbook' contact page with a straight-forward form containing 'name', 'email' and 'content' fields. When someone submits the form, I want to recieve an email notification.
Pony claims that it can send email via simple 'one-line' of code. I have read the Pony documentation but it is not very detailed in how to set it up.
I don't know if I am not setting it up properly, the code is not right, Pony is not the best tool, or if my development environment is not allowing the mail to be sent.
The code below is supposed to be sending an email from the post method, it is then saving the data to a PostgreSQL database via the save_message method. The data is being persisted correctly.
#server.rb
require 'sinatra'
require 'pony'
require_relative 'model/methods'
get '/contact' do
erb :contact
end
post '/thankyou' do
unless params[:name] == '' || params[:email] == '' || params[:content] == ''
Pony.options = {
:subject => "Portfolio page: Message delivery from #{params[:name]}",
:body => "#{params[:content]}",
:via => :smtp,
:via_options => {
:address => 'smtp.1and1.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => ENV["USER_EMAIL_ADDRESS"],
:password => ENV["SMTP_PASSWORD"],
:authentication => :login,
:domain => 'nterrafranca.com'
}
}
Pony.mail(:to => ENV["DESTINATION_EMAIL_ADDRESS"])
save_message(params[:name], params[:email], params[:content])
end
redirect '/'
end
Pony needs to know how to send the email, not just who it's to, from, what the subject and body are, etc.
From the pony documentation, it will default to use sendmail, otherwise configures SMTP to use localhost. Depending on where this application is running, it's highly likely that sendmail is not available, and that there is no SMTP configured on localhost.
I've used Pony for several applications. Each one, I configure a "noreply#" email address for Pony to use to authenticate for SMTP, therefore using my own domain email (usually Google Apps, or even Gmail) for my SMTP connection. For example:
Pony.options = {
:subject => "Some Subject",
:body => "This is the body.",
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'noreply#cdubs-awesome-domain.com',
:password => ENV["SMTP_PASSWORD"],
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain"
}
}
In the case of a Sinatra app, I perform the exact above code (with the obvious substitutions) right before I call:
Pony.mail(:to => <some_email>)
I've configured Pony multiple times - comment if you still have issues and I'll be glad to help.
If you are using a gmail account with 2-step verification, you must generate an application specific password for the Pony mailer, and NOT use your usual SMTP password.
See https://support.google.com/accounts/answer/185833?hl=en
Insert the application specific password in the place of your usual password.
This is from the Pony project page on Github.
Hi guys I remove /page from high_voltage gem with this answer.
Remove page/ of High Voltage for statics page rails
I have in my routes for high_voltage this:
match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
For maintenance page 404 in rails 3.1 I follow this fix http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution with errors_controller.rb with the next code:
def routing
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Then I add to routes.rb the next code for maintenance page 404 in rails
match '*a', :to => 'errors#routing'
The problem is that if I put in browser www.mydomain.com/sdfs dont working 404 system error and show No such page: sdfs
but however if I put www.mydomain.com/a_controller/action/sdfs yes working fine the fix for error 404 page.
I think that problem is my routes.rb
I solved this problem by extending the HighVoltage::PagesController and modifying the error catching:
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
In my case though, my 404 function resides in my application controller so that it can easily be called from any location. If you make the same change, you will also need to update your route:
match '/:id' => 'pages#show', :as => :static, :via => :get
Thank you kevinthopson for mi this dont working fine :(.
I have in my routes.rb:
match '/:id' => 'pages#show', :as => :static, :via => :get
I have added this in pages_controller.rb
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
I have added this code to aplication_controller.rb:
def render_not_found
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Working fine if you put now:
localhost:3000/dfadsfadsf
The problem now is that if you put for example that routes in navigation bar:
localhost:3000/users_or_static_page/asdfadfadfa
Dont working for me :(.
I am using rails 3.0.3, ruby 1.9.2-p180, mail (2.2.13). I m trying to setup a mail interceptor but I am getting the following error
/home/abhimanyu/Aptana_Studio_3_Workspace/delivery_health_dashboard_03/config/initializers/mailer_config.rb:16:in `<top (required)>': uninitialized constant DevelopmentMailInterceptor (NameError)
How do i fix it?
The code I am using is shown below:
config/initializer/mailer_config.rb
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "secure.emailsrvr.com",
:port => '25',
:domain => "domain",
:user_name => "user_name",
:password => "password",
:authentication => :plain
}
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
lib/development_mail_interceptor.rb
class DevelopmentMailInterceptor
def self.delivering_email(message)
message.to = "email"
end
end
Thanks in advance.
require 'development_mail_interceptor' #add this line
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
I found it easier to install the mailcatcher gem. Then in development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "`localhost`",
:port => 1025
}
Then just run "mailcatcher" and hit http://localhost:1080/ in a browser. It runs in the background, but can be quit directly from the browser. Gives you text+html views, source, and analysis with fractal, if you swing that way. Super-clean.
I'm writing a small Ruby program that will pull records from a database and send an HTML email daily. I'm attempting to use ActionMailer 3.0.3 for this, but I'm running in to issues. All the searching I've done so far on using ActionMailer outside of Rails applies to versions prior to version 3. Could someone point me in the right direction of where to find resources on how to do this? Here's where I am so far on my mailer file:
# lib/bug_mailer.rb
require 'action_mailer'
ActionMailer::Base.delivery_method = :file
class BugMailer < ActionMailer::Base
def daily_email
mail(
:to => "example#mail.com",
:from => "example#mail.com",
:subject => "testing mail"
)
end
end
BugMailer.daily_email.deliver
I'm definitely stuck on where to put my views. Every attempt I've made to tell ActionMailer where my templates are has failed.
I guess I should also ask if there's a different way to go about accomplishing this program. Basically, I'm doing everything from scratch at this point. Obviously what makes Rails awesome is it's convention, so is trying to use parts of Rails on their own a waste of time? Is there a way to get the Rails-like environment without creating a full-blown Rails app?
After some serious debugging, I found how to configure it.
file mailer.rb
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.com.ar",
:authentication => :plain,
:user_name => "test#domain.com.ar",
:password => "passw0rd",
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths= File.dirname(__FILE__)
class Mailer < ActionMailer::Base
def daily_email
#var = "var"
mail( :to => "myemail#gmail.com",
:from => "test#domain.com.ar",
:subject => "testing mail") do |format|
format.text
format.html
end
end
end
email = Mailer.daily_email
puts email
email.deliver
file mailer/daily_email.html.erb
<p>this is an html email</p>
<p> and this is a variable <%= #var %> </p>
file mailer/daily_email.text.erb
this is a text email
and this is a variable <%= #var %>
Nice question! It helped me to understand a bit more how Rails 3 works :)
It took me a while to get this to work in (non-)Rails 4. I suspect it's just because I have ':require => false' all over my Gemfile, but I needed to add the following to make it work:
require 'action_view/record_identifier'
require 'action_view/helpers'
require 'action_mailer'
Without the above code, I kept getting a NoMethodError with undefined method 'assign_controller'.
After that, I configured ActionMailer as follows:
ActionMailer::Base.smtp_settings = {
address: 'localhost', port: '25', authentication: :plain
}
ActionMailer::Base.default from: 'noreply#example.com'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.logger = Logger.new(STDOUT)
ActionMailer::Base.logger.level = Logger::DEBUG
ActionMailer::Base.view_paths = [
File.join(File.expand_path("../../", __FILE__), 'views', 'mailers')
# Note that this is an Array
]
The templates go in lib/<GEM_NAME>/views/mailers/<MAILER_CLASS_NAME>/<MAILER_ACTION_NAME>.erb (MAILER_ACTION_NAME is the public instance method of your mailer class that you call to send the email).
Lastly, don't forget to put this in your spec_helper:
ActionMailer::Base.delivery_method = :test