Module: Mail::CheckDeliveryParams

Included in:
LoggerDelivery
Defined in:
lib/mail/check_delivery_params.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.check(mail) ⇒ Object


5
6
7
8
9
# File 'lib/mail/check_delivery_params.rb', line 5

def check(mail)
  [ check_from(mail.smtp_envelope_from),
    check_to(mail.smtp_envelope_to),
    check_message(mail) ]
end

.check_addr(addr_name, addr) ⇒ Object


29
30
31
32
33
# File 'lib/mail/check_delivery_params.rb', line 29

def check_addr(addr_name, addr)
  validate_smtp_addr addr do |error_message|
    raise ArgumentError, "SMTP #{addr_name} address #{error_message}: #{addr.inspect}"
  end
end

.check_from(addr) ⇒ Object


11
12
13
14
15
16
17
# File 'lib/mail/check_delivery_params.rb', line 11

def check_from(addr)
  if Utilities.blank?(addr)
    raise ArgumentError, "SMTP From address may not be blank: #{addr.inspect}"
  end

  check_addr 'From', addr
end

.check_message(message) ⇒ Object


49
50
51
52
53
54
55
56
57
# File 'lib/mail/check_delivery_params.rb', line 49

def check_message(message)
  message = message.encoded if message.respond_to?(:encoded)

  if Utilities.blank?(message)
    raise ArgumentError, 'An encoded message is required to send an email'
  end

  message
end

.check_to(addrs) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/mail/check_delivery_params.rb', line 19

def check_to(addrs)
  if Utilities.blank?(addrs)
    raise ArgumentError, "SMTP To address may not be blank: #{addrs.inspect}"
  end

  Array(addrs).map do |addr|
    check_addr 'To', addr
  end
end

.validate_smtp_addr(addr) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mail/check_delivery_params.rb', line 35

def validate_smtp_addr(addr)
  if addr
    if addr.bytesize > 2048
      yield 'may not exceed 2kB'
    end

    if /[\r\n]/ =~ addr
      yield 'may not contain CR or LF line breaks'
    end
  end

  addr
end