Module: BBMB::Util::Mail

Defined in:
lib/bbmb/util/mail.rb

Class Method Summary collapse

Class Method Details

.notify_confirmation_error(order) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bbmb/util/mail.rb', line 14

def Mail.notify_confirmation_error(order)
  config = BBMB.config
  if to = config.confirm_error_to
    header, message = setup
    customer = order.customer
    from = header.from = config.confirm_error_from
    header.to = to
    cc = header.cc = config.confirm_error_cc
    header.subject = config.confirm_error_subject % customer.customer_id
    message.body = config.confirm_error_body % customer.customer_id
    Mail.sendmail message, from, to, cc
  end
end

.notify_debug(subject, body) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bbmb/util/mail.rb', line 27

def Mail.notify_debug(subject, body)
  header, message = setup
  config = BBMB.config
  from = header.from = config.mail_order_from
  to = header.to = config.error_recipients
  header.subject = sprintf "%s: %s", BBMB.config.name, subject
  message.body = body
  Mail.sendmail(message, from, to)
end

.notify_error(error) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/bbmb/util/mail.rb', line 36

def Mail.notify_error(error)
  header, message = setup
  config = BBMB.config
  from = header.from = config.mail_order_from
  to = header.to = config.error_recipients
  header.subject = sprintf "%s: %s", BBMB.config.name, error.message
  message.body = [ error.class, error.message, 
    error.backtrace.pretty_inspect ].join("\n")
  Mail.sendmail(message, from, to)
end

.notify_inject_error(order, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bbmb/util/mail.rb', line 46

def Mail.notify_inject_error(order, opts={})
  config = BBMB.config
  if to = config.inject_error_to
    customer = order.customer
    header, message = setup
    from = header.from = config.inject_error_from
    header.to = to
    cc = header.cc = config.inject_error_cc
    header.subject = config.inject_error_subject % [
      order.order_id,
      opts[:customer_name] || customer.customer_id,
    ]
    message.body = config.inject_error_body % [
      opts[:customer_name] || customer.customer_id,
      order.commit_time.strftime('%d.%m.%Y %H:%M:%S'),
      customer.customer_id,
    ]
    Mail.sendmail message, from, to, cc
  end
end

.send_confirmation(order) ⇒ Object



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
# File 'lib/bbmb/util/mail.rb', line 74

def Mail.send_confirmation(order)
  config = BBMB.config
  ## there are two switches that determine whether a confirmation is sent out:
  #  application-wide: mail_confirm_reply_to must be configured
  reply_to = config.mail_confirm_reply_to or return nil
  #  per-customer: order_confirmation must be checked
  order.customer.order_confirmation or return nil
  to = order.customer.email or return notify_confirmation_error(order)
  body = config.mail_confirm_body or return nil
  date = order.commit_time.strftime("%d.%m.%Y")
  parts = []
  config.mail_confirm_lines.collect do |line|
    content = order.collect do |pos|
      sprintf line, pos.quantity, pos.description, pos.price_qty, pos.total
    end
    parts.push date, content.join("\n"), order.total
    if vtotal = order.total_incl_vat
      parts.push vtotal
    end
  end

  header, message = setup
  from = header.from = config.mail_confirm_from
  header.to = to
  header.subject = config.mail_confirm_subject % order.order_id
  header.add('Message-ID', sprintf('<%s@%s>', order.order_id,
                                   from.tr('@', '.')))
  header.add('Reply-To', reply_to)
  message.body = sprintf body, *parts

  Mail.sendmail(message, from, to, config.mail_confirm_cc)
end

.send_order(order) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bbmb/util/mail.rb', line 106

def Mail.send_order(order)
  header, message = setup
  config = BBMB.config
  from = header.from = config.mail_order_from
  to = header.to = config.mail_order_to
  cc = header.cc = config.mail_order_cc
  header.subject = config.mail_order_subject % order.order_id
  header.add('Message-ID', sprintf('<%s@%s>', order.order_id, 
                                   from.tr('@', '.')))
  message.body = order.to_target_format

  Mail.sendmail(message, from, to, cc)
end

.send_request(email, organisation, body) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bbmb/util/mail.rb', line 119

def Mail.send_request(email, organisation, body)
  header, message = setup
  config = BBMB.config
  from = header.from = config.mail_request_from
  to = header.to = config.mail_request_to
  cc = header.cc = config.mail_request_cc
  header.subject = config.mail_request_subject % organisation
  header.add('Reply-To', email)
  message.body = body
  Mail.sendmail(message, from, to, cc)
end

.sendmail(message, from, to, cc = []) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/bbmb/util/mail.rb', line 66

def Mail.sendmail(message, from, to, cc=[])
  config = BBMB.config
  Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_helo,
                  config.smtp_user, config.smtp_pass, 
                  config.smtp_authtype) { |smtp|
    smtp.sendmail(message.to_s, from, [to, cc].flatten.compact)
  }
end

.setup ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/bbmb/util/mail.rb', line 130

def Mail.setup
  message = RMail::Message.new
  header = message.header
  header.add('Date', Time.now.rfc822)
  header.add('Mime-Version', '1.0')
  header.add('User-Agent', BBMB.config.name)
  header.add('Content-Type', 'text/plain', nil, 'charset' => 'utf-8')
  header.add('Content-Disposition', 'inline')
  [header, message]
end