Class: MailRelay::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_relay/base.rb

Overview

A generic email relay object. Retrieves messages from a mail server and resends them to a list of recievers. In subclasses, override the methods #relay_address?, #sender_allowed? and #receivers to constrain which mails are sent to whom.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Base

Returns a new instance of Base.



41
42
43
# File 'lib/mail_relay/base.rb', line 41

def initialize(message)
  @message = message
end

Class Attribute Details

.receiver_headerObject

Define a header that contains the original receiver address. This header could be set by the mail server.



10
11
12
# File 'lib/mail_relay/base.rb', line 10

def receiver_header
  @receiver_header
end

.retrieve_countObject

Number of emails to retrieve in one batch.



13
14
15
# File 'lib/mail_relay/base.rb', line 13

def retrieve_count
  @retrieve_count
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



39
40
41
# File 'lib/mail_relay/base.rb', line 39

def message
  @message
end

Class Method Details

.relay_currentObject

Retrieve, process and delete all mails from the mail server.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mail_relay/base.rb', line 16

def relay_current
  begin
    last_exception = nil
    
    mails = Mail.find_and_delete(:count => retrieve_count) do |message|
      begin
        new(message).relay
      rescue Exception => e
        last_exception = e
      end
    end
    
    raise(last_exception) if last_exception
    
  end while mails.size >= retrieve_count
end

Instance Method Details

#envelope_receiver_nameObject

The receiver account that originally got this email. You probably have to re-implement this method depending on your mail server setup. Returns only the part before the @ sign.



83
84
85
86
87
# File 'lib/mail_relay/base.rb', line 83

def envelope_receiver_name
  receiver_from_x_header || 
  receiver_from_received_header || 
  raise("Could not determine original receiver for email:\n#{message.header}")
end

#receiver_from_received_headerObject

Heuristic method to find actual receiver of the message. May return nil if could not determine.



97
98
99
100
101
102
# File 'lib/mail_relay/base.rb', line 97

def receiver_from_received_header
  if received = message.received
    received = received.first if received.respond_to?(:first)
    received.info[/ for .*?([^\s<>]+)@[^\s<>]+/, 1]
  end
end

#receiver_from_x_headerObject

Try to read the envelope receiver from the given x header



105
106
107
108
109
# File 'lib/mail_relay/base.rb', line 105

def receiver_from_x_header
  if field = message.header[self.class.receiver_header]
    field.to_s.split('@', 2).first
  end
end

#receiversObject

List of receiver email addresses for the resent email.



122
123
124
# File 'lib/mail_relay/base.rb', line 122

def receivers
  []
end

#reject_not_allowedObject

If the email sender was not allowed to post messages, this method is called. Silently ignores the message by default.



70
71
72
# File 'lib/mail_relay/base.rb', line 70

def reject_not_allowed
  # do nothing
end

#reject_not_existingObject

If the email is sent to an address that is not a valid relay, this method is called. Silently ignores the message by default.



76
77
78
# File 'lib/mail_relay/base.rb', line 76

def reject_not_existing
  # do nothing
end

#relayObject

Process the given email.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mail_relay/base.rb', line 46

def relay
  if relay_address?
    if sender_allowed?
      resend_to(receivers)
    else
      reject_not_allowed
    end
  else
    reject_not_existing
  end
end

#relay_address?Boolean

Is the mail sent to a valid relay address?

Returns:

  • (Boolean)


112
113
114
# File 'lib/mail_relay/base.rb', line 112

def relay_address?
  true
end

#resend_to(destinations) ⇒ Object

Send the same mail as is to all receivers, if any.



59
60
61
62
63
64
65
66
# File 'lib/mail_relay/base.rb', line 59

def resend_to(destinations)
  if destinations.size > 0
    add_custom_message_destinations
    message.destinations = destinations
    message.header['Precedence'] = 'list'
    deliver(message)
  end
end

#sender_allowed?Boolean

Is the mail sender allowed to post to this address

Returns:

  • (Boolean)


117
118
119
# File 'lib/mail_relay/base.rb', line 117

def sender_allowed?
  true
end

#sender_emailObject

The email address of the sender. As found in the from header.



90
91
92
# File 'lib/mail_relay/base.rb', line 90

def sender_email
  @sender_email ||= message.from && message.from.first
end