-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

How about something like this?

Create a function and then call it with the required optins of server
url, sender address, destination address, subject, and message text?



def send(serverURL=None, sender='', to='', subject='', text=''):
    """
    Form and send an e-mail message
    """

    import smtplib

    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
    message = headers + text
    mailServer = smtplib.SMTP(serverURL) # replace serverURL with site
specific mail server
    check_valid=mailer.isAddressValid(to)
    if check_valid==1 :
            mailServer.sendmail(sender, to, message)
            mailServer.quit()
    else:
            print "\n\nDestination e-mail address %s is invalid.
\n\n\tPlease check and run again" % to



def isAddressValid(addr):
        """
        Validate e-mail addresses based on lexical rules set forth in
RFC 822
        Ported from Recipe 3.9 in Secure Programming Cookbook for C and
C++ by
        John Viega and Matt Messier (O'Reilly 2003)

        If the supplied email address is syntactically valid,
isAddressValid()
        will return 1; otherwise, it will return 0.
        """
        # Mail variables
        rfc822_specials = '()<>@,;:\\"[]'

        c = 0
        while c < len(addr):
            if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c
- - 1] == '"'):
                c = c + 1
                while c < len(addr):
                    if addr[c] == '"': break
                    if addr[c] == '\\' and addr[c + 1] == ' ':
                        c = c + 2
                        continue
                    if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0
                    c = c + 1
                else: return 0
                if addr[c] == '@': break
                if addr[c] != '.': return 0
                c = c + 1
                continue
            if addr[c] == '@': break
            if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
            if addr[c] in rfc822_specials: return 0
            c = c + 1
        if not c or addr[c - 1] == '.': return 0

        # Next we validate the domain portion ([EMAIL PROTECTED])
        domain = c = c + 1
        if domain >= len(addr): return 0
        count = 0
        while c < len(addr):
            if addr[c] == '.':
                if c == domain or addr[c - 1] == '.': return 0
                count = count + 1
            if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
            if addr[c] in rfc822_specials: return 0
            c = c + 1

        return count >= 1

euoar wrote:
> I'm trying to learn how to send e-mails with python. But I always become 
> this error: socket.error: (110, 'Connection timed out'). Is there 
> something bad in the code? Does anyone knows what could be wrong? I'm 
> using python from linux, and have no proxy...
> 
> The code that I use is this (of course, filling with my persona data the 
> " "):
> 
> import smtplib
> 
> smtpserver = 'my smtp server goes here'
> AUTHREQUIRED = 1
> smtpuser = 'my user name'
> smtppass = 'my password'
> 
> RECIPIENTS = ['the recipients']
> SENDER = 'my mail'
> mssg = "una prueba desde python"
> 
> session = smtplib.SMTP(smtpserver)
> if AUTHREQUIRED:
>     session.login(smtpuser, smtppass)
> smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
> 
> if smtpresult:
>     errstr = ""
>     for recip in smtpresult.keys():
>         errstr = """Could not delivery mail to: %s
> 
> Server said: %s
> %s
> 
> %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
>     raise smtplib.SMTPException, errstr
> 
> 
> 
> Thank you for your help
> 
> 
> 
>       
>       
>               
> ______________________________________________ 
> LLama Gratis a cualquier PC del Mundo. 
> Llamadas a fijos y móviles desde 1 céntimo por minuto. 
> http://es.voice.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: GnuPT 2.7.2

iD8DBQFFOhCLDvn/4H0LjDwRAm9AAJwOkrtF/R4zWQhhCwjlBrBVQ4XpwwCgpGHy
ukZIoIuoAh/hmBZ/r/ZbQmM=
=6weK
-----END PGP SIGNATURE-----
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to