[Twisted-Python] SMTP client with authentication

Marcin Kasperski Marcin.Kasperski at softax.com.pl
Fri Apr 28 09:28:29 EDT 2006


Dnia piątek, 28 kwietnia 2006 14:22, Mustafa Sakalsiz napisał:
> Hi,
>
> I try to implement a simple smtp client, but my smtp server
> requires authentication. Is there a client sample which
> accomplishes the basic or plain smtp authentication.

I use the following replacement for the standard twisted sendmail function:

from twisted.mail import smtp
from twisted.internet import reactor
from twisted.internet import defer

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def sendmail_auth(smtphost, user, password,
                 from_addr, to_addrs, msg, senderDomainName=None, port=25):
    if not hasattr(msg,'read'):
        msg = StringIO(str(msg))
    d = defer.Deferred()
    factory = smtp.ESMTPSenderFactory(user, password, from_addr, to_addrs, msg, d,
                                      requireTransportSecurity=False)
    if senderDomainName is not None:
        factory.domain = senderDomainName
    reactor.connectTCP(smtphost, port, factory)
    return d

(nothing wise, just copied original and changed to ESMTPSenderFactory). Maybe it would make sense to add something like that to twisted.




More information about the Twisted-Python mailing list