help using smtplib to work ..

N

NicolasG

I'm using the following code to send e-mail through python:

import smtplib

fromaddr = '(e-mail address removed)'
toaddrs = '(e-mail address removed)'
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:<[email protected]> 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', '(e-mail address removed)')

What am I doing wrong ?
 
L

Larry Bates

NicolasG said:
I'm using the following code to send e-mail through python:

import smtplib

fromaddr = '(e-mail address removed)'
toaddrs = '(e-mail address removed)'
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:<[email protected]> 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', '(e-mail address removed)')

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. (e-mail address removed)) 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
 
G

Gabriel Genellina

I'm using the following code to send e-mail through python:

import smtplib

fromaddr = '(e-mail address removed)'
toaddrs = '(e-mail address removed)'
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()

send: 'mail FROM:<[email protected]> size=106\r\n'
reply: '530 5.7.0 Must issue a STARTTLS command first
s1sm7666914uge\r\n'

What am I doing wrong ?

gmail.com requires authentication before sending mail. Try this
sequence: ehlo, starttls, ehlo (again!), login, sendmail, quit
(BTW, you need a space after Subject:, and all \n should be \r\n)


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
N

NicolasG

<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. (e-mail address removed)) 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
Actually I used alt1.gmail-smtp-in.l.google.com for a server I don't
need authentication. The problem is that for some mail addresses I can
send mail's while for other's I can't . I don't know how to explained
that.....
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.
the code bellow worked fine to me :

import smtplib

fromaddr = '(e-mail address removed)'
toaddrs = '(e-mail address removed)'
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('alt1.gmail-smtp-in.l.google.com')
#'smtp.gmail.com')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

cheers.
 
T

Tim Williams

Actually I used alt1.gmail-smtp-in.l.google.com for a server I don't
need authentication. The problem is that for some mail addresses I can
send mail's while for other's I can't . I don't know how to explained
that.....

Without authentication, you will only be able to send to the severs
local addresses eg (e-mail address removed) (e-mail address removed)

or you can probably use your ISP's outgoing mail server, which usually
won't require authentication.

HTH :)
 
T

Tim Williams

If I left From empty I don't get any error but I still don't receive any
message.


How you can explain that only for some address in the From field I can
receive mails' and from others I can't ?
It looks really weird to me...

It must be down to how gmail's server(s) work.
Other question : Suppose I use the login procedure, the password will be
vulnerable to attacks ? If I'm right I think when no ssl is used for
authentication some one can easy see the password just by sniffing the
packets.

I think the risk is low to be honest. However, smtp.gmail.com uses
TLS (SSL) on ports 465 or 587. You can call STARTTLS from within
smtplib. but TrevorP's TLS-lite extensions to smtplib handle it
better

http://trevp.net/tlslite/

:)
 
J

Jordan

Your post was definitely the most helpful for me. For some reason,
smtp for gmail seems to require that you call server.ehlo() a second
time, after having called server.starttls(), otherwise, the server
won't accept authorization. Thanks.

-Jordan
 
T

Tim Williams

For some reason,
smtp for gmail seems to require that you call server.ehlo() a second
time, after having called server.starttls(), otherwise, the server
won't accept authorization. Thanks.

Not just GMAIL, all (RFC Compliant) SMTP servers require a 2nd EHLO
after a successful STARTTLS command.

The list of returned options / available commands will usually be
different for the 2nd EHLO. Often this will just mean the removal of
the STARTTLS option, but sometimes there can be additional
functionality offered.

HTH :)
 
G

Gabriel Genellina

Your post was definitely the most helpful for me. For some reason,
smtp for gmail seems to require that you call server.ehlo() a second
time, after having called server.starttls(), otherwise, the server
won't accept authorization. Thanks.

That's clearly stated on the docs for the starttls method:
http://docs.python.org/lib/SMTP-objects.html


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top