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

Discussion in 'Python' started by fdu.xiaojf@gmail.com, Oct 16, 2006.

  1. Guest

    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
     
    , Oct 16, 2006
    #1
    1. Advertising

  2. Rob Wolfe Guest

    wrote:
    > 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
     
    Rob Wolfe, Oct 16, 2006
    #2
    1. Advertising

  3. Leo Kislov Guest

    On Oct 15, 10:25 pm, "" <>
    wrote:
    > 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.
     
    Leo Kislov, Oct 16, 2006
    #3
  4. Guest

    Rob Wolfe wrote:
    > wrote:
    >
    >> 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
    >
    >


    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:
    To:
    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:<>\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', '')


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

    Thanks a lot.

    xiaojf
     
    , Oct 16, 2006
    #4
  5. Rob Wolfe Guest

    wrote:

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


    If I see correctly your smtp server is gmail.com.

    HTH,
    Rob
     
    Rob Wolfe, Oct 16, 2006
    #5
  6. Leo Kislov Guest

    On Oct 16, 12:31 am, "" <>
    wrote:
    > Rob Wolfe wrote:
    > > wrote:

    >
    > >> 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,
    > > 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:
    > To:
    > 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:<>\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', '')
    >
    > 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.
     
    Leo Kislov, Oct 16, 2006
    #6
  7. Guest

    Leo Kislov wrote:
    >>
    >> 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.
    >
    >

    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
     
    , Oct 16, 2006
    #7
  8. Leo Kislov Guest

    On Oct 16, 2:04 am, "" <>
    wrote:
    > 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.
     
    Leo Kislov, Oct 16, 2006
    #8
  9. On 2006-10-16, <> wrote:

    > 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?

    --
    Grant Edwards grante Yow! Let me do my TRIBUTE
    at to FISHNET STOCKINGS...
    visi.com
     
    Grant Edwards, Oct 16, 2006
    #9
  10. On Mon, 16 Oct 2006 17:04:19 +0800, ""
    <> declaimed the following in comp.lang.python:

    >
    > 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 16, 2006
    #10
  11. 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
    --
    Sybren Stüvel
    Stüvel IT - http://www.stuvel.eu/
     
    Sybren Stuvel, Oct 16, 2006
    #11
  12. On 2006-10-16, Dennis Lee Bieber <> wrote:
    > On Mon, 16 Oct 2006 17:04:19 +0800, ""
    ><> declaimed the following in comp.lang.python:
    >
    >>
    >> 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).


    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.

    --
    Grant Edwards grante Yow! Today, THREE WINOS
    at from DETROIT sold me a
    visi.com framed photo of TAB HUNTER
    before his MAKEOVER!
     
    Grant Edwards, Oct 16, 2006
    #12
  13. On 2006-10-16, Sybren Stuvel <> wrote:

    >> 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.


    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.

    --
    Grant Edwards grante Yow! Gibble, Gobble, we
    at ACCEPT YOU...
    visi.com
     
    Grant Edwards, Oct 16, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ones Self
    Replies:
    1
    Views:
    741
    Karel Kubat
    Jul 1, 2004
  2. =?Utf-8?B?QW5kcmVhcw==?=

    Send email via external SMTP Server

    =?Utf-8?B?QW5kcmVhcw==?=, Jan 14, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    6,674
    Juan T. Llibre
    Jan 18, 2005
  3. Bruce Hodge
    Replies:
    0
    Views:
    426
    Bruce Hodge
    Feb 15, 2006
  4. Replies:
    3
    Views:
    1,095
    Roedy Green
    Jan 24, 2006
  5. Alex Hunsley
    Replies:
    4
    Views:
    3,501
    Tim Williams (gmail)
    Jun 29, 2004
Loading...

Share This Page