aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mail_handler/backends/mail_backend.rb
blob: b86f84cef6fbfac33d3cbf2c5cdc84e1b8bad040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
86
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
require 'mail'

module MailHandler
    module Backends
        module MailBackend

            def backend()
                'Mail'
            end

            # Note that the decode flag is not yet used
            def mail_from_raw_email(data, decode=true)
                Mail.new(data)
            end

             # Extracts all attachments from the given TNEF file as a Mail object
            def mail_from_tnef(content)
                main = Mail.new
                tnef_attachments(content).each do |attachment|
                    main.add_file(attachment)
                end
                main.ready_to_send!
                main
            end

            # Return a copy of the file name for the mail part
            def get_part_file_name(mail_part)
                part_file_name = mail_part.filename
                part_file_name.nil? ? nil : part_file_name.dup
            end

            # Get the body of a mail part
            def get_part_body(mail_part)
                mail_part.body.decoded
            end

            # Return the first from field if any
            def first_from(mail)
                if mail[:from]
                    begin
                        mail[:from].addrs[0]
                        mail[:from].decoded
                        return mail[:from].addrs[0]
                    rescue
                        return mail[:from].value
                    end
                else
                    nil
                end
            end

            # Return the first from address if any
            def get_from_address(mail)
                first_from = first_from(mail)
                if first_from
                    if first_from.is_a?(String)
                        return nil
                    else
                        return first_from.address
                    end
                else
                    return nil
                end
            end

            # Return the first from name if any
            def get_from_name(mail)
                first_from = first_from(mail)
                if first_from
                    if first_from.is_a?(String)
                        return nil
                    else
                        return first_from.name
                    end
                else
                    return nil
                end
            end

            def get_all_addresses(mail)
                envelope_to = mail['envelope-to'] ? [mail['envelope-to'].value] : []
                ((mail.to || []) +
                (mail.cc || []) +
                (envelope_to || [])).uniq
            end

            def empty_return_path?(mail)
                return false if mail['return-path'].nil?
                return true if mail['return-path'].value.blank?
                return false
            end

            def get_auto_submitted(mail)
                mail['auto-submitted'] ? mail['auto-submitted'].value : nil
            end

            # Format
            def address_from_name_and_email(name, email)
                if !MySociety::Validate.is_valid_email(email)
                    raise "invalid email " + email + " passed to address_from_name_and_email"
                end
                if name.nil?
                    return Mail::Address.new(email)
                end
                address = Mail::Address.new
                address.display_name = name
                address.address = email
                address.to_s
            end

            def address_from_string(string)
                Mail::Address.new(string).address
            end
        end
    end
end