Class: CircularMail::PostMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/circular-mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



50
51
52
# File 'lib/circular-mail.rb', line 50

def attachments
  @attachments
end

#mail_per_dispatchObject

Returns the value of attribute mail_per_dispatch.



52
53
54
# File 'lib/circular-mail.rb', line 52

def mail_per_dispatch
  @mail_per_dispatch
end

#message_bodyObject

Returns the value of attribute message_body.



48
49
50
# File 'lib/circular-mail.rb', line 48

def message_body
  @message_body
end

#message_character_setObject

Returns the value of attribute message_character_set.



47
48
49
# File 'lib/circular-mail.rb', line 47

def message_character_set
  @message_character_set
end

#message_formatObject

Returns the value of attribute message_format.



46
47
48
# File 'lib/circular-mail.rb', line 46

def message_format
  @message_format
end

#message_subjectObject

Returns the value of attribute message_subject.



49
50
51
# File 'lib/circular-mail.rb', line 49

def message_subject
  @message_subject
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



51
52
53
# File 'lib/circular-mail.rb', line 51

def recipients
  @recipients
end

#senderObject

Returns the value of attribute sender.



54
55
56
# File 'lib/circular-mail.rb', line 54

def sender
  @sender
end

#sender_emailObject

Returns the value of attribute sender_email.



55
56
57
# File 'lib/circular-mail.rb', line 55

def sender_email
  @sender_email
end

#smtp_serverObject

Returns the value of attribute smtp_server.



41
42
43
# File 'lib/circular-mail.rb', line 41

def smtp_server
  @smtp_server
end

#smtp_server_authenticationObject

Returns the value of attribute smtp_server_authentication.



45
46
47
# File 'lib/circular-mail.rb', line 45

def smtp_server_authentication
  @smtp_server_authentication
end

#smtp_server_passwordObject

Returns the value of attribute smtp_server_password.



44
45
46
# File 'lib/circular-mail.rb', line 44

def smtp_server_password
  @smtp_server_password
end

#smtp_server_portObject

Returns the value of attribute smtp_server_port.



42
43
44
# File 'lib/circular-mail.rb', line 42

def smtp_server_port
  @smtp_server_port
end

#smtp_server_usernameObject

Returns the value of attribute smtp_server_username.



43
44
45
# File 'lib/circular-mail.rb', line 43

def smtp_server_username
  @smtp_server_username
end

#wait_after_dispatchObject

Returns the value of attribute wait_after_dispatch.



53
54
55
# File 'lib/circular-mail.rb', line 53

def wait_after_dispatch
  @wait_after_dispatch
end

Instance Method Details

#add_attachment(filename) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/circular-mail.rb', line 183

def add_attachment(filename)
  if @attachments.include?(filename)
    CircularMail::die "Attachment '#{filename}' already added!" if CircularMail::config('strictness')
  else
    CircularMail::die "Attachment file '#{filename}' does not exists!" if CircularMail::config('strictness') && !File.exists?(filename)
    @attachments << filename
  end
end

#add_recipient(*args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/circular-mail.rb', line 144

def add_recipient(*args)
  if CircularMail::config('strictness')
    CircularMail::die "Email address '#{args[0]}' already added to recipients list!" if @recipients.include?(args[0])
    CircularMail::die "Email address '#{args[0]}' is invalid!" if CircularMail::config('email_validation').match(args[0]).nil? && CircularMail::config('email_group_validation').match(args[0]).nil?
  end
  message_vars = []
  if args.length > 1
    (2..args.length).each { |var|
      message_vars << args[var - 1]
    }
  end
  @recipients << {args[0] => message_vars}
end

#load_from_marshal(filename) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/circular-mail.rb', line 297

def load_from_marshal(filename)
  if File.exists?(filename)
    o = File.open(filename, 'rb') {|f| m = Marshal::load(f)}
    @smtp_server = o.smtp_server
    @smtp_server_port = o.smtp_server_port
    @smtp_server_username = o.smtp_server_username
    @smtp_server_password = o.smtp_server_password
    @smtp_server_authentication = o.smtp_server_password
    @message_format = o.smtp_server_password
    @message_character_set = o.smtp_server_password
    @message_body = o.smtp_server_password
    @message_subject = o.smtp_server_password
    @attachments = o.smtp_server_password
    @recipients = o.smtp_server_password
    @mail_per_dispatch = o.smtp_server_password
    @wait_after_dispatch = o.smtp_server_password
    @sender = o.smtp_server_password
    @sender_email = o.smtp_server_password
  elsif CircularMail::config('strictness')
    CircularMail::die "Cannot load object because file '#{filename}' does not exists!"
  end
end

#load_from_yaml(filename) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/circular-mail.rb', line 274

def load_from_yaml(filename)
  if File.exists?(filename)
    o = YAML.load(File.read(filename))
    @smtp_server = o.smtp_server
    @smtp_server_port = o.smtp_server_port
    @smtp_server_username = o.smtp_server_username
    @smtp_server_password = o.smtp_server_password
    @smtp_server_authentication = o.smtp_server_password
    @message_format = o.smtp_server_password
    @message_character_set = o.smtp_server_password
    @message_body = o.smtp_server_password
    @message_subject = o.smtp_server_password
    @attachments = o.smtp_server_password
    @recipients = o.smtp_server_password
    @mail_per_dispatch = o.smtp_server_password
    @wait_after_dispatch = o.smtp_server_password
    @sender = o.smtp_server_password
    @sender_email = o.smtp_server_password
  elsif CircularMail::config('strictness')
    CircularMail::die "Cannot load object because file '#{filename}' does not exists!"
  end
end

