示例#1
0
 def __init__(self, team):
     MIMEMultipart.__init__(self, "alternative")
     self.team = team
     self.initHeaders()
     self.initText()
     self.initHTML()
     self.join()
示例#2
0
文件: mail.py 项目: zogzog/cubicweb
 def __init__(self,
              subject,
              textcontent,
              htmlcontent,
              sendermail=None,
              sendername=None,
              recipients=None,
              ccrecipients=None):
     MIMEMultipart.__init__(self, 'related')
     self['Subject'] = header(subject)
     self.preamble = 'This is a multi-part message in MIME format.'
     # Attach alternative text message
     alternative = MIMEMultipart('alternative')
     self.attach(alternative)
     msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8')
     alternative.attach(msgtext)
     # Attach html message
     msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8')
     alternative.attach(msghtml)
     if sendermail or sendername:
         self['From'] = addrheader(sendermail, sendername)
     if recipients:
         self['To'] = ', '.join(
             addrheader(addr) for addr in recipients if addr is not None)
     if ccrecipients:
         self['Cc'] = ', '.join(
             addrheader(addr) for addr in ccrecipients if addr is not None)
示例#3
0
    def __init__(self,
                 message,
                 attachments,
                 gpgbinary='gpg',
                 gnupghome=None,
                 use_agent=True,
                 keyring=None,
                 keyid=None):

        # Hack around the fact that the Pyton 2.x MIMEMultipart is not
        # a new-style class.
        try:
            unicode  # Doesn't exist in Python 3
            MIMEMultipart.__init__(self, 'signed')
        except NameError:
            super(SignedMessage, self).__init__('signed')

        payload = self.__payload(message, attachments)
        signature = self.__signature(payload, gpgbinary, gnupghome, use_agent,
                                     keyring, keyid)

        self.set_param('protocol', 'application/pgp-signature')
        self.set_param('micalg', 'pgp-sha512')  ####!!! GET THIS FROM KEY!
        self.preamble = 'This is an OpenPGP/MIME signed message.'
        self.attach(payload)
        self.attach(signature)
示例#4
0
    def __init__(self,
                 _data=None,
                 _subtype='encrypted',
                 boundary=None,
                 **_params):
        """Creates a multipart/encrypted type message.

        By default, creates a multipart/encrypted message, with proper
        Content-Type and MIME-Version headers.

        _subtype is the subtype of the multipart content type, defaulting to
        `encrypted'.

        boundary is the multipart boundary string.  By default it is
        calculated as needed.

        _data is a string containing the raw payload data (encrypted).

        Additional parameters for the Content-Type header are taken from the
        keyword arguments (or passed into the _params argument).

        It will create the Email structure:
        └┬╴multipart/encrypted
         ├─╴application/pgp-encrypted
         └─╴application/octet-stream inline [encrypted.asc]
         """
        _params['protocol'] = "application/pgp-encrypted"
        description = MIMEApplicationPGPDescription()
        payload = MIMEApplicationPGPPayload(_data)
        _subparts = [description, payload]
        MIMEMultipart.__init__(self,
                               _subtype=_subtype,
                               boundary=boundary,
                               _subparts=_subparts,
                               **_params)
示例#5
0
    def __init__(self, _data=None, _subtype='mixed', boundary=None, **_params):
        """Creates a multipart/mixed type message containing
        application/autocrypt-setup.

        By default, creates a multipart/mixed message, with proper
        Content-Type and MIME-Version headers.

        _subtype is the subtype of the multipart content type, defaulting to
        `mixed'.

        boundary is the multipart boundary string.  By default it is
        calculated as needed.

        _data is a string containing the raw payload data (encrypted).

        Additional parameters for the Content-Type header are taken from the
        keyword arguments (or passed into the _params argument).

        It will create the Email structure:
        └┬╴multipart/mixed
         ├─╴text/plain
         └─╴application/autocrypt-setup attachment
            [autocrypt-setup-message.html]

         """
        # _params['protocol'] = "?"
        description = MIMETextACSetupDescription()
        payload = MIMEApplicationACSetupPayload(_data)
        payload.add_header("Content-Disposition",
                           'attachment',
                           filename=AC_CT_SETUP_FN)
        _subparts = [description, payload]
        # policy is only introduced in Python3.6 (policy=policy)
        MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
