Class: MailRelay::Base
- Inherits:
-
Object
- Object
- MailRelay::Base
- 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
-
.receiver_header ⇒ Object
Define a header that contains the original receiver address.
-
.retrieve_count ⇒ Object
Number of emails to retrieve in one batch.
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Class Method Summary collapse
-
.relay_current ⇒ Object
Retrieve, process and delete all mails from the mail server.
Instance Method Summary collapse
-
#envelope_receiver_name ⇒ Object
The receiver account that originally got this email.
-
#initialize(message) ⇒ Base
constructor
A new instance of Base.
-
#receiver_from_received_header ⇒ Object
Heuristic method to find actual receiver of the message.
-
#receiver_from_x_header ⇒ Object
Try to read the envelope receiver from the given x header.
-
#receivers ⇒ Object
List of receiver email addresses for the resent email.
-
#reject_not_allowed ⇒ Object
If the email sender was not allowed to post messages, this method is called.
-
#reject_not_existing ⇒ Object
If the email is sent to an address that is not a valid relay, this method is called.
-
#relay ⇒ Object
Process the given email.
-
#relay_address? ⇒ Boolean
Is the mail sent to a valid relay address?.
-
#resend_to(destinations) ⇒ Object
Send the same mail as is to all receivers, if any.
-
#sender_allowed? ⇒ Boolean
Is the mail sender allowed to post to this address.
-
#sender_email ⇒ Object
The email address of the sender.
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 = end |
Class Attribute Details
.receiver_header ⇒ Object
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_count ⇒ Object
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
#message ⇒ Object (readonly)
Returns the value of attribute message.
39 40 41 |
# File 'lib/mail_relay/base.rb', line 39 def @message end |
Class Method Details
.relay_current ⇒ Object
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 || begin new().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_name ⇒ Object
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#{.header}") end |
#receiver_from_received_header ⇒ Object
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 = .received received = received.first if received.respond_to?(:first) received.info[/ for .*?([^\s<>]+)@[^\s<>]+/, 1] end end |
#receiver_from_x_header ⇒ Object
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 = .header[self.class.receiver_header] field.to_s.split('@', 2).first end end |
#receivers ⇒ Object
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_allowed ⇒ Object
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_existing ⇒ Object
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 |
#relay ⇒ Object
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?
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 .destinations = destinations .header['Precedence'] = 'list' deliver() end end |
#sender_allowed? ⇒ Boolean
Is the mail sender allowed to post to this address
117 118 119 |
# File 'lib/mail_relay/base.rb', line 117 def sender_allowed? true end |
#sender_email ⇒ Object
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 ||= .from && .from.first end |