Module: YDIM::Mail

Defined in:
lib/ydim/mail.rb

Class Method Summary collapse

Class Method Details

.body(config, debitor, invoice) ⇒ Object



10
11
12
13
# File 'lib/ydim/mail.rb', line 10

def Mail.body(config, debitor, invoice)
	salutation = config.salutation[debitor.salutation.to_s]
	sprintf(config.mail_body, salutation, debitor.contact, invoice.description)
end

.encode_subject(config, subject) ⇒ Object



90
91
92
93
# File 'lib/ydim/mail.rb', line 90

def Mail.encode_subject(config, subject)
  encoded = [subject].pack('M').gsub("=\n", '').gsub(' ', '=20')
  sprintf("=?%s?q?%s?=", config.mail_charset, encoded)
end

.send_invoice(config, invoice, sort_args = {}) ⇒ Object



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
# File 'lib/ydim/mail.rb', line 14

def Mail.send_invoice(config, invoice, sort_args={})
	debitor = invoice.debitor
	to = debitor.email
	subject = sprintf('Rechnung %s #%i, %s', debitor.name,
					invoice.unique_id, invoice.description)
	invoice_name = sprintf("%s.pdf", subject.tr(' /', '_-'))
	mpart = RMail::Message.new
	header = mpart.header
	header.to = to
    cc = header.cc = debitor.emails_cc
	header.from = config.mail_from
    header.subject = encode_subject config, subject
    header.date = Time.now
	tpart = RMail::Message.new
	mpart.add_part(tpart)
	tpart.header.add('Content-Type', 'text/plain', nil, 
                     'charset' => config.mail_charset)
	tpart.body = body(config, debitor, invoice)
	fpart = RMail::Message.new
	mpart.add_part(fpart)
	header = fpart.header
	header.add('Content-Type', 'application/pdf')
	header.add('Content-Disposition', 'attachment', nil,
		{'filename' => invoice_name })
	header.add('Content-Transfer-Encoding', 'base64')
	fpart.body = [invoice.to_pdf(sort_args)].pack('m')
	recipients = config.mail_recipients.dup.push(to).concat(cc).uniq
	Net::SMTP.start(config.smtp_server, config.smtp_port,
                    config.smtp_domain, config.smtp_user, config.smtp_pass,
                    config.smtp_authtype) { |smtp|
		recipients.each { |recipient|
			smtp.sendmail(mpart.to_s, config.smtp_user, recipient)
		}
	}
	recipients
  rescue Timeout::Error
    retries ||= 3
    if retries > 0
      sleep 3 - retries
      retries -= 1
      retry
    else
      raise
    end
end

.send_reminder(config, autoinvoice) ⇒ Object



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
# File 'lib/ydim/mail.rb', line 59

def Mail.send_reminder(config, autoinvoice)
    subject = autoinvoice.reminder_subject.to_s.strip
    subject.gsub! %r{<year>\s*}, ''
    subject.gsub! %r{\s*</year>}, ''
    body = autoinvoice.reminder_body.to_s.strip
    body.gsub! %r{<invoice>\s*}, ''
    body.gsub! %r{\s*</invoice>}, ''
    unless(subject.empty? || body.empty?)
      debitor = autoinvoice.debitor
      to = debitor.email
      mpart = RMail::Message.new
      header = mpart.header
      header.to = to
      cc = header.cc = debitor.emails_cc
      header.from = config.mail_from
      header.subject = encode_subject config, subject
      header.date = Time.now
      header.add('Content-Type', 'text/plain', nil, 
                 'charset' => config.mail_charset)
      mpart.body = body
      recipients = config.mail_recipients.dup.push(to).concat(cc).uniq
      Net::SMTP.start(config.smtp_server, config.smtp_port,
                      config.smtp_domain, config.smtp_user, config.smtp_pass,
                      config.smtp_authtype) { |smtp|
        recipients.each { |recipient|
          smtp.sendmail(mpart.to_s, config.smtp_user, recipient)
        }
      }
      recipients
    end
end