示例#6
0
 def __init__(self):
     MIMEMultipart.__init__(self)
     self['To'] = ''
     self.text = ''
     self.html = ''
     self.recipients = []
     self.attachments = []
示例#7
0
    def __init__(self, fingerprint=None, **kwargs):

        MIMEMultipart.__init__(self, **kwargs)
        self.fingerprint = fingerprint
        self.crypto = getgpg()
        self['From'] = app.config['SMTP_FROM']
        self['Date'] = formatdate() 
示例#8
0
 def __init__(self, to_addr, subj, cc_addr=None):
     MIMEMultipart.__init__(self)
     self['From'] = '"' + smtp_from_name + '" <' + smtp_from_addr + '>'
     self['To'] = to_addr
     if not cc_addr == None:
         self['Cc'] = cc_addr
     self['Date'] = formatdate(localtime=True)
     self['Subject'] = subj
示例#9
0
 def __init__(self,
              _subtype='mixed',
              boundary=None,
              _subparts=None,
              encoding=None,
              **_params):
     self.encoding = encoding
     MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
示例#10
0
 def __init__(self, _subtype='mixed', boundary=None, _subparts=None, encoding=None, **_params):
     self.encoding = encoding
     MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
     try:
         import email.policy
         # https://docs.python.org/3/library/email.policy.html
         self.policy = email.policy.default
     except ImportError:
         pass
示例#11
0
 def __init__(self, from_address, to_address, subject, body):
     MIMEMultipart.__init__(self, 'encrypted',
                            boundary=_boundary,
                            protocol='application/pgp-encrypted')
     self.from_address = self['From'] = from_address
     self.to_address = self['To'] = to_address
     self['Subject'] = subject
     self.attach(MIMEApplication('Version: 1', 'pgp-encrypted', encode_7or8bit))
     encrypted = MIMEApplication(body, 'octet-stream; name="encrypted.asc"',
                                 encode_noop)
     encrypted['Content-Description'] = 'OpenPGP encrypted message'
     encrypted['Content-Disposition'] = 'inline; filename="encrypted.asc"'
     self.attach(encrypted)
示例#12
0
 def __init__(self,
              _subtype='mixed',
              boundary=None,
              _subparts=None,
              encoding=None,
              **_params):
     self.encoding = encoding
     MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
     try:
         import email.policy
         # https://docs.python.org/3/library/email.policy.html
         self.policy = email.policy.default
     except ImportError:
         pass
示例#13
0
 def __init__(self, from_address, to_address, subject, body, signature):
     MIMEMultipart.__init__(self, 'signed',
                            micalg='pgp-sha1',
                            boundary=_boundary,
                            protocol='application/pgp-signature')
     self.from_address = self['From'] = from_address
     self.to_address = self['To'] = to_address
     self['Subject'] = subject
     signed_data = EmptyHeaderMessage(body)
     self.attach(signed_data)
     signature = MIMEApplication(signature, 'pgp-signature; name="signature.asc"',
                                 encode_7or8bit)
     signature['Content-Description'] = 'OpenPGP digital signature'
     signature['Content-Disposition'] = 'attachment; filename="signature.asc"'
     self.attach(signature)
示例#14
0
    def from_parts(cls, msg, sig):
        """Assemble a PGP/MIME signed message from its two parts: the message and the signature,
        both of which already properly encoded.

        :param msg: a MIME encoded message :param sig: a PGP/MIME signature

        """
        self = PGPMIMEsigned()
        MIMEMultipart.__init__(self,
                               'signed',
                               micalg='pgp-sha1',
                               protocol='application/pgp-signature')
        self.attach(msg)
        self.attach(sig)
        return self
示例#15
0
    def __init__(self, protocol, micalg, boundary=None, _subparts=None):
        """
        Initialize the multipart/signed message.

        :param boundary: the multipart boundary string. By default it is
            calculated as needed.
        :type boundary: str
        :param _subparts: a sequence of initial subparts for the payload. It
            must be an iterable object, such as a list. You can always
            attach new subparts to the message by using the attach() method.
        :type _subparts: iterable
        """
        MIMEMultipart.__init__(
            self, _subtype='signed', boundary=boundary,
            _subparts=_subparts)
        self.set_param('protocol', protocol)
        self.set_param('micalg', micalg)
示例#16
0
 def __init__(self, protocol, boundary=None, _subparts=None):
     """
     :param protocol: The encryption protocol to be added as a parameter to
         the Content-Type header.
     :type protocol: str
     :param boundary: the multipart boundary string. By default it is
         calculated as needed.
     :type boundary: str
     :param _subparts: a sequence of initial subparts for the payload. It
         must be an iterable object, such as a list. You can always
         attach new subparts to the message by using the attach() method.
     :type _subparts: iterable
     """
     MIMEMultipart.__init__(
         self, _subtype='encrypted', boundary=boundary,
         _subparts=_subparts)
     self.set_param('protocol', protocol)
示例#17
0
 def __init__(self, protocol, boundary=None, _subparts=None):
     """
     :param protocol: The encryption protocol to be added as a parameter to
         the Content-Type header.
     :type protocol: str
     :param boundary: the multipart boundary string. By default it is
         calculated as needed.
     :type boundary: str
     :param _subparts: a sequence of initial subparts for the payload. It
         must be an iterable object, such as a list. You can always
         attach new subparts to the message by using the attach() method.
     :type _subparts: iterable
     """
     MIMEMultipart.__init__(
         self, _subtype='encrypted', boundary=boundary,
         _subparts=_subparts)
     self.set_param('protocol', protocol)
示例#18
0
    def __init__(self, protocol, micalg, boundary=None, _subparts=None):
        """
        Initialize the multipart/signed message.

        :param boundary: the multipart boundary string. By default it is
            calculated as needed.
        :type boundary: str
        :param _subparts: a sequence of initial subparts for the payload. It
            must be an iterable object, such as a list. You can always
            attach new subparts to the message by using the attach() method.
        :type _subparts: iterable
        """
        MIMEMultipart.__init__(
            self, _subtype='signed', boundary=boundary,
            _subparts=_subparts)
        self.set_param('protocol', protocol)
        self.set_param('micalg', micalg)
示例#19
0
    def __init__(self, to_addrs, subject=None, text_body=None, html_body=None, from_addr=DefaultFrom, cc_addrs=DefaultCc):
        """Construct an instance.

        Parameters
        ----------
        to_addrs : array of str
            An array of string objects, each of which is an email
            address to which this message should be sent.

        subject : str
            The subject of this email message.

        text_body : str
            The plain-text version of the email body.

        html_body : str
            The HTML version of the email body.

        from_addr : str
            The email address from which this message is to be sent.

        cc_addrs : array of str
            An array of string objects, each of which is a CC email
            address to which this message should be sent.
        """
        MIMEMultipart.__init__(self, 'mixed')

        self['From'] = from_addr
        logging.debug('Message to be sent from: %s', self['From'])

        self['To'] = ','.join(to_addrs)
        logging.debug('Message to be sent to: %s', self['To'])

        if cc_addrs:
            self['CC'] = ','.join(cc_addrs)
            logging.debug('Message to be sent as CC to: %s', self['CC'])

        if subject:
            self['Subject'] = subject
            logging.debug('Message subject: %s', subject)

        if html_body or text_body:
            self.attach_text_and_html_bodies(html_body, text_body)
示例#20
0
 def __init__(self, tweet, to):
     MIMEMultipart.__init__(self, 'alternative', _charset='utf-8')
     
     tweet['html_text'] = Parser().parse(tweet['text']).html
     
     self['Subject'] = Header(tweet['text'].encode('utf-8'), 'utf-8')
     f = Header(tweet['user']['name'].encode('utf-8'), 'utf-8')
     f.append('<{0}@{1}>'.format(tweet['user']['screen_name'], domain), 'ascii')
     self['From'] = f
     self['To'] = to+'@'+domain
     self['Date'] = tweet['created_at']
     self['Message-ID'] = "<{0}@{1}>".format(tweet['id'], domain)
     
     self.attach(MIMEText(tweet['text'].encode('utf-8'), 'plain', _charset='utf-8'))
     
     with codecs.open('mail.html', 'r', 'utf-8') as f:
         template = f.read()
         template = template.format(**tweet)
         template = template.encode('utf-8')
         self.attach(MIMEText(template, 'html', _charset='utf-8'))
示例#21
0
    def __init__(self, message, attachments,
                 gpgbinary='gpg', gnupghome=None, use_agent=True,
                 keyring=None, keyid=None):

        # Hack around the fact that the Pyton 2.x MIMEMultipart is not
        # a new-style class.
        try:
            unicode                       # Doesn't exist in Python 3
            MIMEMultipart.__init__(self, 'signed')
        except NameError:
            super(SignedMessage, self).__init__('signed')

        payload = self.__payload(message, attachments)
        signature = self.__signature(
            payload, gpgbinary, gnupghome, use_agent, keyring, keyid)

        self.set_param('protocol', 'application/pgp-signature')
        self.set_param('micalg', 'pgp-sha512')   ####!!! GET THIS FROM KEY!
        self.preamble = 'This is an OpenPGP/MIME signed message.'
        self.attach(payload)
        self.attach(signature)
示例#22
0
    def __init__(
            self, report_type="message/delivery-status", boundary=None,
            _subparts=None):
        """
        Initialize the message.

        As per RFC 6522, boundary and report_type are required parameters.

        :param report_type: The type of report. This is set as a
                            "Content-Type" parameter, and should match the
                            MIME subtype of the second body part.
        :type report_type: str

        """
        MIMEMultipart.__init__(
            self,
            # set mime type to "multipart/report"
            _subtype="report",
            boundary=boundary,
            _subparts=_subparts,
            # add "report-type" as a "Content-Type" parameter
            report_type=report_type)
        self._report_type = report_type
示例#23
0
    def __init__(self, msg=None, signer=None):
        if msg is None:
            return
        if msg.is_multipart():
            # we need these to get our message correctly parsed by KMail and Thunderbird
            msg.preamble = 'This is a multi-part message in MIME format.'
            msg.epilogue = ''

        if signer is not None:
            msg_str = flatten(msg)
            sig = session.gnupg.msg_sign(msg_str, signer.kid)
            sig = MIMEApplication(
                _data=sig,
                _subtype='pgp-signature; name="signature.asc"',
                _encoder=encode_7or8bit)
            sig['Content-Description'] = 'This is a digital signature.'
            sig.set_charset('us-ascii')

            MIMEMultipart.__init__(self,
                                   'signed',
                                   micalg='pgp-sha1',
                                   protocol='application/pgp-signature')
            self.attach(msg)
            self.attach(sig)
示例#24
0
    def __init__(self, msg, recipients):
        MIMEMultipart.__init__(self,
                               'encrypted',
                               micalg='pgp-sha1',
                               protocol='application/pgp-encrypted')

        body = flatten(msg)
        encrypted = session.gnupg.msg_encrypt(body,
                                              [r.kid for r in recipients])

        payload = MIMEApplication(_data=encrypted,
                                  _subtype='octet-stream',
                                  _encoder=encode_7or8bit)
        payload['Content-Disposition'] = 'inline; name="encrypted.asc"'
        payload.set_charset('us-ascii')

        control = MIMEApplication(_data='Version: 1\n',
                                  _subtype='pgp-encrypted',
                                  _encoder=encode_7or8bit)
        control.set_charset('us-ascii')

        self.attach(control)
        self.attach(payload)
        self['Content-Disposition'] = 'attachment'
