Class: SystemMail::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/system_mail/message.rb

Overview

Manages compiling an email message from various attributes and files.

Constant Summary collapse

BASE64_SIZE =
76
UTF8_SIZE =
998
SETTINGS =
{
  :sendmail => '/usr/sbin/sendmail -t',
  :base64 => 'base64',
  :file => 'file --mime-type --mime-encoding -b',
  :storage => ENV['TMP'] || '/tmp',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Creates new message. Available options:

  • :text, String, Textual part of the message

  • :enriched, String, Enriched alternative of the message (RFC 1896)

  • :html, String, HTML alternative of the message

  • :from, String, ‘From:’ header for the message

  • :to, String or Array of Strings, ‘To:’ header, if Arrey, it gets joined by ‘, ’

  • :subject, String, Subject of the message, it gets encoded automatically

  • :files, File or String of file path or Array of them, Attachments of the message

Options :text, :enriched and :html are interchangeable. Option :to is required.

Examples:

mail = Message.new(
  :from        => '[email protected]',
  :to          => '[email protected]',
  :subject     => 'test subject',
  :text        => 'big small normal',
  :html        => File.read('test.html'),
  :attachments => [File.open('Gemfile'), 'attachment.zip'],
)


44
45
46
47
48
49
50
51
52
# File 'lib/system_mail/message.rb', line 44

def initialize(options={})
  @body = {}
  @to = []
  @mutex = Mutex.new
  %W(text enriched html from to subject attachments).each do |option|
    name = option.to_sym
    send(name, options[name])
  end
end

Instance Method Details

#deliverObject

Delivers the message using sendmail.

Example:

mail.deliver #=> nil


61
62
63
64
65
66
67
68
# File 'lib/system_mail/message.rb', line 61

def deliver
  validate
  with_storage do
    write_headers
    write_message
    send_message
  end
end

#settingsObject



70
71
72
# File 'lib/system_mail/message.rb', line 70

def settings
  @settings ||= SETTINGS.dup
end