Class: Gurgitate::Message

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

Overview

A complete mail message.

Direct Known Subclasses

Mailmessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil) ⇒ Message

Creates a new Gurgitate message from a pre-existing message. This is what is used when gurgitate-mail is used as a mail filter.

ARGUMENTS
text

An RFC822-formatted message.

recipient

The recipient of the email message, from the MTA

sender

The sender of the email message, also from the MTA

All of its arguments can be nil: if called with no arguments, it simply returns an empty email message, which can be populated after the fact.



89
90
91
92
93
94
95
96
97
# File 'lib/gurgitate/message.rb', line 89

def initialize(text=nil)
    if text
        (@headertext,@body)=text.split(/\n\n/,2)
        @headers=Headers.new(@headertext);
    else
        @headers = Headers.new
        @body = ""
    end
end

Instance Attribute Details

#bodyObject

The body of the message



18
19
20
# File 'lib/gurgitate/message.rb', line 18

def body
  @body
end

#headersObject (readonly)

The headers of the message



16
17
18
# File 'lib/gurgitate/message.rb', line 16

def headers
  @headers
end

Class Method Details

.create(*args) ⇒ Object

Creates a new message from the options hash, and the body of the message in a string.

This can actually be invoked in several ways:

Gurgitate::Mailmessage.create "This is the message body",
   :from    => "[email protected]",
   :to      => "[email protected]",
   :subject => "This is the message subject"

This results in an email message that, when rendered via to_s, will look like this:

From: [email protected]
To: [email protected]
Subject: This is the message subject

This is the message body

If you prefer to do things entirely by options hashes, as some do, you can substitute a :body key for the first argument:

Gurgitate::Mailmessage.create(
    :body    => "This is the message body",
    :from    => "[email protected]",
    :to      => "[email protected]",
    :subject => "This is the message subject"
)

There are two other special options you can use: :sender and :recipient. These are used to specify the sender and recipient of email messages, when the message is sent via SMTP.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gurgitate/message.rb', line 53

def self.create(*args)
    options = body = nil

    if String === args[0]
        options = args[1]
        body = args[0]
    elsif Hash === args[0]
        options = args[0]
    else
        options = {}
    end

    message = self.new

    message.instance_eval do
        if body
            @body=body
        end

        @headers = Headers.new(options)
    end

    message
end

Instance Method Details

#header(name) ⇒ Object

Returns the header name, which is, note, a HeaderBag of all headers by that name, not just a single header.

If you want the text of the header, then you have to coerce it to a string:

header("name").to_s


107
108
109
# File 'lib/gurgitate/message.rb', line 107

def header(name)
    @headers[name].each { |h| h.contents }.join(", ")
end

#to_sObject

Returns the formatted mail message



112
# File 'lib/gurgitate/message.rb', line 112

def to_s; @headers.to_s + "\n\n" + ( @body || ""); end