summaryrefslogtreecommitdiffstats
path: root/scripts/send_rawhide_report
blob: 19fe8dccb4fbec8c54675e0222d150c5a1846893 (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
#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-

# Copyright © 2008 Jeffrey C. Ollie

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

import sys
import smtplib

from optparse import OptionParser
from email.charset import Charset, QP
from email.message import Message
from email import utils
from email.header import Header
from email.generator import Generator
from cStringIO import StringIO

default_recipients = ["Fedora Development List <fedora-devel-list@redhat.com>",
                      "Fedora Test List <fedora-test-list@redhat.com>"],

default_sender = "Rawhide Report <rawhide@fedoraproject.org>"

utf8 = Charset('utf-8')
utf8.body_encoding = QP
utf8.header_encoding = QP

parser = OptionParser()

parser.add_option("--subject",
                  action = "store",
                  dest = "subject",
                  help = "subject for email")

parser.add_option("--from",
                  action = "store",
                  dest = "sender",
                  default = default_sender,
                  help = "report will be from EMAIL",
                  metavar = "EMAIL")

parser.add_option("--to",
                  action = "append",
                  dest = "recipients",
                  default = [],
                  help="add EMAIL to list of recipients",
                  metavar="EMAIL")

(options, args) = parser.parse_args()

if options.subject is None:
    sys.stderr.write('Must specify subject!\n')
    sys.exit(1)

if len(options.recipients) == 0:
    options.recipients = default_recipients

subject = options.subject.decode('utf-8')

sender_realname, sender_email = utils.parseaddr(options.sender.decode('utf-8'))

sender = utils.formataddr((sender_realname, sender_email))

recipients_email_only = []
recipients = []

for recipient in options.recipients:
    recipient_realname, recipient_email = utils.parseaddr(recipient.decode('utf-8'))
    recipients_email_only.append(recipient_email)
    recipients.append(utils.formataddr((recipient_realname, recipient_email)))

body_parts = []

for arg in args:
    body_parts.append(file(arg, 'r').read().decode('utf-8', 'replace').encode('utf-8').encode('quoted-printable'))
    
body = ''.join(body_parts)

msg = Message()
msg.set_charset(utf8)
msg.set_payload(body)

msg['From'] = Header(sender, utf8)
msg['To'] = Header(u', '.join(recipients), utf8)
msg['Subject'] = Header(subject, utf8)
msg['Date'] = Header(unicode(utils.formatdate()), utf8)

msg_fp = StringIO()
msg_g = Generator(msg_fp, False)
msg_g.flatten(msg)

smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail(sender_email, recipients_email_only, msg_fp.getvalue())
smtp.close()