smtplib starttls gmail example - comments?
Tim Williams
tim at tdw.net
Wed Jan 24 10:24:40 EST 2007
On 24/01/07, BJ Swope <bigblueswope at gmail.com> wrote:
>
>
>
> On 1/24/07, Tim Williams <tim at tdw.net> wrote:
> >
> > On 24/01/07, py <py at th.on> wrote:
> > > I would love for anybody to comment on this code with regard to
> redundancy/efficiency/wordiness or whatever else.
> > > for instance, do i understand correctly that i cant have a try: else:
> without an intervening except:?
> > > -dave
> > >
> > > stdout.write("calling smtp server...")
> > > try:
> > > server = SMTP(msgsmtp)
> > > except:
> > > stdout.write("FAIL.") #using .write to avoid the implied
> \n with print
> > > else:
> > > server.set_debuglevel(msgdebug)
> > > stdout.write("starting tls...")
> > > server.ehlo(msgfrom)
> > > try: server.starttls ()
> > > except: stdout.write("FAIL.")
> > > else:
> > > server.ehlo(msgfrom) #neessary duplication (?)
> > > stdout.write("logging in...")
> > > try: server.login(msgfrom, msgpass)
> > > except: stdout.write("FAIL.")
> > > else:
> > > stdout.write("sending...")
> > > try: server.sendmail(msgfrom, msgto, msgtxt +
> "\n.\n")
> > > except: stdout.write("FAIL.")
> > > else:
> > > try:
> > > server.quit()
> > > except sslerror: # a known and largely
> ignored issue with early EOF in ssl protocol
> > > stdout.write("success.")
> > > else:
> > > stdout.write("success.")
> > > --
> >
> >
> > *** Not tested but should have the same functionality and error
> > handling as your script ***
> >
> > this_host = 'myhostname.mydom1.com'
> > print "calling smtp server...", # the trailing comma removes '\n'
> > try:
> > server =
> smtplib.SMTP(msgsmtp,local_hostname=this_host)
> > server.set_debuglevel(msgdebug)
> > print "starting tls...",
> > server.starttls()
> > server.ehlo(this_host) # RFC requirement for 2nd EHLO after
> requesting TLS
> > print "logging in...",
> > server.login(msgfrom, msgpass)
> > print "sending...",
> > failed = server.sendmail(msgfrom, msgto, msgtxt + "\n.\n")
> > try:
> > server.quit()
> > except: pass
> > print "success."
> > except:
> > print "FAIL.",
> >
> > if failed:
> > print "failed:", failed # some recipients, but not all of them,
> failed
> > --
> > http://mail.python.org/mailman/listinfo/python-list
>
>
> Both examples have included the cardinal sin in smtp...
>
> They both send the message text followed by new line dot new line.
>
> The smtp protocol specifically mentions CRLF dot CRLF. Please please
> please use \r\n.\r\n in your code...
>
Ha, yeah I didn't spot that, but then I wasn't looking for it either :)
However, Its not an issue with smtplib because its takes these kind of
things into account by:
* modifying line endings that aren't RFC compliant to CRLFs.
* normalising/changing a single leading dot (.) to dot dot (..)
* adding the CRLF dot CRLF to the end of the supplied message text.
So adding adding "\n.\n" to the end of the msg text will just add a
line containing ".." to the end of the message text, it won't
terminate the DATA part.
that line should therefore be just
failed = server.sendmail(msgfrom, msgto, msgtxt)
:)
More information about the Python-list
mailing list