Sending mail from 'current user' in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leo Breebaart

    Sending mail from 'current user' in Python

    I am writing a utility in Python and I'd like to add a
    command-line option "--mailto <address>" that will cause an
    e-mail summary to be sent to <address> when the utility finishes
    running.

    My first thought was to use smtplib.sendmai l(), and basically
    this works like a charm, except that this function expects a
    valid 'sender' address as a parameter.

    Every single smtplib example I've found so far shows a hardcoded
    'sender' address, but as my utility can be run by any user on any
    system I am looking for a way to obtain or create a valid default
    value.

    I can get the username info (at least on Unix) via the 'pwd'
    module, but that still leaves me with the domainname, or rather
    the mailname, and I have not been able to spot a way of finding
    that from within Python. (I could try opening /etc/mailname by
    hand, but how standard is that filename/location?)

    I've also tried opening a pipe to sendmail, and feeding the
    message to that instead. This too works great (and does give an
    appropriate default 'From'), but that also turns my problem into
    the problem of finding the location of the sendmail program,
    which doesn't seem like much of an improvement, portability-wise.

    Finally, if at all possible I'd also like to get this working on
    Windows, so I'd rather stick with the standard smtplib if I can.

    Does anybody here have any thoughts on this?

    --
    Leo Breebaart <leo@lspace.org >
  • Mike Meyer

    #2
    Re: Sending mail from 'current user' in Python

    Leo Breebaart <leo@lspace.org > writes:
    [color=blue]
    > I can get the username info (at least on Unix) via the 'pwd'
    > module, but that still leaves me with the domainname, or rather
    > the mailname, and I have not been able to spot a way of finding
    > that from within Python. (I could try opening /etc/mailname by
    > hand, but how standard is that filename/location?)[/color]

    Not very. Certainly doesn't exist on FreeBSD, and doesn't appear
    anywhere in the sources for my UMA.

    BTW, an alternative for the username is the USER environment
    variable. I don't know whether or not it exists on Windows.
    [color=blue]
    > I've also tried opening a pipe to sendmail, and feeding the
    > message to that instead. This too works great (and does give an
    > appropriate default 'From'), but that also turns my problem into
    > the problem of finding the location of the sendmail program,
    > which doesn't seem like much of an improvement, portability-wise.[/color]

    Well, you could provide a list of places to look for it. But you're
    right, this doesn't help much with portability.
    [color=blue]
    > Finally, if at all possible I'd also like to get this working on
    > Windows, so I'd rather stick with the standard smtplib if I can.[/color]

    smtplib needs an SMTP server to connect to. For unix systems, this is
    typically localhost. What do you use for Windows systems? Or are you
    connecting to your machine to deliver the mail?

    The problem with getting this working on Windows is that, IIRC, the
    information you need is configured in the UMA, and not in some
    system-wide location, at least on some versions of Windows, and it
    typically is unrelated to the name of the Windows box you're on.

    Given those constraints, the simple solution is probably to ask the
    user for the information you need.

    Failing that, and assuming os.env['USER'] exists on windows, you could
    try:

    1) Looking up the IP address of the host in question. Not the
    interface - you need the address the outside world sees.
    See <URL: http://www.whatismyip.com/ > for example.
    2) Do a reverse DNS lookup on that ip address to get a host name.
    If they've got a dynamic IP address, this will probably be
    something ugly, if you get anything at all.
    3) Start doing MX lookups on that host name, stripping off one
    domain level at a time from the left until you get an address
    with an MX record, or you've got nothing left. This assumes
    that an MX record will exist for the part of the domain name
    which get mail - which may not be true.

    This entire procedure also assumes that the user reads mail using
    their ISP-provided maildrop, which may not be true.

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Matt

      #3
      Re: Sending mail from 'current user' in Python



      Mike Meyer wrote:[color=blue]
      > Leo Breebaart <leo@lspace.org > writes:
      >[color=green]
      > > I can get the username info (at least on Unix) via the 'pwd'
      > > module, but that still leaves me with the domainname, or rather
      > > the mailname, and I have not been able to spot a way of finding
      > > that from within Python. (I could try opening /etc/mailname by
      > > hand, but how standard is that filename/location?)[/color]
      >
      > Not very. Certainly doesn't exist on FreeBSD, and doesn't appear
      > anywhere in the sources for my UMA.
      >
      > BTW, an alternative for the username is the USER environment
      > variable. I don't know whether or not it exists on Windows.
      >[color=green]
      > > I've also tried opening a pipe to sendmail, and feeding the
      > > message to that instead. This too works great (and does give an
      > > appropriate default 'From'), but that also turns my problem into
      > > the problem of finding the location of the sendmail program,
      > > which doesn't seem like much of an improvement, portability-wise.[/color]
      >
      > Well, you could provide a list of places to look for it. But you're
      > right, this doesn't help much with portability.
      >[color=green]
      > > Finally, if at all possible I'd also like to get this working on
      > > Windows, so I'd rather stick with the standard smtplib if I can.[/color]
      >
      > smtplib needs an SMTP server to connect to. For unix systems, this is
      > typically localhost. What do you use for Windows systems? Or are you
      > connecting to your machine to deliver the mail?
      >
      > The problem with getting this working on Windows is that, IIRC, the
      > information you need is configured in the UMA, and not in some
      > system-wide location, at least on some versions of Windows, and it
      > typically is unrelated to the name of the Windows box you're on.
      >
      > Given those constraints, the simple solution is probably to ask the
      > user for the information you need.
      >
      > Failing that, and assuming os.env['USER'] exists on windows, you could
      > try:
      >
      > 1) Looking up the IP address of the host in question. Not the
      > interface - you need the address the outside world sees.
      > See <URL: http://www.whatismyip.com/ > for example.
      > 2) Do a reverse DNS lookup on that ip address to get a host name.
      > If they've got a dynamic IP address, this will probably be
      > something ugly, if you get anything at all.
      > 3) Start doing MX lookups on that host name, stripping off one
      > domain level at a time from the left until you get an address
      > with an MX record, or you've got nothing left. This assumes
      > that an MX record will exist for the part of the domain name
      > which get mail - which may not be true.
      >
      > This entire procedure also assumes that the user reads mail using
      > their ISP-provided maildrop, which may not be true.
      >[/color]

      This does work (at least on WinXP Pro, using python 2.3.4):
      [color=blue][color=green][color=darkred]
      >>> os.environ["USERNAME"][/color][/color][/color]
      'myusername'


      [color=blue]
      > <mike
      > --
      > Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      > Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.[/color]

      Comment

      • Grig Gheorghiu

        #4
        Re: Sending mail from 'current user' in Python

        I use this function as a platform-independent way of finding out the
        current user name:

        def get_username():
        if sys.platform == 'win32':
        return win32api.GetUse rName()
        else:
        return getpass.getuser ()

        Comment

        • Irmen de Jong

          #5
          Re: Sending mail from 'current user' in Python

          Grig Gheorghiu wrote:[color=blue]
          > I use this function as a platform-independent way of finding out the
          > current user name:
          >
          > def get_username():
          > if sys.platform == 'win32':
          > return win32api.GetUse rName()
          > else:
          > return getpass.getuser ()
          >[/color]


          [e:\]python
          Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> import getpass
          >>> getpass.getuser ()[/color][/color][/color]
          'idj'

          So why bother with the win32api??

          --Irmen

          Comment

          • Grig Gheorghiu

            #6
            Re: Sending mail from 'current user' in Python

            There may have been a reason for the win32 stuff at some point....but I
            don't remember and you're right, it does seem like getpass by itself
            would do the job.

            Grig

            Comment

            • Marcus Alanen

              #7
              Re: Sending mail from 'current user' in Python

              Mike Meyer wrote:[color=blue]
              > BTW, an alternative for the username is the USER environment
              > variable. I don't know whether or not it exists on Windows.[/color]

              Or LOGNAME. Don't about windows, though.
              [color=blue][color=green]
              >>I've also tried opening a pipe to sendmail, and feeding the
              >>message to that instead. This too works great (and does give an
              >>appropriate default 'From'), but that also turns my problem into
              >>the problem of finding the location of the sendmail program,
              >>which doesn't seem like much of an improvement, portability-wise.[/color]
              > Well, you could provide a list of places to look for it. But you're
              > right, this doesn't help much with portability.[/color]

              No, but at least it can be expected to do the right thing w.r.t. sending
              the mail.
              [color=blue][color=green]
              >>Finally, if at all possible I'd also like to get this working on
              >>Windows, so I'd rather stick with the standard smtplib if I can.[/color]
              > smtplib needs an SMTP server to connect to. For unix systems, this is
              > typically localhost. What do you use for Windows systems? Or are you
              > connecting to your machine to deliver the mail?[/color]

              I'd be very surprised if the typical SMTP server is localhost on
              unix-like computers. Rather, sendmail is configured to transport the
              message to company/university mailserver(s). If that happens to fail,
              the mail is put on the queue at localhost, and transported later (e.g.
              via a cronjob) to the server. At no point is there a server on localhost
              involved. Of course, not everybody's computer is on such a network and a
              sendmail server may indeed be running on localhost, but that's not a
              very informed guess. Let the sendmail program take care of those details.

              The Unix Programming Frequently Asked Questions "Q5.2 What's the best
              way to send mail from a program?" is worth reading.

              I'd try some simple autodetection (Mike's suggestion sounded great) and
              prompt the user to correct the information, although sendmail itself
              ought to give good defaults, so this might not be necessary. Then try
              /usr/sbin/sendmail, /usr/libexec/sendmail and /usr/lib/sendmail. You
              could try using exitcode 127 to detect "program could not be found or
              executed" but I don't know how portable that is.

              I can't comment on the Windows side of things.

              Regards,
              Marcus

              Comment

              • Dennis Lee Bieber

                #8
                Re: Sending mail from 'current user' in Python

                On Sun, 12 Jun 2005 14:24:45 +0300, Marcus Alanen <marcus.alanen@ abo.fi>
                declaimed the following in comp.lang.pytho n:
                [color=blue]
                > Mike Meyer wrote:[color=green]
                > > BTW, an alternative for the username is the USER environment
                > > variable. I don't know whether or not it exists on Windows.[/color]
                >
                > Or LOGNAME. Don't about windows, though.[/color]

                How old a version of Windows?

                WinXP Pro: (I really need to figure out how to change the
                HOMEDRIVE/HOMEPATH -- though after yesterday, when APPDATA somehow got
                set to the W9x C:\Application Data, making all my applications think
                they were new installs -- I should have the info needed...)

                Microsoft Windows XP [Version 5.1.2600]
                (C) Copyright 1985-2001 Microsoft Corp.

                C:\Documents and Settings\Dennis Lee Bieber>echo %USERNAME%
                Wulfraed

                C:\Documents and Settings\Dennis Lee Bieber>

                {I figured out how to change the account name from the "real name"
                whilst figuring out how to clean up the above-mentioned APPDATA
                glitch...}
                --[color=blue]
                > =============== =============== =============== =============== == <
                > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                > wulfraed@dm.net | Bestiaria Support Staff <
                > =============== =============== =============== =============== == <
                > Home Page: <http://www.dm.net/~wulfraed/> <
                > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                Comment

                • Mike Meyer

                  #9
                  Re: Sending mail from 'current user' in Python

                  Marcus Alanen <marcus.alanen@ abo.fi> writes:[color=blue]
                  > Mike Meyer wrote:[color=green][color=darkred]
                  >>>Finally, if at all possible I'd also like to get this working on
                  >>>Windows, so I'd rather stick with the standard smtplib if I can.[/color]
                  >> smtplib needs an SMTP server to connect to. For unix systems, this is
                  >> typically localhost. What do you use for Windows systems? Or are you
                  >> connecting to your machine to deliver the mail?[/color]
                  > I'd be very surprised if the typical SMTP server is localhost on
                  > unix-like computers.[/color]

                  On reflection, you're right. It was the way I described when I started
                  working with Unix, and I still configure all my Unix systems that
                  way. But recently there's been move to have no servers configured by
                  default. I've been bit by this, now having to enable TCP on Postgres
                  by every time I upgrade the server. So most modern Unix systems
                  probably come out of the box without a local SMTP listener.
                  [color=blue]
                  > Rather, sendmail is configured to transport the message to
                  > company/university mailserver(s).[/color]

                  Sendmail? Don't you mean qmail/postfix/exim?
                  [color=blue]
                  > If that happens to fail,
                  > the mail is put on the queue at localhost, and transported later
                  > (e.g. via a cronjob) to the server. At no point is there a server on
                  > localhost involved. Of course, not everybody's computer is on such a
                  > network and a sendmail server may indeed be running on localhost, but
                  > that's not a very informed guess. Let the sendmail program take care
                  > of those details.[/color]

                  Well, maybe you do mean sendmail. The other MTAs typically aren't that
                  configurable. qmail, for instance, always queues messages to the local
                  disk if they are bound for another host.

                  In lieue of the current discussion, this stuff doesn't matter. All of
                  these MTAs come with a "sendmail" program designed specifically so
                  that programs that invoke sendmail to arrange delivery of a message
                  will continue work if you replace the system sendmail with their
                  version. On FreeBSD, this has been carried a step further: the system
                  sendmail is a wrapper that reads /etc/mail/mailer.conf to figure out
                  what sendmail binary to invoke, so that users don't have to deal with
                  replacing the system sendmail, or worry about accidently overwriting
                  the replaced system sendmail if they upgrade their system.

                  <mike
                  --
                  Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                  Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                  Comment

                  • Leo Breebaart

                    #10
                    Re: Sending mail from 'current user' in Python

                    Marcus Alanen <marcus.alanen@ abo.fi> writes:
                    [color=blue][color=green][color=darkred]
                    > >>I've also tried opening a pipe to sendmail, and feeding the
                    > >>message to that instead. This too works great [but] doesn't
                    > >>seem like much of an improvement, portability-wise.[/color][/color]
                    >
                    > No, but at least it can be expected to do the right thing
                    > w.r.t. sending the mail.[/color]

                    Good point.
                    [color=blue][color=green][color=darkred]
                    > >>Finally, if at all possible I'd also like to get this working on
                    > >>Windows, so I'd rather stick with the standard smtplib if I can.[/color]
                    > > smtplib needs an SMTP server to connect to. For unix systems, this is
                    > > typically localhost. What do you use for Windows systems? Or are you
                    > > connecting to your machine to deliver the mail?[/color]
                    >
                    > I'd be very surprised if the typical SMTP server is localhost
                    > on unix-like computers. [...] Let the sendmail program take
                    > care of those details.
                    >
                    > The Unix Programming Frequently Asked Questions "Q5.2 What's the best
                    > way to send mail from a program?" is worth reading.[/color]

                    Thanks to you and everyone else who contributed to this thread.

                    I've now decided to accept the inherent non-portabilitiness of
                    the whole concept of 'sending mail from within a program', and
                    will be going with a Unix-only see-if-you-can-find-sendmail
                    approach.

                    --
                    Leo Breebaart <leo@kronto.org >

                    Comment

                    Working...