def set_charset(self, charset): if charset is None: self.del_param('charset') self._charset = None return if isinstance(charset, basestring): charset = email.charset.Charset(charset) if not isinstance(charset, email.charset.Charset): raise TypeError(charset) self._charset = charset if 'MIME-Version' not in self: self.add_header('MIME-Version', '1.0') if 'Content-Type' not in self: self.add_header('Content-Type', 'text/plain', charset=charset.get_output_charset()) else: self.set_param('charset', charset.get_output_charset()) if isinstance(self._payload, unicode): self._payload = self._payload.encode(charset.output_charset) if str(charset) != charset.get_output_charset(): self._payload = charset.body_encode(self._payload) if 'Content-Transfer-Encoding' not in self: cte = charset.get_body_encoding() try: cte(self) except TypeError: self._payload = charset.body_encode(self._payload) self.add_header('Content-Transfer-Encoding', cte)
def set_charset(self, charset): """Set the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and encoded properly, if needed, when generating the plain text representation of the message. MIME headers (MIME-Version, Content-Type, Content-Transfer-Encoding) will be added as needed. """ if charset is None: self.del_param('charset') self._charset = None return else: if isinstance(charset, basestring): charset = email.charset.Charset(charset) if not isinstance(charset, email.charset.Charset): raise TypeError(charset) self._charset = charset if 'MIME-Version' not in self: self.add_header('MIME-Version', '1.0') if 'Content-Type' not in self: self.add_header('Content-Type', 'text/plain', charset=charset.get_output_charset()) else: self.set_param('charset', charset.get_output_charset()) if isinstance(self._payload, unicode): self._payload = self._payload.encode(charset.output_charset) if str(charset) != charset.get_output_charset(): self._payload = charset.body_encode(self._payload) if 'Content-Transfer-Encoding' not in self: cte = charset.get_body_encoding() try: cte(self) except TypeError: self._payload = charset.body_encode(self._payload) self.add_header('Content-Transfer-Encoding', cte) return
def set_charset(self, charset): """Set the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and encoded properly, if needed, when generating the plain text representation of the message. MIME headers (MIME-Version, Content-Type, Content-Transfer-Encoding) will be added as needed. """ if charset is None: self.del_param("charset") self._charset = None return if isinstance(charset, basestring): charset = email.charset.Charset(charset) if not isinstance(charset, email.charset.Charset): raise TypeError(charset) # BAW: should we accept strings that can serve as arguments to the # Charset constructor? self._charset = charset if "MIME-Version" not in self: self.add_header("MIME-Version", "1.0") if "Content-Type" not in self: self.add_header("Content-Type", "text/plain", charset=charset.get_output_charset()) else: self.set_param("charset", charset.get_output_charset()) if isinstance(self._payload, unicode): self._payload = self._payload.encode(charset.output_charset) if str(charset) != charset.get_output_charset(): self._payload = charset.body_encode(self._payload) if "Content-Transfer-Encoding" not in self: cte = charset.get_body_encoding() try: cte(self) except TypeError: self._payload = charset.body_encode(self._payload) self.add_header("Content-Transfer-Encoding", cte)
def main(projects_xml='projects.xml', proj_map_json='proj-map.json', devs_json='devs.json', master_aliases='master.aliases', proj_reports='proj-reports'): projs_x = lxml.etree.parse(projects_xml) with open(devs_json) as devs_f: devs = json.load(devs_f) with open(proj_map_json) as proj_map_f: proj_map = json.load(proj_map_f) aliases = {} with open(master_aliases) as master_aliases_f: for l in master_aliases_f: k, v = l.split(':') gentooize = lambda u: ('*****@*****.**' % u if '@' not in u and not u.startswith('/') else u) aliases[k.strip().lower()] = [ gentooize(u.strip().lower()) for u in v.split(',') ] # the default charset is insane charset = email.charset.Charset('utf-8') charset.header_encoding = email.charset.QP charset.body_encoding = email.charset.QP # find out who I am pwent = pwd.getpwuid(os.getuid()) email_from = pwent[0] + '@gentoo.org' fullname = pwent[4] signature = ''' -- Yours sincerely, {}'''.format(fullname) os.makedirs(proj_reports, exist_ok=True) for px in projs_x.getroot(): p = Project(px, proj_map, devs, aliases) msg = email.message.Message() msg.set_charset(charset) msg.set_payload( charset.body_encode(format_body_for_project(p) + signature)) msg['From'] = email.utils.formataddr((fullname, email_from)) msg['To'] = email.utils.formataddr((p.name, p.email)) msg['BCC'] = email.utils.formataddr((fullname, email_from)) msg['Subject'] = format_subject_for_project(p) msg['Date'] = email.utils.formatdate() msg['Message-Id'] = email.utils.make_msgid() with open(os.path.join(proj_reports, p.email.split('@')[0]), 'wb') as f: f.write(bytes(msg))
def main(data_file, template_file): conf = configparser.ConfigParser() conf.read(['qatools.conf']) try: fullname = conf['user']['fullname'] except KeyError: u = pwd.getpwuid(os.getuid()) gecos = u.pw_gecos.split(',') fullname = gecos[0] with open(template_file, 'r') as f: template = f.read() print('* Fetching repositories.xml ...') with urllib_req.urlopen( 'https://api.gentoo.org/overlays/repositories.xml') as f: repos_xml = ET.parse(f) print('* Connecting to SMTP ...') try: smtp_config = conf['smtp'] except KeyError: smtp_config = {} if smtp_config.get('ssl'): smtp_class = smtplib.SMTP_SSL else: smtp_class = smtplib.SMTP with smtp_class(smtp_config.get('host', '')) as smtp: if smtp_config.get('tls'): smtp.starttls() smtp.ehlo() if smtp_config.get('username') and smtp_config.get('password'): smtp.login(smtp_config['username'], smtp_config['password']) try: mail_from = conf['user']['email'] except KeyError: if smtp_config.get('username'): mail_from = smtp_config.get('username') else: mail_from = pwd.getpwuid(os.getuid()).pw_name if smtp_config.get('host'): mail_from += '@' + smtp_config['host'] print('* Processing input ...') with open(data_file, 'r') as f: for l in f: l = shlex.split(l) if l: mail_addr = l[0] print('%s' % mail_addr) owners = repos_xml.findall('./repo/owner[email="%s"]' % mail_addr) if not owners: print('!! owner matching %s does not exist!' % mail_addr) continue mail_to = [] for o in owners: mail = o.find('email') assert (mail is not None) assert (mail.text == mail_addr) name = o.find('name') mail_to.append( (name.text if name is not None else None, mail.text)) break assert (mail_to) mail_to.append(('Gentoo Overlays', '*****@*****.**')) mail_body = template for i, lf in enumerate(l): tag = '${%d}' % (i + 1) mail_body = mail_body.replace(tag, lf) mail_body = mail_body.replace('${fullname}', fullname) mail_subj, mail_body = mail_body.split('\n\n', 1) mail_body = '\n'.join( textwrap.fill(x, 72) for x in mail_body.split('\n')) # get some sanity into Python... charset = email.charset.Charset('utf-8') charset.header_encoding = email.charset.QP charset.body_encoding = email.charset.QP msg = email.message.Message() msg.set_charset(charset) msg.set_payload(charset.body_encode(mail_body)) msg['From'] = email.utils.formataddr( ('%s (on behalf of Gentoo Overlays Project)' % fullname, mail_from)) msg['To'] = email.utils.formataddr(mail_to[0]) if len(mail_to) > 1: msg['CC'] = ', '.join( email.utils.formataddr(x) for x in mail_to[1:]) msg['Subject'] = mail_subj msg['Date'] = email.utils.formatdate() msg['Message-Id'] = email.utils.make_msgid() smtp.sendmail(mail_from, [x[1] for x in mail_to], msg.as_string()) print('Done.')