1

I am trying to run a php script that will call a python script that calls imaplib to pull some files from my email. When I run the python script standalone in vscode (bash terminal), it works as designed. When I run the PHP script that calls the python script, the following error is produced.

Traceback (most recent call last):
  File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 43, in <module>
    mail = imaplib.IMAP4_SSL('imap.googlemail.com')
AttributeError: module 'imaplib' has no attribute 'IMAP4_SSL'

As a test, I echoed out the command and pasted it into Git Bash. The files were pulled and added to the folder specified. When I pasted the command into CMD, the error above was produced. What settings am I missing?

I am using XAMPP, VSCode, PHP 7.3.4, Python 3.7.1

From recommendation below I added "import ssl" to my python script, I received the following error:

"Traceback (most recent call last):"
"  File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 2, in <module>"
"    import ssl"
"  File "C:\Users\Garrett\Anaconda3\envs\snowflakes\lib\ssl.py", line 98, in <module>"
"    import _ssl             # if we can't import it, let the error propagate"
"ImportError: DLL load failed: The specified module could not be found."

so then I went to my python environment, and tried to "pip install ssl" and I got the following error.

(snowflakes) C:\Users\Garrett>pip install ssl
Collecting ssl
  Using cached https://files.pythonhosted.org/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\setup.py", line 33
        print 'looking for', f
                          ^
    SyntaxError: Missing parentheses in call to 'print'. Did you mean print('looking for', f)?
    ----------------------------------------
ERROR: Command "python setup.py egg_info" failed with error code 1 in C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\

(snowflakes) C:\Users\Garrett>

Another Update

The following command is what is executed in PHP (exec()). The input folder will contain files pulled from email. The output folder will contain a singular file created from the files pulled from email. If I paste this command into GitBash, everything works as planned. If I paste the following command into CMD, I get the error shown above.

C:/Users/Garrett/Anaconda3/python.exe C:/xampp/htdocs/report/pythonfile/parse_script_og.py C:/xampp/htdocs/report/input/ C:/xampp/htdocs/report/output/out.csv John Doe 06-Jan-2018 16-Jan-2019
Garrett
  • 107
  • 10
  • I have to ask: why do this in Python when PHP has an extensive range of [IMAP functions](https://www.php.net/manual/en/book.imap.php)? –  May 24 '19 at 00:45
  • working in a team, this is where I pick up the baton. Unfortunately the existing code does not work on my machine with my current settings. – Garrett May 24 '19 at 00:57
  • Your PYTHONPATH or similar is munged when running it, and it can't find its support DLLs (eg, the internal _ssl module). – Max May 24 '19 at 01:58
  • I have tried making a few modifications to my Environment variables without luck, updating the body again with more details – Garrett May 24 '19 at 19:49

1 Answers1

0

IMAP4_SSL is only present if Python's ssl module is available (you can test with import ssl). Try upgrading your server's python version, and/or installing the ssl module.

https://docs.python.org/3/library/ssl.html

EDIT 1:

There’s also a subclass for secure connections:

class imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None)

This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is a ssl.SSLContext object which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read Security considerations for best practices.

keyfile and certfile are a legacy alternative to ssl_context - they can point to PEM-formatted private key and certificate chain files for the SSL connection. Note that the keyfile/certfile parameters are mutually exclusive with ssl_context, a ValueError is raised if keyfile/certfile is provided along with ssl_context.

Changed in version 3.3: ssl_context parameter added.

Changed in version 3.4: The class now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI).

Deprecated since version 3.6: keyfile and certfile are deprecated in favor of ssl_context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.

https://docs.python.org/3/library/imaplib.html

EDIT 2:

you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:

$mystring = exec('/usr/bin/python /home/user/testing.py');

and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.

Python Script Failing to Execute from PHP exec()

Hello World
  • 207
  • 1
  • 12
  • 1
    added what happened when I added "import ssl" to my python script above – Garrett May 24 '19 at 01:19
  • Are you sure you could install the module correctly? – Hello World May 24 '19 at 01:30
  • could you clarify? I have installed everything to the best of my current knowledge – Garrett May 24 '19 at 01:32
  • Python comes with SSL. It is not something you need to install. – Max May 24 '19 at 01:57
  • exec() in PHP executes command line arguments, the command line doesn't like ssl in my current setup. It is not clear to me how I fix this. I looked at the documentation above and when I tried the import ssl in my python script it produced the error above. There is a way to run this on in windows without explicitly importing ssl, but that remains a mystery as of now. – Garrett May 24 '19 at 02:19
  • try to update the version of setuptools, Simply pip install --upgrade setuptools – Hello World May 24 '19 at 02:30
  • I was able to update setuptools, but I am still getting the issue unfortunately. Something I found today was that in CMD I can type "python"it will pull up the python interpreter. I am then able to successfully import imaplib and run imaplib.IMAP4_SSL(). However if I run the command in CMD listed above, I get the error. – Garrett May 24 '19 at 23:08