示例#25
0
    def __init__(self,
                 report_type="message/delivery-status",
                 boundary=None,
                 _subparts=None):
        """
        Initialize the message.

        As per RFC 6522, boundary and report_type are required parameters.

        :param report_type: The type of report. This is set as a
                            "Content-Type" parameter, and should match the
                            MIME subtype of the second body part.
        :type report_type: str

        """
        MIMEMultipart.__init__(
            self,
            # set mime type to "multipart/report"
            _subtype="report",
            boundary=boundary,
            _subparts=_subparts,
            # add "report-type" as a "Content-Type" parameter
            report_type=report_type)
        self._report_type = report_type
示例#26
0
from __future__ import unicode_literals
示例#27
0
 def __init__(self, _subtype='mixed', boundary=None, _subparts=None, encoding=None, **_params):
     self.encoding = encoding
     MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
示例#28
0
 def __init__(self):
     MIMEMultipart.__init__(self, _subtype='form-data')
    def __init__(self,
                 to_addrs,
                 cyhy_pdf_filename,
                 agency_acronym,
                 financial_year,
                 fy_quarter,
                 tmail_pdf_filename=None,
                 https_pdf_filename=None,
                 from_addr=DefaultFrom,
                 cc_addrs=DefaultCc):
        """Construct an instance.

        Parameters
        ----------
        to_addrs : array of str
            An array of string objects, each of which is an email
            address to which this message should be sent.

        cyhy_pdf_filename : str
            The filename of the PDF file that is the CYHY report
            corresponding to this message.

        agency_acronym : str
            The acronym ised by the agency corresponding to the CYHY
            report attachment.

        financial_year : str
            The two-digit financial year to corresponding to the CYHY
            report attachment.

        fy_quarter : str
            The quarter of the financial year corresponding to the
            CYHY report attachment.

        tmail_pdf_filename : str
            The filename of the PDF file that is the trustymail report
            corresponding to this message.

        https_pdf_filename : str
            The filename of the PDF file that is the https-scan report
            corresponding to this message.

        from_addr : str
            The email address from which this message is to be sent.

        cc_addrs : array of str
            An array of string objects, each of which is a CC email
            address to which this message should be sent.
        """
        MIMEMultipart.__init__(self)

        self['From'] = from_addr
        logging.debug('Message to be sent from: %s', self['From'])

        self['To'] = ','.join(to_addrs)
        logging.debug('Message to be sent to: %s', self['To'])

        if cc_addrs:
            self['CC'] = ','.join(cc_addrs)
            logging.debug('Message to be sent as CC to: %s', self['CC'])

        # This is the data mustache will use to render the templates
        mustache_data = {
            'acronym': agency_acronym,
            'financial_year': financial_year,
            'quarter': fy_quarter
        }
        self['Subject'] = pystache.render(CyhyMessage.Subject, mustache_data)
        logging.debug('Message subject: %s', self['Subject'])

        text_body = pystache.render(CyhyMessage.TextBody, mustache_data)
        self.attach(MIMEText(text_body, 'plain'))
        logging.debug('Message plain-text body: %s', text_body)

        html_body = pystache.render(CyhyMessage.HtmlBody, mustache_data)
        html_part = MIMEText(html_body, 'html')
        html_part.add_header('Content-Disposition', 'inline')
        self.attach(html_part)
        logging.debug('Message HTML body: %s', html_body)

        self.attach_pdf(cyhy_pdf_filename)
        logging.debug('Message PDF CYHY attachment: %s', cyhy_pdf_filename)

        if tmail_pdf_filename:
            self.attach_pdf(tmail_pdf_filename)
            logging.debug('Message PDF trustymail attachment: %s',
                          tmail_pdf_filename)

        if https_pdf_filename:
            self.attach_pdf(https_pdf_filename)
            logging.debug('Message PDF https-scan attachment: %s',
                          https_pdf_filename)
示例#30
0
    def __init__(self, subtype, **headers):
        MIMEMultipart.__init__(self, subtype)

        for header in headers:
            dest = header.lstrip('_').replace('_','-')
            self[dest] = headers[header]
示例#31
0
	def __init__(self):
		MIMEMultipart.__init__(self, _subtype='form-data')