[Zope] = replaced with == in sendmail!!!!!

Jay, Dylan djay@lucent.com
Fri, 16 Apr 1999 13:23:42 +1000


> -----Original Message-----
> From: Paulo Eduardo Neves [mailto:neves@inf.puc-rio.br]
> Sent: Friday, 16 April 1999 12:06
> To: Jay, Dylan
> Cc: 'Michel Pelletier'; 'zope@zope.org'
> Subject: Re: [Zope] = replaced with == in sendmail!!!!!
> 
> 
> "Jay, Dylan" wrote:
> > I couldn't get it to work but I'm pretty sure it wouldn't anyway.
> > I had a look at the MailHost code and found some quoting 
> stuff that if I
> > knew more about python I might be able to do work out the 
> problem with.
> > 
> 
> I've just managed to send accend chars (iso8859-1) in an 
> email. I had to
> add some headers to the message to make it work. Adding it 
> made the mail
> client interpret correctly the email. Probably everything 
> will be ok if
> you also use them.
> 
> Here is:
> 
> <!--#sendmail mailhost="mailhost"-->
> From: Myself <me@here.com>
> To: Somebody <she@somewhere.com>
> Subject: Hi!
> Content-Type: text/plain; charset=iso-8859-1
> Content-Transfer-Encoding: quoted-printable
> 
> 1+1=2
> <!--#/sendmail-->
> 
> Try it and see if it works. 
> 
> Maybe these headers (content-type and enconding) should be added by
> default. 

In fact looking at the code in MailHost.py again, these headers are already
added. Is the quoting going wrong or is are the headers wrong or is it the
email client?

       bfile = StringIO(body)
        mo=rfc822.Message(bfile)
        for k, v in mo.items():
            self.conn.send('%s: %s\015\012' % (string.capitalize(k), v))
        # Add some Mime headers if not present
        if not mo.has_key('Mime-Version'):
            self.conn.send('Mime-Version: 1.0\015\012')
        if not mo.has_key('Content-Type'):
            self.conn.send(
                'Content-Type: text/plain; charset="iso-8859-1"\015\012')
        if not mo.has_key('Content-Transfer-Encoding'):
            self.conn.send(
                'Content-Transfer-Encoding: quoted-printable\015\012')
        self.conn.send('\015\012')
        body=bfile.read()
        body=self.singledots.sub('..', body)
        body=string.replace(body, '\r\n', '\n')
        body=string.replace(body, '\r', '\n')
        body=encode(body, 0)
        body=string.replace(body, '\n', '\015\012')
        self.conn.send(body)
        self.conn.send("\015\012.\015\012")
        self._check('354') 

    def _close(self):
        self.conn.send("quit\015\012")
        self.conn.close()





ESCAPE = '='
MAXLINESIZE = 76
HEX = '0123456789ABCDEF'

def needsquoting(c, quotetabs):
    if c == '\t':
        return not quotetabs
    return c == ESCAPE or not(' ' <= c <= '~')

def quote(c):
    if c == ESCAPE:
        return ESCAPE * 2
    else:
        i = ord(c)
        return ESCAPE + HEX[i/16] + HEX[i%16]

def encode(input, quotetabs):
    """Encode a string to Quoted-Printable"""
    output = ''
    for line in string.split(input, '\n'):
        new = ''
        prev = ''
        for c in line:
            if needsquoting(c, quotetabs):
                c = quote(c)
            if len(new) + len(c) >= MAXLINESIZE:
                output = output + new + ESCAPE + '\n'
                new = ''
            new = new + c
            prev = c
        if prev in (' ', '\t'):
            output = output + new + ESCAPE + '\n\n'
        else:
            output = output + new + '\n'
    return output


def decapitate(message):
    # split message into headers / body
    mfile=StringIO(message)
    mo=rfc822.Message(mfile)

    hd={}
    hd['to']=[]
    for header in (mo.getaddrlist('to'),
                   mo.getaddrlist('cc')):
        if not header: continue
        for name, addr in header:
            hd['to'].append(addr)
    
    hd['from']=mo.getaddr('from')[1]
    hd['subject']=mo.getheader('subject') or "No Subject"

    return hd, mfile.read()