Skip to content

Instantly share code, notes, and snippets.

@cyrildiagne
Last active October 7, 2015 17:18
Show Gist options
  • Save cyrildiagne/3199629 to your computer and use it in GitHub Desktop.
Save cyrildiagne/3199629 to your computer and use it in GitHub Desktop.
Super simple html photo emailer using Sinatra, Amazon S3 and Gmail
public_folder : /path/to/your/photos
# Amazon s3
s3_bucket : yourS3bucket
s3_key : yourS3key
s3_secret : yourS3secret
# Mail
mail_options :
:user_name : [email protected]
:password : yourGmailPass
:address : smtp.gmail.com
:port : 587
:enable_starttls_auto : true
:authentication : :plain
:domain : localhost.localdomain
require 'rubygems'
require 'sinatra'
require "sinatra/config_file"
require 'haml'
require 'aws/s3'
require 'pony'
config_file 'config.yml'
$photos = Dir[ File.join(settings.public_folder, '**', '*.{jpg,JPG}') ].last(20).reverse
get '/' do
haml :index, :locals => {:notice=> ''}
end
post '/' do
filename = params[:filename]
# Connect to Amazon S3
AWS::S3::Base.establish_connection!(
:access_key_id => settings.s3_key,
:secret_access_key => settings.s3_secret
)
# Upload the file only if it doesn't exist yet
unless AWS::S3::S3Object.exists? filename, settings.s3_bucket
AWS::S3::S3Object.store(
filename,
open( settings.public_folder + filename ),
settings.s3_bucket,
:access => :public_read
)
end
# Send the link via email
Pony.mail :to => params[:email],
:from => "[email protected]",
:subject => "Votre photo Playtime",
:html_body => haml(:email, :locals => { :url => "http://" + settings.s3_bucket + ".s3.amazonaws.com/" + filename }),
:via => :smtp,
:via_options => settings.mail_options
# Redirect to home page with a success notice
haml :index, :locals => {:notice=> 'photo successfully uploaded & sent by mail!'}
end
__END__
@@index
%p=notice
%table
- $photos.each do |item|
- item = item.split('/').last
%tr
%td
%img{:src=>item, :width=>"300"}
%br
%td
%form{:action=>"/",:method=>"post",:enctype=>"multipart/form-data"}
%input{:type=>"hidden", :name=>"filename", :value=> item }
%input{:type=>"text", name:"email"}
%input{:type=>"submit",:value=>"Envoyer"}
@@email
%a{:href=>url}
%img{:src=>url}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment