Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • email from Stata on Mac

    I'm on Mac (version 16, 12 processor MP) and I would like to send email through Stata. I found the email package by William Buchanan, available on ssc, and have given it a shot but with no success. The help file indicates that it is compatible with Stata v 12.1 and requires Python 2.7. I looked at the ado file and there is a version command there so I don't think that is my problem.

    Here is the out-of-the-box command I tried as a tester and the error, with the email addresses masked. I double checked the emails to make sure they were correct and they are. I know little to nothing about Python so I don't know how to understand the error

    Code:
    . email, from("[email protected]") to("[email protected]") subject("I hope...") body("...it works.")
    (note: file body.txt not found)
    
    Traceback (most recent call last):
      File "/var/folders/wt/xskz7sw51492dqs6jj0d_6dc0000gp/T//S_35247.000001.py", line 9, in <module>
        s = smtplib.SMTP('localhost')
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 256, in __init__
        (code, msg) = self.connect(host, port)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 316, in connect
        self.sock = self._get_socket(host, port, self.timeout)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 291, in _get_socket
        return socket.create_connection((host, port), timeout)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection
        raise err
    socket.error: [Errno 61] Connection refused
    With the command being out-of-the-box, I recognize that something needed to change because Python 3.7 is what came with my Mac, not Python 2.7. So, I installed Python 2.7 using Homebrew. I can now run Python 2.7 from Terminal using the command python2. I tried editing the ado file in a similar way (i.e, python2 instead of python; there were two places where this was needed) so that the ado file would try to use the right version. After that edit, here is what I get...

    Code:
    . email, from("[email protected]") to("[email protected]") subject("I hope...") body("...it works.")
    (note: file body.txt not found)
    
    /bin/bash: python2: command not found
    So, here it seems that Stata is not even finding Python 2.7 even though I can run it from Terminal.

    It will be plain to see that I'm woefully inadequate to deal with this problem...I don't understand Python and how Stata works with it enough to even know if I've framed the question in a sensible way.

    Thanks for any insight.

    Alternatively, does anyone have an approach to emailing from Stata on Mac that has worked for them? I used to do this routinely on PC but made the switch to Mac years ago.

    Best,
    Lance


  • #2
    I adapted this process into a do-file with lots of calls to the terminal shell using the "!" command, but maybe this will help: http://mdzhang.com/posts/osx-terminal-email/
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Thanks for the tip Eric. I'm not convinced I'm up to the task but I'm going to give it a go.

      Best,
      Lance

      Comment


      • #4
        Just to tie this up, I ended up pursuing what seemed the easiest of the approaches for me that various people pointed to. That was adjusting William Buchanan's email program. I suppose my application is fairly specific but I thought I would share it in case it is helpful for anyone else. Here's the program...
        Code:
        *! email program adapted from email by William R. Buchanan
        *! Uses Python 3.7, which is native to MacOSX as of Oct 2019
        *! Must replace localhost and XXX in the code with appropriate host and port
        *! The body of the email must be previously composed and saved into a file
         
        cap prog drop email
         
        prog email
        syntax , From(str) To(str) Subject(str) File(str) Directory(str)
         
        if "`to'" == "" | "`from'" == "" {
                err 198
                di in red `"Must include 'To' and 'From' email addresses"'
        }
        if "`file'" == "" | "`directory'" == "" {
                err 198
                di in red `"Must include a .txt file as the message body and it's file path"'
                }
        else { 
                tempname em msg
                tempfile email
                file open  `em' using `email'.py, w replace
                file write `em' `"import smtplib"' _n
                file write `em' `"from email.mime.text import MIMEText"' _n
                file write `em' `"fp = open("`directory'/`file'", 'r')"' _n
                file write `em' `"msg = MIMEText(fp.read())"' _n
                file write `em' `"fp.close"' _n
                file write `em' `"msg['Subject'] = '`subject''"' _n
                file write `em' `"msg['From'] = '`from''"' _n
                file write `em' `"msg['To'] = '`to''"' _n
                file write `em' "s = smtplib.SMTP(host='localhost', port=XXX)" _n
                file write `em' `"s.sendmail(msg['From'], msg['To'], msg.as_string())"' _n
                file write `em' `"s.quit()"' _n
                file close `em'
                ! python "`email'.py"
        }
        end
        This adjusted version of the program requires the message body to come from a file that already exists. That is different than what the original program did, which was to allow the body text to be added in an option and/or to attach a file to the email. The program won't work "out-of-the-box" though. To get it to work, host and port have to be changed from the generic entries here to whatever is appropriate for your messaging service. I had to ask someone what these were at my university. That, however should be the only change needed to get it to work.

        Best,
        Lance

        Comment

        Working...
        X