How to send E-mail without an external SMTP server ?

F

fdu.xiaojf

Hi,

I just want to send a very simple email from within python.

I think the standard module of smtpd in python can do this, but I
haven't found documents about how to use it after googleing. Are there
any examples of using smtpd ? I'm not an expert,so I need some examples
to learn how to use it.

Or maybe there is a better way to to this?

Thanks.

xiaojf
 
R

Rob Wolfe

Hi,

I just want to send a very simple email from within python.

I think the standard module of smtpd in python can do this, but I
haven't found documents about how to use it after googleing. Are there
any examples of using smtpd ? I'm not an expert,so I need some examples
to learn how to use it.

See standard documentation:

http://docs.python.org/lib/SMTP-example.html

HTH,
Rob
 
L

Leo Kislov

Hi,

I just want to send a very simple email from within python.

I think the standard module of smtpd in python can do this, but I
haven't found documents about how to use it after googleing. Are there
any examples of using smtpd ? I'm not an expert,so I need some examples
to learn how to use it.

smtpd is for relaying mail not for sending. What you need it a dns
toolkit (search cheeseshop) to map domain name to list of incoming mail
servers, and then using stdlib smtplib try to submit the message to
them.
Or maybe there is a better way to to this?

This won't work if you're behind a strict corporate firewall or if ISP
is blocking port 25 outgoing connections. In those cases you _have_ to
use an external mail server.
 
F

fdu.xiaojf

Rob said:

I have read the example and copied the code and save as send.py, then I
run it. Here is the output:
$ python send.py
From: (e-mail address removed)
To: (e-mail address removed)
Enter message, end with ^D (Unix) or ^Z (Windows):
just a test from localhost
Message length is 82
send: 'ehlo [202.127.19.74]\r\n'
reply: '250-WebMail\r\n'
reply: '250 AUTH plain\r\n'
reply: retcode (250); Msg: WebMail
AUTH plain
send: 'mail FROM:<[email protected]>\r\n'
reply: '502 negative vibes\r\n'
reply: retcode (502); Msg: negative vibes
send: 'rset\r\n'
reply: '502 negative vibes\r\n'
reply: retcode (502); Msg: negative vibes
Traceback (most recent call last):
File "send.py", line 26, in ?
server.sendmail(fromaddr, toaddrs, msg)
File "/usr/lib/python2.4/smtplib.py", line 680, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (502, 'negative vibes', '(e-mail address removed)')


Do I have to setup a smtp server on my localhost ?

Thanks a lot.

xiaojf
 
L

Leo Kislov

Rob said:
(e-mail address removed) wrote:
See standard documentation:

HTH,
RobI have read the example and copied the code and save as send.py, then I
run it. Here is the output:
$ python send.py
From: (e-mail address removed)
To: (e-mail address removed)
Enter message, end with ^D (Unix) or ^Z (Windows):
just a test from localhost
Message length is 82
send: 'ehlo [202.127.19.74]\r\n'
reply: '250-WebMail\r\n'
reply: '250 AUTH plain\r\n'
reply: retcode (250); Msg: WebMail
AUTH plain
send: 'mail FROM:<[email protected]>\r\n'
reply: '502 negative vibes\r\n'
reply: retcode (502); Msg: negative vibes
send: 'rset\r\n'
reply: '502 negative vibes\r\n'
reply: retcode (502); Msg: negative vibes
Traceback (most recent call last):
File "send.py", line 26, in ?
server.sendmail(fromaddr, toaddrs, msg)
File "/usr/lib/python2.4/smtplib.py", line 680, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (502, 'negative vibes', '(e-mail address removed)')

Do I have to setup a smtp server on my localhost ?

You need to use login method
<http://docs.python.org/lib/SMTP-objects.html>. And by the way, the
subject of your message is very confusing, you are posting log where
you're sending email using external server.
 
F

fdu.xiaojf

Leo said:
You need to use login method
<http://docs.python.org/lib/SMTP-objects.html>. And by the way, the
subject of your message is very confusing, you are posting log where
you're sending email using external server.
Thanks.

Yes, I want to find a way to send email without an external smtp server.

According to Rob's reply, I thought this could be implemented as
described in
the example code of "SMTP Example" on
"http://docs.python.org/lib/SMTP-example.html". So I tried the code,
but failed.

Actually I was writing a script that can submit jobs automatically on a
remote server. I should not submit the jobs all at once, because this
will take up all resources of the server. I just want a specified number
of jobs to be running at one time. If one jobs is over, the script
should submit another job. I run the script with nohup, then I logout.
The script will take care of all jobs.

But I have to check the status of the script manually, which is very
inefficient. So I want to be informed automatically by E-mail when all
jobs are done.


It's not safe if I have to use login method explicitly by which I have
to put my username and password in the script. I have also tried the
Unix command 'mail', but without success, either. I could use 'mail' to
send an E-mail to the user on the server, but I couldn't send an E-mail
to an external E-mail server. I realized that it may because the port 25
outgoing connections are blocked, so I gave up. I will have to login
periodically to check the status of the jobs:-(

Anyway, thank you, Rob, and thank you, Leo.

xiaojf
 
L

Leo Kislov

It's not safe if I have to use login method explicitly by which I have
to put my username and password in the script. I have also tried the
Unix command 'mail', but without success, either. I could use 'mail' to
send an E-mail to the user on the server, but I couldn't send an E-mail
to an external E-mail server. I realized that it may because the port 25
outgoing connections are blocked, so I gave up. I will have to login
periodically to check the status of the jobs:-(

Using username password is safe as long as you trust system admin, you
just need to make your script readable only to you. Or even better put
the username and password in a separate file. There is also a way to
limit damage in case you don't trust admin, you just need to get auth
token. Start smtp session and set debug level(True), use login method
and see the token:

send: 'AUTH PLAIN <HERE IS THE TOKEN>\r\n'
reply: '235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: 2.7.0 Accepted

Then put the token in a file readable only to you, and from now on
instead of login() method use docmd('AUTH PLAIN',"<YOUR TOKEN FROM
FILE>). If the token is stolen, the thief can only send mail from your
account but won't be able to login with password.
 
G

Grant Edwards

Yes, I want to find a way to send email without an external smtp server.

If you're not going to use an SMTP server, what sort of server
do you want to use?
 
D

Dennis Lee Bieber

Yes, I want to find a way to send email without an external smtp server.
You're going to have to hit an SMTPd somewhere along the route...
Many moons ago it was easy to connect directly to the recipient's mail
server (and even easier to grab any server one could find, to send mail
to any address). In closing down these "open relays" to block spammers,
many SMTPd's are now configured to accept "outgoing" mail only from
hosts that are part of the local network, and to accept "incoming" mail
only if the recipient is a member of the local network. Hence, most
client programs are now configured to connect to the local ISP server to
relay mail.
Actually I was writing a script that can submit jobs automatically on a
remote server. I should not submit the jobs all at once, because this

Does that "remote server" machine have, or is part of a network
with, an outgoing SMTPd process? If so, that is likely the server you
need to submit the message to.
It's not safe if I have to use login method explicitly by which I have
to put my username and password in the script. I have also tried the

Well, the username itself is likely available from the environment
of the process running.
import os
os.environ["USERNAME"] 'Wulfraed'

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
Yes, I want to find a way to send email without an external smtp server.

You can't. Use a DNS server to find the MX record of the destination
domain, connect to that SMTP server, then deliver the mail.

Sybren
 
G

Grant Edwards

You're going to have to hit an SMTPd somewhere along
the route... Many moons ago it was easy to connect directly to
the recipient's mail server (and even easier to grab any
server one could find, to send mail to any address).

But those were smtp servers, so I don't see you the OP could
have accomplished his goal unless you set the wayback machine
to pre-Internet days and use the uucp transport mechanism.
In closing down these "open relays" to block spammers, many
SMTPd's are now configured to accept "outgoing" mail only from
hosts that are part of the local network, and to accept
"incoming" mail only if the recipient is a member of the local
network. Hence, most client programs are now configured to
connect to the local ISP server to relay mail.

That local ISP server is also an smtp server. I haven't heard
of anybody accepting vail via uucp for at least 10 years. I'm
not saying that such hosts don't exist, but that they're pretty
darned rare.
 
G

Grant Edwards

You can't. Use a DNS server to find the MX record of the
destination domain, connect to that SMTP server, then deliver
the mail.

Or, just send it to a relay server for which you have
privledges. Those are the two choices, and both mean using an
SMTP server.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top