Skip to content

Instantly share code, notes, and snippets.

@huemorgan
Created January 21, 2014 09:01
Show Gist options
  • Save huemorgan/8536656 to your computer and use it in GitHub Desktop.
Save huemorgan/8536656 to your computer and use it in GitHub Desktop.
namespace :email do
namespace :al do
desc "send emails to leads"
task :send => :environment do |t, args|
require 'mandrill'
leads = AlLead.where("email1_try_ok is null and
email2_try_ok is null and
email3_try_ok is null and
email4_try_ok is null
")
puts("sending to #{leads.count} Leads")
leads.each_with_index do |lead,index|
res = contact_lead_sus(lead) if index < 200
break if res == "queued"
end
# lead = AlLead.new()
# lead.name = "roy man"
# lead.user_id = 123456
# lead.id = 666
# lead.email1_try = "roy@dapulse.com"
# contact_lead(lead)
end
end
end
def contact_lead_sus(lead)
fields = ["email1","email3","email4"]
fields.each do |field|
if lead["#{field}_try"] != "" && lead["#{field}_try"] != nil && bump_email(lead["#{field}_try"]) == false
ret = send_email_sus(lead,lead["#{field}_try"])
puts(ret.inspect)
return "queued" if ret[0]["status"] == "queued"
if (ret[0]["status"] == "sent")
lead["#{field}_sent"] = 1
lead["#{field}_try_ok"] = 1
puts("YEY")
else
lead["#{field}_try_ok"] = 0
puts("NAY")
end
lead.save
send_email_sus(lead,"#{lead.first_name}@dapulse.com")
send_email_sus(lead,"#{lead.last_name}@dapulse.com")
send_email_sus(lead,"#{lead.first_name}.#{lead.last_name}@dapulse.com")
send_email_sus(lead,"#{lead.last_name}.#{lead.first_name}@dapulse.com")
send_email_sus(lead,"#{lead.first_name}.#{lead.first_name}@dapulse.com")
send_email_sus(lead,"#{lead.last_name}.#{lead.last_name}@dapulse.com")
send_email_sus(lead,"#{lead.first_name}2@dapulse.com")
send_email_sus(lead,"#{lead.last_name}3@dapulse.com")
send_email_sus(lead,"#{lead.first_name}4.#{lead.last_name}@dapulse.com")
send_email_sus(lead,"#{lead.last_name}5.#{lead.first_name}@dapulse.com")
send_email_sus(lead,"#{lead.first_name}6.#{lead.first_name}@dapulse.com")
send_email_sus(lead,"#{lead.last_name}7.#{lead.last_name}@dapulse.com")
end
end
return ""
end
def bump_email(email)
return true if email.nil? || !email.index("angel").nil?
return false
end
def send_email_sus(lead,email)
begin
puts("EMAILING SUS")
# mandrill = Mandrill::API.new '5nj2fNgoRB0bXuFp4qRUgw' #old reduced credibility
mandrill = Mandrill::API.new 'Jgl0TgFU5ProQxbkgtL_1Q' #roy@snatus.com
message = {"metadata"=>{"website"=>"www.sus.io"},
"global_merge_vars"=>[{"name"=>"merge1", "content"=>"merge1 content"}],
"merge"=>true,
"signing_domain"=>nil,
"inline_css"=>nil,
"from_name"=>"rozy@sus.io",
"url_strip_qs"=>nil,
"html"=>"
Hi.<br/>
<br/>
I created a project called Startups-Using-Startups, <a href=\"http://sus.io\">http://sus.io</a><br/>
Our mission is to list all the cool WEB-APPS used in startups so new startups will know what to use. <br/>
<br/>
As a cool startup founder,<br/>
I'd appriciate it if you could add the list of webapps you use (takes 2 min for the whole stack).<br/>
You'll get a page for your company's list shown to other startups + SEO & good karma :) <br/>
<br/>
Would love to get your feedback<br/>
<br/>
Rozy<br/>
<a href=\"http://sus.io\">http://sus.io</a><br/>
",
"merge_vars"=>
[{"vars"=>[{"name"=>"merge2", "content"=>"merge2 content"}],
"rcpt"=>"#{email}"}],
"return_path_domain"=>nil,
"tags"=>["password-resets"],
"tracking_domain"=>nil,
"track_opens"=>nil,
"to"=>
[{"type"=>"to",
"name"=>"#{lead.name}",
"email"=>"#{email}"}],
"recipient_metadata"=>
[{"values"=>{"user_id"=>lead.id}, "rcpt"=>"#{email}"}],
"google_analytics_campaign"=>"message.rozy@sus.io",
"google_analytics_domains"=>["sus.io"],
"preserve_recipients"=>nil,
"from_email"=>"rozy@sus.io",
"view_content_link"=>nil,
"auto_text"=>nil,
"subject"=>"Hi #{lead.first_name},I just saw your impressive Angle List profile.",
"auto_html"=>nil,
"headers"=>{"Reply-To"=>"rozy@sus.io"},
"important"=>false,
"text"=>"
Hi.
I created a project called Startups-Using-Startups, http://sus.io
Our mission is to list all the cool WEB-APPS used in startups so new startups will know what to use.
As a cool startup founder,
I'd appriciate it if you could add the list of webapps you use (takes 2 min for the whole stack).
You'll get a page for your company's list shown to other startups + SEO & good karma :)
Would love to get your feedback
Rozy
http://sus.io
",
"track_clicks"=>true}
async = false
ip_pool = "Main Pool"
result = mandrill.messages.send message, async, ip_pool
# [{"status"=>"sent",
# "reject_reason"=>"hard-bounce",
# "_id"=>"abc123abc123abc123abc123abc123",
# "email"=>"recipient.email@example.com"}]
return result
rescue Mandrill::Error => e
# Mandrill errors are thrown as exceptions
puts "A mandrill error occurred: #{e.class} - #{e.message}"
return false
# A mandrill error occurred: Mandrill::UnknownSubaccountError - No subaccount exists with the id 'customer-123'
raise
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment