Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send mail via any SMTP or sendmail server #379

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/deploy_targets/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.include_common_deploy_options
def self.ensure_all_options_are_present
# These usually come from the $BARKEEP_CREDENTIALS file (see "load_barkeep_credentials_file()" for more
# info) and so are often forgotten/missing.
required_options = [:gmail_address, :gmail_password, :cookie_session_secret]
required_options = [:pony_options, :requests_outgoing_address, :comments_outgoing_address, :commits_outgoing_address, :cookie_session_secret]
required_options.each do |option|
next if Fezzik.environments[hostname][option]
puts "You haven't defined the Fezzik env variable #{option}, which is needed for this deploy. " +
Expand Down
19 changes: 16 additions & 3 deletions config/deploy_targets/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
set :domain, hostname
BarkeepDeploy.include_common_deploy_options
Fezzik.env :unicorn_workers, 2
# Replace these with your own Gmail account credentials if you want to test Barkeep's email features.
Fezzik.env :gmail_address, "[email protected]"
Fezzik.env :gmail_password, "password!"
# Replace these with your own email account credentials if you want to test Barkeep's email features.
mail_user = ""
mail_domain = ""
Fezzik.env :pony_options, {
:address => "smtp.gmail.com",
:port => "587",
:enable_starttls_auto => true,
:user_name => "#{mail_user}@#{mail_domain}",
:password => "",
:authentication => :plain,
# the HELO domain provided by the client to the server
:domain => "localhost.localdomain"
}
Fezzik.env :requests_outgoing_address, "#{mail_user}+requests@#{mail_domain}"
Fezzik.env :commits_outgoing_address, "#{mail_user}+commits@#{mail_domain}"
Fezzik.env :comments_outgoing_address, "#{mail_user}+comments@#{mail_domain}"
# This secret is used to encrypt session information into cookies.
Fezzik.env :cookie_session_secret, "This should be a long, random, secret string."
# This "deploy_user" is the user that code will be deployed and run as.
Expand Down
2 changes: 1 addition & 1 deletion config/tasks/environment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace :fezzik do
FileUtils.mkdir_p root_config_dir
File.open(File.join(root_config_dir, "environment.rb"), "w") do |file|
environment.each do |key, value|
quote = value.is_a?(Numeric) ? '' : '"'
quote = value.is_a?(String) ? '"' : ''
file.puts "#{key.to_s.upcase} = #{quote}#{value}#{quote}"
end
end
Expand Down
23 changes: 19 additions & 4 deletions environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
DB_USER = "root"
DB_PASSWORD = ""

# These are the credentials of the Gmail account that you want to send mail as.
# NOTE(philc): We may want to make configuration variables which generically support SMTP.
GMAIL_ADDRESS = ""
GMAIL_PASSWORD = ""
# These are the credentials of the email account that you want to send mail as.
MAIL_USER = ""
MAIL_DOMAIN = ""

# These settings are from the Pony documentation and work with Gmail's SMTP TLS server.
PONY_OPTIONS = {
:address => "smtp.gmail.com",
:port => "587",
:enable_starttls_auto => true,
:user_name => "#{MAIL_USER}@#{MAIL_DOMAIN}",
:password => "",
:authentication => :plain,
# the HELO domain provided by the client to the server
:domain => "localhost.localdomain"
}

REQUESTS_OUTGOING_ADDRESS = "#{MAIL_USER}+requests@#{MAIL_DOMAIN}"
COMMENTS_OUTGOING_ADDRESS = "#{MAIL_USER}+comments@#{MAIL_DOMAIN}"
COMMITS_OUTGOING_ADDRESS = "#{MAIL_USER}+commits@#{MAIL_DOMAIN}"

# This a list of paths to git repos we should watch.
REPOS_ROOT = "#{ENV["HOME"]}/barkeep_repos"
Expand Down
28 changes: 9 additions & 19 deletions lib/emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ def self.send_review_request_email(requester, commit, emails)
completed_email = CompletedEmail.new(:to => emails.join(","), :subject => subject,
:result => "success")

user, domain = GMAIL_ADDRESS.split("@")
pony_options = pony_options_for_commit(commit).merge({
# Make the From: address e.g. "[email protected]" so it's easily filterable.
:from => "#{user}+requests@#{domain}"
:from => REQUESTS_OUTGOING_ADDRESS
})

begin
Expand Down Expand Up @@ -61,11 +60,9 @@ def self.send_comment_email(commit, comments)
completed_email = CompletedEmail.new(:to => ([to] + cc).join(","), :subject => subject,
:result => "success", :comment_ids => comments.map(&:id).join(","))

user, domain = GMAIL_ADDRESS.split("@")
pony_options = pony_options_for_commit(commit).merge({
:cc => cc.join(","),
# Make the From: address e.g. "[email protected]" so it's easily filterable.
:from => "#{user}+comments@#{domain}"
:from => COMMENTS_OUTGOING_ADDRESS
})

begin
Expand All @@ -91,10 +88,9 @@ def self.send_commit_email(commit)

return if to.empty? # Sometimes... there's just nobody listening.

user, domain = GMAIL_ADDRESS.split("@")
pony_options = pony_options_for_commit(commit).merge({
# Make the From: address e.g. "[email protected]" so it's easily filterable.
:from => "#{user}+commits@#{domain}"
:from => COMMITS_OUTGOING_ADDRESS
})

deliver_mail(to.join(","), subject, html_body, pony_options)
Expand Down Expand Up @@ -141,18 +137,12 @@ def self.pony_options_for_commit(commit)
# "message-ID" to enable threading.
# Subject forced to utf-8 to avoid issues with mail encodings (ooyala/barkeep#285)
def self.deliver_mail(to, subject, html_body, pony_options = {})
options = { :to => to, :via => :smtp, :subject => subject.force_encoding("utf-8"), :html_body => html_body,
# These settings are from the Pony documentation and work with Gmail's SMTP TLS server.
:via_options => {
:address => "smtp.gmail.com",
:port => "587",
:enable_starttls_auto => true,
:user_name => GMAIL_ADDRESS,
:password => GMAIL_PASSWORD,
:authentication => :plain,
# the HELO domain provided by the client to the server
:domain => "localhost.localdomain"
}
options = {
:to => to,
:via => :smtp,
:subject => subject.force_encoding("utf-8"),
:html_body => html_body,
:via_options => PONY_OPTIONS
}
begin
Pony.mail(options.merge(pony_options))
Expand Down