Class: QuickBase::Emailer
- Inherits:
-
Object
- Object
- QuickBase::Emailer
- Defined in:
- lib/QuickBaseEmailer.rb
Overview
Simple class to read data from QuickBase and email it using the SMTP client that comes with Ruby. This class should handle sending emails to people in the same domain as you, e.g. when your ‘from’ and ‘to’ email addresses all end in ‘@ourcompany.com’. To send emails outside your domain, it’s likely that you will have to override the sendUnauthenticatedEmail() method or the sendAuthenticatedEmail() method and add authentication acceptable to your email server.
To send email via GMail’s server:
-
comment out the ‘require “net/smtp”’ line above
-
uncomment the #require ‘tls_smtp’ line
-
use smtp.gmail.com as the mail server
Instance Attribute Summary collapse
-
#authenticationType ⇒ Object
writeonly
Sets the attribute authenticationType.
-
#emailPassword ⇒ Object
writeonly
Sets the attribute emailPassword.
-
#emailUsername ⇒ Object
writeonly
Sets the attribute emailUsername.
-
#from ⇒ Object
writeonly
Sets the attribute from.
-
#fromDomain ⇒ Object
writeonly
Sets the attribute fromDomain.
-
#mailPort ⇒ Object
writeonly
Sets the attribute mailPort.
-
#mailServer ⇒ Object
writeonly
Sets the attribute mailServer.
-
#message ⇒ Object
writeonly
Sets the attribute message.
-
#subject ⇒ Object
writeonly
Sets the attribute subject.
-
#to ⇒ Object
writeonly
Sets the attribute to.
Class Method Summary collapse
Instance Method Summary collapse
- #buildEmail(from, to, subject, message) ⇒ Object
-
#initialize(username, password, emailUsername = nil, emailPassword = nil) ⇒ Emailer
constructor
A new instance of Emailer.
- #readEmailServerConfigFromQuickBase(dbid, fieldNames, fids, query = nil, qname = nil, qid = nil) ⇒ Object
- #readEmailsToSendFromQuickBase(dbid, fieldNames, fids, query = nil, qname = nil, qid = nil) ⇒ Object
- #sendAuthenticatedEmail(email, from, to, mailServer, mailPort, fromDomain, emailUsername, emailPassword, authenticationType) ⇒ Object
- #sendBccEmail(from, to, bcc, subject, message, mailServer, mailPort, fromDomain) ⇒ Object
- #sendEmail(from, to, subject, message, mailServer, mailPort, fromDomain) ⇒ Object
- #sendEmailMessages(emailMessages, emailConfiguration) ⇒ Object
- #sendUnauthenticatedEmail(email, from, to, mailServer, mailPort, fromDomain) ⇒ Object
- #validateEmailData ⇒ Object
Constructor Details
#initialize(username, password, emailUsername = nil, emailPassword = nil) ⇒ Emailer
Returns a new instance of Emailer.
41 42 43 44 45 46 47 |
# File 'lib/QuickBaseEmailer.rb', line 41 def initialize(username,password, emailUsername=nil, emailPassword=nil) @username,@password = username,password @emailUsername, @emailPassword=emailUsername, emailPassword @emailUsername = @username if @emailUsername.nil? @emailPassword = @password if @emailPassword.nil? @mailPort = 25 end |
Instance Attribute Details
#authenticationType=(value) ⇒ Object (writeonly)
Sets the attribute authenticationType
38 39 40 |
# File 'lib/QuickBaseEmailer.rb', line 38 def authenticationType=(value) @authenticationType = value end |
#emailPassword=(value) ⇒ Object (writeonly)
Sets the attribute emailPassword
39 40 41 |
# File 'lib/QuickBaseEmailer.rb', line 39 def emailPassword=(value) @emailPassword = value end |
#emailUsername=(value) ⇒ Object (writeonly)
Sets the attribute emailUsername
39 40 41 |
# File 'lib/QuickBaseEmailer.rb', line 39 def emailUsername=(value) @emailUsername = value end |
#from=(value) ⇒ Object (writeonly)
Sets the attribute from
37 38 39 |
# File 'lib/QuickBaseEmailer.rb', line 37 def from=(value) @from = value end |
#fromDomain=(value) ⇒ Object (writeonly)
Sets the attribute fromDomain
38 39 40 |
# File 'lib/QuickBaseEmailer.rb', line 38 def fromDomain=(value) @fromDomain = value end |
#mailPort=(value) ⇒ Object (writeonly)
Sets the attribute mailPort
38 39 40 |
# File 'lib/QuickBaseEmailer.rb', line 38 def mailPort=(value) @mailPort = value end |
#mailServer=(value) ⇒ Object (writeonly)
Sets the attribute mailServer
38 39 40 |
# File 'lib/QuickBaseEmailer.rb', line 38 def mailServer=(value) @mailServer = value end |
#message=(value) ⇒ Object (writeonly)
Sets the attribute message
37 38 39 |
# File 'lib/QuickBaseEmailer.rb', line 37 def (value) @message = value end |
#subject=(value) ⇒ Object (writeonly)
Sets the attribute subject
37 38 39 |
# File 'lib/QuickBaseEmailer.rb', line 37 def subject=(value) @subject = value end |
#to=(value) ⇒ Object (writeonly)
Sets the attribute to
37 38 39 |
# File 'lib/QuickBaseEmailer.rb', line 37 def to=(value) @to = value end |
Class Method Details
.sendEmailsFromQuickBase(username, password, configDBID, emailsDBID) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/QuickBaseEmailer.rb', line 285 def Emailer.sendEmailsFromQuickBase(username,password,configDBID,emailsDBID) emailer = Emailer.new(username,password) configFieldNames = Hash.new configFieldNames["mailServer"]="mailServer" configFieldNames["mailPort"]="mailPort" configFieldNames["fromDomain"]="fromDomain" configFieldNames["authenticationType"]="authenticationType" configFieldIDs = Hash.new configFieldIDs["mailServer"]="6" configFieldIDs["mailPort"]="7" configFieldIDs["fromDomain"]="8" configFieldIDs["authenticationType"]="9" emailConfiguration = emailer.readEmailServerConfigFromQuickBase(configDBID,configFieldNames,configFieldIDs) if emailConfiguration puts "Read an email configuration from '#{configDBID}'." emailFieldNames = Hash.new emailFieldNames["from"]="from" emailFieldNames["to"]="to" emailFieldNames["subject"]="subject" emailFieldNames["message"]="message" emailFieldIDs = Hash.new emailFieldIDs["from"]="6" emailFieldIDs["to"]="7" emailFieldIDs["subject"]="8" emailFieldIDs["message"]="9" emailMessages = emailer.readEmailsToSendFromQuickBase(emailsDBID,emailFieldNames,emailFieldIDs) if emailMessages and emailMessages["from"].length > 0 numMessages = emailMessages["from"].length puts "Read #{numMessages} email messages from '#{emailsDBID}'." emailer.sendEmailMessages(emailMessages, emailConfiguration) else puts "Did not read email messages from '#{emailsDBID}'." end else puts "Did not read an email configuration from '#{configDBID}'." end end |
Instance Method Details
#buildEmail(from, to, subject, message) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/QuickBaseEmailer.rb', line 169 def buildEmail(from,to,subject,) from.strip! email = "From:#{from}\n" email << "Date:#{Time.now}\n" to.each{|toAddress| toAddress.strip! email << "To:#{toAddress}\n" } email << "Subject:#{subject}\n" email << "#{}\n" email end |
#readEmailServerConfigFromQuickBase(dbid, fieldNames, fids, query = nil, qname = nil, qid = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/QuickBaseEmailer.rb', line 182 def readEmailServerConfigFromQuickBase(dbid,fieldNames,fids,query=nil,qname=nil,qid=nil) emailConfiguration = nil if not fids.is_a?(Hash) raise "'fids' must be a Hash of email server configuration field names and their QuickBase field id's" end if not fieldNames.is_a?(Hash) raise "'fieldNames' must be a Hash of email configuration fields and their field names in QuickBase" end clist = "" ["mailServer","mailPort","fromDomain","authenticationType"].each{|fieldName| if fids[fieldName] clist << fids[fieldName] clist << "." unless fieldName == "authenticationType" else raise "'#{fieldName}' fid entry is missing from the 'fids' Hash" end if not fieldNames[fieldName] raise "'#{fieldName}' QuickBase field name entry is missing from the 'fieldNames' Hash" end } qbc = QuickBase::Client.new(@username,@password) if qbc and qbc.requestSucceeded qbc.getSchema(dbid) if qbc.requestSucceeded emailConfiguration = qbc.getAllValuesForFields(dbid,fieldNames.values,query,qname,qid,clist,nil,"structured","num-1") emailConfiguration = nil if emailConfiguration.length == 0 else raise "Error accessing QuickBase table '#{dbid}'." end else raise "Error accessing QuickBase. Please check your internet connection, username (#{@username}) and password (#{@password})" end qbc.signOut if qbc emailConfiguration end |
#readEmailsToSendFromQuickBase(dbid, fieldNames, fids, query = nil, qname = nil, qid = nil) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/QuickBaseEmailer.rb', line 218 def readEmailsToSendFromQuickBase(dbid,fieldNames,fids,query=nil,qname=nil,qid=nil) emailMessages = nil if not fids.is_a?(Hash) raise "'fids' must be a Hash of email field names and their QuickBase field id's" end if not fieldNames.is_a?(Hash) raise "'fieldNames' must be a Hash of email fields and their field names in QuickBase" end clist = "" ["from","to","subject","message"].each{|fieldName| if fids[fieldName] clist << fids[fieldName] clist << "." unless fieldName == "message" else raise "'#{fieldName}' fid entry is missing from the 'fids' Hash" end if not fieldNames[fieldName] raise "'#{fieldName}' QuickBase field name entry is missing from the 'fieldNames' Hash" end } qbc = QuickBase::Client.new(@username,@password) if qbc and qbc.requestSucceeded qbc.getSchema(dbid) if qbc.requestSucceeded emailMessages = qbc.getAllValuesForFields(dbid,fieldNames.values,query,qname,qid,clist) emailMessages = nil if emailMessages.length == 0 else raise "Error accessing QuickBase table '#{dbid}'." end else raise "Error accessing QuickBase. Please check your internet connection, username and password" end qbc.signOut if qbc emailMessages end |
#sendAuthenticatedEmail(email, from, to, mailServer, mailPort, fromDomain, emailUsername, emailPassword, authenticationType) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/QuickBaseEmailer.rb', line 95 def sendAuthenticatedEmail(email,from,to,mailServer, mailPort, fromDomain, emailUsername,emailPassword,authenticationType) begin Net::SMTP::start(mailServer,mailPort,fromDomain,emailUsername,emailPassword,authenticationType) { |smtp| smtp.(email,from,to) } rescue StandardError => error raise "Error sending email: #{error}" end end |
#sendBccEmail(from, to, bcc, subject, message, mailServer, mailPort, fromDomain) ⇒ Object
49 50 51 52 53 |
# File 'lib/QuickBaseEmailer.rb', line 49 def sendBccEmail(from, to, bcc, subject, , mailServer, mailPort, fromDomain) @bcc = bcc.dup sendEmail(from, to, subject, , mailServer, mailPort, fromDomain) @bcc = nil end |
#sendEmail(from, to, subject, message, mailServer, mailPort, fromDomain) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/QuickBaseEmailer.rb', line 55 def sendEmail(from, to, subject, , mailServer, mailPort, fromDomain) @from = from if from @to = to if to @subject = subject if subject @message = if @mailServer = mailServer if mailServer @mailPort = mailPort if mailPort @fromDomain = fromDomain if fromDomain if validateEmailData email = buildEmail(@from,@to,@subject,@message) @to = @to + @bcc if @bcc if @authenticationType.nil? or @authenticationType == "" sendUnauthenticatedEmail(email,@from,@to,@mailServer, @mailPort, @fromDomain) else sendAuthenticatedEmail(email, @from, @to, @mailServer, @mailPort, @fromDomain, @emailUsername, @emailPassword, @authenticationType ) end end end |
#sendEmailMessages(emailMessages, emailConfiguration) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/QuickBaseEmailer.rb', line 254 def sendEmailMessages(emailMessages, emailConfiguration) if emailMessages and emailConfiguration raise "'emailMessages' must be a Hash" if not emailMessages.is_a?(Hash) raise "'emailConfiguration' must be a Hash" if not emailConfiguration.is_a?(Hash) (0..(emailMessages["from"].length-1)).each{|i| begin from = emailMessages["from"][i] toAddresses = emailMessages["to"][i].split(/\<BR\/\>/) subject = emailMessages["subject"][i] subject.gsub!("<BR/>","") = emailMessages["message"][i] .gsub!("<BR/>","") sendEmail(from, toAddresses, subject, , emailConfiguration["mailServer"][0], emailConfiguration["mailPort"][0].to_i, emailConfiguration["fromDomain"][0]) puts "Sent '#{subject}' email." rescue StandardError => error puts error end } end end |
#sendUnauthenticatedEmail(email, from, to, mailServer, mailPort, fromDomain) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/QuickBaseEmailer.rb', line 85 def sendUnauthenticatedEmail(email,from,to,mailServer, mailPort, fromDomain) begin Net::SMTP::start(mailServer,mailPort,fromDomain) { |smtp| smtp.(email,from,to) } rescue StandardError => error raise "Error sending email: #{error}" end end |
#validateEmailData ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/QuickBaseEmailer.rb', line 106 def validateEmailData raise "'@from' email address is missing" if @from.nil? raise "'@to' email address is missing" if @to.nil? @subject = "" if @subject.nil? @message = "" if @message.nil? raise "'@mailServer' is missing" if @mailServer.nil? raise "'@mailPort' is missing" if @mailPort.nil? raise "'@mailServer' must be a String" if not @mailServer.is_a?(String) if @mailServer and @fromDomain.nil? @fromDomain = @mailServer end raise "'@fromDomain' must be a String" if not @fromDomain.is_a?(String) if not @to.is_a?(Array) raise "'@to' must be an Array" end @to.each { |toAddress| if not toAddress.index('@') raise "'#{toAddress}' is not a valid email address" end } if @bcc if not @bcc.is_a?(Array) raise "'@bcc' must be an Array" end @bcc.each { |toAddress| if not toAddress.index('@') raise "'#{toAddress}' is not a valid email address" end } end if not @from.is_a?(String) raise "'@from' must be an String" end if not @from.index('@') raise "'@from' is not a valid email address" end if not @mailPort.is_a?(Numeric) raise "'@mailPort' must be a positive number" end raise "'@subject' must be a String" if not @subject.is_a?(String) raise "'@message' must be a String" if not @message.is_a?(String) if @authenticationType and @authenticationType != "" if not ["login","cram-md5","plain"].include?(@authenticationType) raise "'@authenticationType' #{@authenticationType} is invalid." end end true end |