473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 5012
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bill | last post by:
I am trying to have the capability to email attachments. Specifically I want to be able to email a specific attachment that I name that may be a PDF document, text doc, etc. I already have a...
0
by: Frank Zheng | last post by:
I wrote some code to test "smtplib", but i met a problem when i call the "login(user,pass)" of the "SMTP" object. here are the codes: >>> s = smtplib.SMTP() >>> s.set_debuglevel(1) >>>...
6
by: stewart.midwinter | last post by:
I'm having problem with a script that used to work under Win2k but is now broken after an install of WinXP Pro. I can no longer connect to a local mail server. Has anyone else seen this? If so,...
3
by: Van_Gogh | last post by:
Hi, I am learning how to use the smtplib module, but am having some very early problems, maybe because I don't understand it. So, am I correct that by following the example in the Python: >>>...
14
by: sridhar | last post by:
iam having user account on an exchangeserver. with that can i send an email using python? if iam using the following code iam getting error fromAddress = 'sridhar_kasturi@satyam.com'...
3
by: Erik Johnson | last post by:
THE GOAL: I need to send an email with a simple ASCII text body and an attached HTML file. I have scripts that send basic emails via the smtplib module that don't have any attachements and that...
2
by: ornto | last post by:
Hi, I'm trying to create an application which checks a dynamic web site and on certain events sends an email to me. My problem though is with the email task. By now I made this simple test code: ...
5
by: zxo102 | last post by:
Hi, I am trying to use python module smtplib to send my email out on window xp (localhost). import smtplib server = smtplib.SMTP('localhost') but I got the error information as follows: ...
1
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.