Skip to content

Instantly share code, notes, and snippets.

@matherton
Created April 25, 2012 13:35
Show Gist options
  • Save matherton/2489742 to your computer and use it in GitHub Desktop.
Save matherton/2489742 to your computer and use it in GitHub Desktop.
rails controller using pony gem to send internal email
class UsersController < ApplicationController
before_filter RubyCAS::Filter, :except => :cas_update_user
before_filter :check_user, :except => :cas_update_user
def promote
error = "Please enter your name" if params[:name].blank?
error = "Please enter your telephone number" if params[:telephone].blank?
error = "Please enter a valid email address" unless params[:email].match(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/)
if error.blank?
Pony.mail(:to => Setting[:promote_advert_email_addresses], :from => '[email protected]', :subject => 'The Stage Directory - Ad Promotion request', :body => "
The Stage Directory - Ad promotion request
The following user has requested information about promoting their ad:
Name: #{params[:name]}
Telephone: #{params[:telephone]}
Email: #{params[:email]}
To view their account details, visit this page: #{admin_user_url(@user)}
", :html_body => "
<table width='100%' border='0' cellpadding='4' cellspacing='0'>
<tr>
<td height='50' colspan='4' bgcolor='#000000'><a href='http://www.thestage.co.uk'><img border='0' src='http://directory.thestage.co.uk/assets/stage_co_uk_black_logo.gif' alt='the stage ' width='176' height='26' longdesc='http://directory.thestage.co.uk' /></a></td>
</tr>
<tr>
<td style='border-left: 1px #ccc solid;'>&nbsp;</td>
<td colspan='2'><img src='http://directory.thestage.co.uk/assets/email_gifs/spacer_white15px.gif' alt=''/></td>
<td style='border-right: 1px #ccc solid;'>&nbsp;</td>
</tr>
<tr>
<td style='border-left: 1px #ccc solid;'>&nbsp;</td>
<td colspan='2'><img src='http://directory.thestage.co.uk/assets/email_gifs/the-stage-directory.gif' alt='THE STAGE DIRECTORY' width='444' height='40' longdesc='http://directory.thestage.co.uk'></td>
<td style='border-right: 1px #ccc solid;'>&nbsp;</td>
</tr>
<tr>
<td style='border-left: 1px #ccc solid;'>&nbsp;</td>
<td colspan='2' style='border-top: 1px #43b649 solid;'>
<font face='Verdana, Arial, Helvetica, sans-serif' color='#43B649' size='3'>
<a style='color:#43b649; text-decoration:none;' class='nav' href='http://directory.thestage.co.uk'>
<strong>BROWSE SUPPLIERS &amp; SERVICES</strong> </a> </font>
<font face='Verdana, Arial, Helvetica, sans-serif' color='#43B649' size='3'>
<a style='color:#43b649; text-decoration:none;' class='nav' href='http://directory.thestage.co.uk/adverts/select_type'>
<strong>&nbsp;&nbsp;CREATE AN AD</strong> </a> </font> </td>
<td style='border-right: 1px #ccc solid;'>&nbsp;</td>
</tr>
<tr>
<td width='1%' style='border-left: 1px #ccc solid;'>&nbsp;</td>
<td colspan='2' valign='top'>
<!-- header ends -->
<h1>The Stage Directory - Ad promotion request</h1>
<p style='font-family:Helvetica Neue, Helvetica, Arial, sans-serif'><font size='3'>The following user has requested information about promoting their ad:</font></p>
<ul>
<li style='color:#43b649'><font color='#000000' size='3' style='font-family:Helvetica Neue, Helvetica, Arial, sans-serif'>Name: #{params[:name]}</font></li>
<li style='color:#43b649'><font color='#000000' size='3' style='font-family:Helvetica Neue, Helvetica, Arial, sans-serif'>Telephone: #{params[:telephone]}</font></li>
<li style='color:#43b649'><font color='#000000' size='3' style='font-family:Helvetica Neue, Helvetica, Arial, sans-serif'>Email:
<font color='#43b649'>#{params[:email]}</font></font></li>
</ul>
<p style='font-family:Helvetica Neue, Helvetica, Arial, sans-serif'><a style='color:#43b649;' href='#{admin_user_url(@user)}'><font size='3'>Click here to view their account details</font></a></p>
<!-- footer.html.erb starts -->
</td>
<td width='1%' style='border-right: 1px #ccc solid;'>&nbsp;</td>
</tr>
<tr>
<td align='right' bgcolor='#FFFFFF' style='border-left: 1px solid #ccc;'>&nbsp;</td>
<td bgcolor='#FFFFFF'><img src='http://directory.thestage.co.uk/assets/email_gifs/spacer_white15px.gif' alt=''/></td>
<td align='right' bgcolor='#FFFFFF'>&nbsp;</td>
<td bgcolor='#FFFFFF' align='right' style='border-right: 1px #ccc solid;'>&nbsp;</td>
</tr>
<tr>
<td height='30' align='right' bgcolor='#F6F3EC' style='border-left: 1px solid #ccc; border-bottom: 1px solid #ccc'>&nbsp;</td>
<td width='49%' height='30' align='left' bgcolor='#F6F3EC' style='border-bottom: 1px solid #ccc'>
<p>
<font face='Verdana, Arial, Helvetica, sans-serif' size='1'>
Footer text and unsubscribe link goes here when we have it. </font> </p> </td>
<td width='49%' align='right' bgcolor='#F6F3EC' style='border-bottom: 1px solid #ccc'><a href='http://www.facebook.com/TheStageEvents'><img border='0' src='http://directory.thestage.co.uk/assets/email_gifs/facebook-logo.gif' alt='facebook'/></a>&nbsp;&nbsp;<a href='https://twitter.com/#!/TheStage'><img border='0' src='http://directory.thestage.co.uk/assets/email_gifs/twitter-logo.gif' alt='twitter'/></a></td>
<td height='30' bgcolor='#F6F3EC' align='right' style='border-right: 1px #ccc solid; border-bottom: 1px solid #ccc'>&nbsp;</td>
</tr>
</table>
")
flash[:success] = "Thank you, an account manager will contact you within 2 working days."
else
flash[:error] = error
end
redirect_to dashboard_path
end
def check_user # make sure user is logged in
logged_in_user = User.find_by_username( session[:cas_user])
@user = User.find(params[:id])
redirect_to '/' if logged_in_user != @user
end
def cas_update_user
user_details = HashWithIndifferentAccess.new(ActiveSupport::JSON.decode(params[:user]))[:user]
api_key =
unless params['api_key'] == "pqXgEVGTnr1c5VxKgXhxJ0RkU7qZcdwV1SceuNSTXJY6OfilDthf9DlT05nPHfe"
raise ActionController::RoutingError.new('Not Found')
end
user = User.find_by_username(user_details[:username])
unless user.blank?
unless user.update_attributes(:title => user_details[:title], :firstname => user_details[:firstname], :surname => user_details[:surname], :email => user_details[:email],
:company_name => user_details[:company_name], :phone => user_details[:phone])
throw "could not update user #{user_details[:username]}"
end
end
render :nothing => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment