Skip to content
Browse files

Added an example of using sendgrid smtp

  • Loading branch information...
1 parent 111090d commit 50b5f13ce37677ea1ea88cdfcc6b4fdc4be9fc86 @aiwilliams committed
Showing with 72 additions and 2 deletions.
  1. +4 −2 lib/mlist/email_server/smtp.rb
  2. +68 −0 sendgrid_example.rb
View
6 lib/mlist/email_server/smtp.rb
@@ -7,8 +7,10 @@ class Smtp < Base
def deliver(tmail)
destinations = tmail.destinations
tmail.delete_no_send_fields
- Net::SMTP.start(settings[:address], settings[:port], settings[:domain],
- settings[:user_name], settings[:password], settings[:authentication]) do |smtp|
+ smtp = Net::SMTP.new(settings[:address], settings[:port])
+ smtp.enable_starttls_auto if settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
+ smtp.start(settings[:domain], settings[:user_name], settings[:password],
+ settings[:authentication]) do |smtp|
smtp.sendmail(tmail.encoded, tmail['sender'], destinations)
end
end
View
68 sendgrid_example.rb
@@ -0,0 +1,68 @@
+$LOAD_PATH << File.dirname(__FILE__) + '/lib'
+
+require 'mlist'
+require 'mlist/email_server/smtp'
+require 'singleton'
+
+require 'active_record'
+SQLITE_DATABASE = "spec/sqlite3.db"
+ActiveRecord::Base.silence do
+ ActiveRecord::Base.configurations = {'test' => {
+ 'adapter' => 'sqlite3',
+ 'database' => SQLITE_DATABASE
+ }}
+ ActiveRecord::Base.establish_connection 'test'
+ load "spec/fixtures/schema.rb"
+end
+
+
+class MailList
+ include Singleton
+ include MList::List
+
+ def address
+ 'sendgridtest@discuss.memberhub.com'
+ end
+
+ def label
+ 'Sendgrid Testing'
+ end
+
+ def list_id
+ "sendgrid_testing"
+ end
+
+ def subscribers
+ [MList::EmailSubscriber.new('something@nomail.net'), MList::EmailSubscriber.new('anotherthing@nomail.net')]
+ end
+end
+
+class ListManager
+ include MList::Manager
+
+ def lists(email)
+ [MailList.instance]
+ end
+end
+
+list_manager = ListManager.new
+mlist_server = MList::Server.new(
+ :list_manager => list_manager,
+ :email_server => MList::EmailServer::Smtp.new(
+ :enable_starttls_auto => true,
+ :address => "smtp.sendgrid.net",
+ :port => "587",
+ :authentication => :plain,
+ :domain => 'your.domain.com',
+ :user_name => "smtp@your.domain.com",
+ :password => "yourpassword"
+ )
+)
+
+post = MList::EmailPost.new({
+ :subscriber => MailList.instance.subscribers.first,
+ :subject => "I'm a Program!",
+ :text => "My simple message that isn't too short"
+})
+
+mlist_server.mail_list(MailList.instance).post(post)

0 comments on commit 50b5f13

Please sign in to comment.
Something went wrong with that request. Please try again.