Class: Gurgitate::Mailmessage

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

Overview

A complete mail message. This is the base class for gurgitate-mail itself: if you want to use gurgitate-mail to create new messages, this is what you want to use.

Direct Known Subclasses

Gurgitate

Constant Summary collapse

Fromregex =
/([^ ]+@[^ ]+) \(.*\)|[^<][<](.*@.*)[>]|([^ ]+@[^ ]+)/

Instance Attribute Summary collapse

Attributes inherited from Message

#body, #headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#header, #to_s

Constructor Details

#initialize(text = nil, recipient = nil, sender = nil) ⇒ Mailmessage

Returns a new instance of Mailmessage.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gurgitate/mailmessage.rb', line 87

def initialize(text=nil, recipient=nil, sender=nil)

    @recipient = recipient
    @sender = sender

    begin
        # ASCII_8BIT is what Ruby 1.9 and up calls
        # "binary stream of unknown encoding".
        #
        # At least it treats the ASCII characters
        # as strings, so I can do regex things
        # with them
        text.force_encoding(Encoding::ASCII_8BIT)
    rescue NameError  # Ruby 1.9 and up
        true
    end

    if text
        (@headertext,@body)=text.split(/\n\n/m,2)
        @headers=MailHeaders.new(@headertext);
        Fromregex.match(
            if @headers["From"] then
                @headers["From"][0].contents
            else
                ""
            end);
        @from=$+
    else
        super(text)
    end
end

Instance Attribute Details

#recipientObject

Returns the value of attribute recipient.



20
21
22
# File 'lib/gurgitate/mailmessage.rb', line 20

def recipient
  @recipient
end

#senderObject

The envelope sender and recipient, if anyone thought to mention them to us.



19
20
21
# File 'lib/gurgitate/mailmessage.rb', line 19

def sender
  @sender
end

Class Method Details

.create(*args) ⇒ Object

Creates a new mail 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.



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
84
85
# File 'lib/gurgitate/mailmessage.rb', line 55

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

        %w/sender recipient body/.each do |key|
            if options.has_key? key.to_sym
                instance_variable_set("@#{key}", options[key.to_sym])
                options.delete key.to_sym
            end
        end

        @headers = MailHeaders.new(options)
    end

    message
end

Instance Method Details

#fromObject

Returns the message’s sender



122
# File 'lib/gurgitate/mailmessage.rb', line 122

def from; @sender || @headers.from; end

#toObject

Returns all the candidates for a recipient



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gurgitate/mailmessage.rb', line 125

def to
    if @recipient
        then @recipient
    elsif @headers["To"]
        then @headers["To"][0].contents
    elsif @headers["Cc"]
        then @headers["Cc"][0].contents
    elsif @headers["X-Original-To"]
        then @headers["X-Original-To"][0].contents
    else
        ""
    end
end

#to_mboxObject

Returns the mail message formatted for mbox



140
# File 'lib/gurgitate/mailmessage.rb', line 140

def to_mbox; @headers.to_mbox + "\n\n" + @body; end