예제 #1
0
파일: transaction.py 프로젝트: abec/vsmtpd
 def insert_header(self, _pos, _name, _value, **_params):
     parts = []
     for k, v in _params.items():
         if v is None:
             parts.append(k.replace("_", "-"))
         else:
             parts.append(_formatparam(k.replace("_", "-"), v))
     if _value is not None:
         parts.insert(0, _value)
     self._headers.insert(_pos, (_name, SEMISPACE.join(parts)))
예제 #2
0
    def get_mime_object(self):

        # encode the filename; if this isn't done manually ahead of time, 
        # email.Header.encode encodes the entire header as UTF-8 if the 
        # filename portion can't be ASCII, which isn't the preferred approach
        filename, charset = _encode_text(self.name)
        if charset:
            filename = Charset(charset).header_encode(self.name)
        
        # guess the content type based on the file's extension
        ctype, encoding = mimetypes.guess_type(self.name)
        if ctype is None or encoding is not None:
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)
        
        # create the appropriate object; not including the "name" portion 
        # here because the Content-Type header must remain as a string for 
        # the message flattening process and that prevents fixing the header
        if maintype == 'text':
            text, charset = _encode_text(self.get_data(binary=False))
            obj = MIMEText(text, _subtype=subtype, _charset=charset)
        elif maintype == 'image':
            obj = MIMEImage(self.get_data(), _subtype=subtype)
        elif maintype == 'audio':
            obj = MIMEAudio(self.get_data(), _subtype=subtype)
        else:
            obj = MIMEBase(maintype, subtype)
            obj.set_payload(self.get_data())
            encoders.encode_base64(obj)
        
        # add the filename; using Header directly for potentially long 
        # headers since MIMEMultipart overrides its default space folding 
        # character with a hard tab, which Outlook 2003 and earlier (and 
        # possibly other email clients) doesn't unfold back into a space
        obj['Content-Disposition'] = Header(SEMISPACE.join(['attachment', _formatparam('filename', filename)]), 
                                            header_name='Content-Disposition')
        
        # remove the MIME-Version (not needed for every attachment)
        if 'MIME-Version' in obj:
            del obj['MIME-Version']
        
        # return the object
        return obj
예제 #3
0
    def prepend_header(self, _name: str, _value: str,
                       **_params: Union[str, Sequence[str]]) -> None:
        """Extended header setting.

        Like add_header, but prepends the header. This is useful for Received:.

        Examples:

        msg.add_header('content-disposition', 'attachment', filename='bud.gif')
        msg.add_header('content-disposition', 'attachment',
                       filename=('utf-8', '', Fußballer.ppt'))
        msg.add_header('content-disposition', 'attachment',
                       filename='Fußballer.ppt'))
        """
        parts = []
        for k, v in _params.items():
            if v is None:
                parts.append(k.replace('_', '-'))
            else:
                parts.append(_formatparam(k.replace('_', '-'), v))
        if _value is not None:
            parts.insert(0, _value)
        self.prepend(_name, SEMISPACE.join(parts))