472,122 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

help using smtplib to work ..

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

import smtplib

fromaddr = 'm*****@gmail.com'
toaddrs = 'm**********@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:<my****@gmail.comsize=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', 'm*****@gmail.com')

What am I doing wrong ?

Nov 8 '06 #1
8 4861
NicolasG wrote:
I'm using the following code to send e-mail through python:

import smtplib

fromaddr = 'm*****@gmail.com'
toaddrs = 'm**********@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:<my****@gmail.comsize=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', 'm*****@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. yo******@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
Nov 8 '06 #2
At Wednesday 8/11/2006 19:23, NicolasG wrote:
>I'm using the following code to send e-mail through python:

import smtplib

fromaddr = 'm*****@gmail.com'
toaddrs = 'm**********@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()

send: 'mail FROM:<my****@gmail.comsize=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
Nov 8 '06 #3
<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. yo******@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
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 = 'n*******@gmail.com'
toaddrs = 'n*******@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('alt1.gmail-smtp-in.l.google.com')
#'smtp.gmail.com')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

cheers.

Nov 8 '06 #4
On 8 Nov 2006 15:31:27 -0800, NicolasG <ni******@gmail.comwrote:
>
<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. yo******@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
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 ...@gmail.com ...@googlemail.com
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.
or you can probably use your ISP's outgoing mail server, which usually
won't require authentication.

HTH :)
Nov 8 '06 #5
On 09/11/06, Nicolas G <ni******@gmail.comwrote:
>

Usually when sending/relaying without authentication, the From is
irrelevant, only the To is taken into account. Maybe, GMAIL do
something different because they have to put the mail in the sender's
mailbox as well as the recipient's. Some ISPs will only allow a
local FROM address though.

As we're only talking about the envelope TO & FROM, you could try no
FROM address (None or <I forget which Smtplib needs) .

If I left From empty I don't get any error but I still don't receive any
message.
The header FROM can still have the real FROM address in it, as its not
(normally)
used during SMTP relaying.

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/

:)
Nov 9 '06 #6
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
Gabriel Genellina wrote:
At Wednesday 8/11/2006 19:23, NicolasG wrote:
I'm using the following code to send e-mail through python:

import smtplib

fromaddr = 'm*****@gmail.com'
toaddrs = 'm**********@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()

send: 'mail FROM:<my****@gmail.comsize=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
Nov 9 '06 #7
On 8 Nov 2006 19:45:00 -0800, Jordan <jo************@gmail.comwrote:
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 :)
Nov 9 '06 #8
At Thursday 9/11/2006 00:45, Jordan wrote:
>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
Nov 9 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bill | last post: by
reply views Thread by Frank Zheng | last post: by
6 posts views Thread by stewart.midwinter | last post: by
3 posts views Thread by Van_Gogh | last post: by
14 posts views Thread by sridhar | last post: by
3 posts views Thread by Erik Johnson | last post: by
2 posts views Thread by ornto | last post: by
1 post views Thread by Hunter | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.