help using smtplib to work ..

Larry Bates larry.bates at websafe.com
Wed Nov 8 18:09:41 EST 2006


NicolasG wrote:
> I'm using the following code to send e-mail through python:
> 
> import smtplib
> 
> fromaddr = 'myMail at gmail.com'
> toaddrs  = 'myOtherMail at gmail.com'
> subject = 'someting for subject !'
> 
> msg = 'This is body of the mail.'
> 
> msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject:' + subject
> + '\n\n' + msg
> print "Message length is " + repr(len(msg))
> 
> server = smtplib.SMTP('smtp.gmail.com')
> server.set_debuglevel(1)
> server.sendmail(fromaddr, toaddrs, msg)
> server.quit()
> 
> I have replaced the sender and receiver's mail address with the
> original ones but executing this code I get the error (ip is hided for
> obvious reason):
> 
> send: 'ehlo computer.domain_not_set.invalid\r\n'
> reply: '250-mx.google.com at your service, [xxx.xx.xxx.xxx]\r\n'
> reply: '250-SIZE 20971520\r\n'
> reply: '250-8BITMIME\r\n'
> reply: '250-STARTTLS\r\n'
> reply: '250 ENHANCEDSTATUSCODES\r\n'
> reply: retcode (250); Msg: mx.google.com at your service,
> [xxx.xx.xxx.xxx]
> SIZE 20971520
> 8BITMIME
> STARTTLS
> ENHANCEDSTATUSCODES
> send: 'mail FROM:<myMail at gmail.com> size=106\r\n'
> reply: '530 5.7.0 Must issue a STARTTLS command first
> s1sm7666914uge\r\n'
> reply: retcode (530); Msg: 5.7.0 Must issue a STARTTLS command first
> s1sm7666914uge
> send: 'rset\r\n'
> reply: '250 2.1.0 Flushed s1sm7666914uge\r\n'
> reply: retcode (250); Msg: 2.1.0 Flushed s1sm7666914uge
> Traceback (most recent call last):
>   File "C:\Documents and Settings\NicolasG\Desktop\smtpexample.py",
> line 17, in <module>
>     server.sendmail(fromaddr, toaddrs, msg)
>   File "C:\Python25\lib\smtplib.py", line 684, in sendmail
>     raise SMTPSenderRefused(code, resp, from_addr)
> SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first
> s1sm7666914uge', 'myMail at gmail.com')
> 
> What am I doing wrong ?
> 

<snip from Gmail SMTP setup instructions>

Under outgoing mail smtp server, use smtp.gmail.com. Since it requires SMTP
authorization, use your Gmail account as username (e.g. yourname at gmail.com) and
your Gmail password as password. You can turn TSL on or off.

<end snip>

The email server requires authentication before it can send.
Normally this can be accomplished either by calling

server.login(userid, password)

The only alternative is to use an open relay email server that allows
you to send without authenticating.  Hope info at least points you in
the right direction.

-Larry



More information about the Python-list mailing list