#remove_attachment(filename) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/circular-mail.rb', line 192

def remove_attachment(filename)
  if @attachments.include?(filename)
    @attachments.keep_if { |file| file != filename}
  elsif CircularMail::config('strictness')
    CircularMail::die "Attachment list does not contain file '#{filename}' !"
  end
end

#remove_recipient(email) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/circular-mail.rb', line 158

def remove_recipient(email)
  if @recipients.include?(email)
    @recipients.keep_if { |address| address != email}
  elsif CircularMail::config('strictness')
    CircularMail::die "Recipient list does not contain email address '#{email}' !"
  end
end

#save_as_marshal(filename) ⇒ Object



268
269
270
271
272
# File 'lib/circular-mail.rb', line 268

def save_as_marshal(filename)
  CircularMail::die "Cannot save object because file '#{filename}' already exists!" if File.exists?(filename) && CircularMail::config('strictness')
  filename = Dir.getwd() + "/dump #{CircularMail.file_timestamp()}.marshal" unless File.exists?(filename)
  File.open(filename, 'w') { |f| f.write(Marshal.dump(self)) }
end

#save_as_yaml(filename) ⇒ Object



262
263
264
265
266
# File 'lib/circular-mail.rb', line 262

def save_as_yaml(filename)
  CircularMail::die "Cannot save object because file '#{filename}' already exists!" if File.exists?(filename) && CircularMail::config('strictness')
  filename = Dir.getwd() + "/dump #{CircularMail.file_timestamp()}.yaml" unless File.exists?(filename)
  File.open(filename, 'w') { |f| f.write(YAML.dump(self)) }
end

#sendObject Also known as: send_all

Start sending to all recipients using the standard process



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
253
# File 'lib/circular-mail.rb', line 201

def send()
  self.save_as_yaml(Dir.getwd() + "/backup #{CircularMail.file_timestamp()}.yaml") if CircularMail::config('auto_backup_postmaster')
  messages_sent = 0
  messages_dispatch = 0

  Net::SMTP.start(@smtp_server, @smtp_server_port, 'localhost', @smtp_server_username, @smtp_server_password, @smtp_server_authentication) do |smtp|
    message_index = 0
    message_body_personal = ""

    @recipients.each { |recipient|
      message_index += 1
      to_email = ""

      recipient.each { |mail, vars|
        to_email = mail
        var_index = 0
        message_body_personal = @message_body
        if vars.length > 0
          vars.each { |var_value|
            var_index += 1
            message_body_personal = message_body_personal.gsub("@#{var_index.to_s}@", var_value)
          }
        else
          message_body_personal = @message_body
        end
      }

      message = CircularMail::Message.new(@message_format, @message_character_set, message_body_personal)
      message.attachments = @attachments
      message.header.add_field('From', "#{@sender}<#{@sender_email}>")
      message.header.add_field('Sender', "#{@sender}<#{@sender_email}>")
      message.header.add_field('Reply-To', @sender_email)
      message.header.add_field('To', "<#{to_email}>")
      message.header.add_field('Subject', @subject)
      message.header.add_field('Comments', 'This email was generated and posted by the CircularMail ruby library. Author is not responsible for body content!')
      message.header.add_field('Date', CircularMail::mail_timestamp())
      $message_id = Digest::MD5.hexdigest(message_body_personal)
      message.header.add_field('Message-ID', "<CircularMail@#{$message_id}>")

      full_message = message.get()

      smtp.send_message full_message, @sender_email, to_email

      messages_sent += 1
      messages_dispatch += 1
      if messages_dispatch == @mail_per_dispatch
        sleep @wait_after_dispatch
        messages_dispatch = 0
      end
    }
  end
  return messages_sent
end

#send_to(email) ⇒ Object

Send the message to a single recipient Useful if a recipient is complaining that he did not get the message, due to a spam filter or accidentally deleted the email



258
259
260
# File 'lib/circular-mail.rb', line 258

def send_to(email)

end

#set_message(format, subject, message) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/circular-mail.rb', line 100

def set_message(format, subject, message)
  self.message_format = format
  if File.exists?(message.to_s)
    self.message_body = File.read(message.to_s)
  else
    self.message_body = message
  end
  @subject = subject
end

#set_message_variable(*args) ⇒ Object Also known as: set_message_variables



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/circular-mail.rb', line 166

def set_message_variable(*args)
  # This method will add the email to recipients list if it does not exists yet!

  unless @recipients.include?(args[0])   #TODO: fix me! Need to look for email address INSIDE hash!

    add_recipient(*args)
  else
    # Ok, email exists lets override whatever variables it has

    message_vars = []
    if args.length > 1
      (2..args.length).each { |var|
        message_vars << args[var - 1]
      }
    end
    @recipients[@recipients.index(args[0])] = message_vars
  end
end

#set_smtp_server(server_and_port, username, password, auth_type) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/circular-mail.rb', line 92

def set_smtp_server(server_and_port, username, password, auth_type)
  self.smtp_server = server_and_port.to_s.split(":")[0]
  self.smtp_server_port = server_and_port.to_s.split(":")[1]
  self.smtp_server_username = username.to_s
  self.smtp_server_password = password.to_s
  self.smtp_server_authentication = auth_type.to_s
end

#smtp_authentication=(auth_type) ⇒ Object



85
86
87
88
89
90
# File 'lib/circular-mail.rb', line 85

def smtp_authentication=(auth_type)
  if CircularMail::config('strictness')
    CircularMail::die "Authentication type '#{auth_type}' is not a valid option!" unless CircularMail::config('valid_authentications').include?(auth_type)
  end
  @smtp_server_authentication = auth_type
end