Skip to content

Instantly share code, notes, and snippets.

@felipecabargas
Last active December 26, 2015 12:07
Show Gist options
  • Save felipecabargas/7e6e0fa5f634531cf636 to your computer and use it in GitHub Desktop.
Save felipecabargas/7e6e0fa5f634531cf636 to your computer and use it in GitHub Desktop.
Mailer Rails
#config/application.rb
module RailsApp
class Application < Rails::Application
config.action_mailer.smtp_settings = {
:address => "smtp.live.com",
:port => 587,
:domain => "squape.com",
:user_name => "[email protected]",
:password => "squapeco",
:authentication => :plain,
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
:host => "squape.com"
}
end
end
#app/controllers/contact_controller.rb
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
NotificationsMailer.new_message(@message).deliver
redirect_to(root_path, :notice => "El mensaje fue enviado correctamente. Te contactaremos a la brevedad (:")
else
redirect_to(contact_path, :notice => "Hay campos incompletos, intenta nuevamente.")
end
end
end
#app/views/contact/new.html.haml
=render('layouts/header')
- if flash[:notice]
.row
.alert-message.warning.large-8.columns.large-centered
%p
= flash[:notice]
.content
.row
.large-6.columns#mision
%h3 Contacto
.row
= form_for @message, :url => contact_path do |form|
%fieldset.fields
.field
= form.label :nombre
= form.text_field :name
.field
= form.label :email
= form.text_field :email
.field
= form.label :asunto
= form.text_field :subject
.field
= form.label :cuerpo
= form.text_area :body
%fieldset.actions
= form.submit "Enviar"
=render('layouts/footer')
<!-- app/views/notifications_mailer/new_message.text.erb -->
Name: <%= @message.name %>
Email: <%= @message.email %>
Subject: <%= @message.subject %>
Body: <%= @message.body %>
#app/controllers/notifications_mailer.rb
class NotificationsMailer < ActionMailer::Base
default :from => "[email protected]"
default :to => "[email protected]"
def new_message(message)
@message = message
mail(:subject => "[SQUAPE.com] #{message.subject}")
end
end
#config/routes.rb
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment