如何在 cpanel 上的 php codigniter 控制器函数中包含 python 路径

How to include a python path in php codigniter controller function on cpanel

提问人:Syed Omer Shah 提问时间:10/30/2023 最后编辑:Syed Omer Shah 更新时间:10/30/2023 访问量:56

问:

我在 cpanel 上通过 php Codeigniter 控制器运行我的电子邮件脚本时遇到了问题,尽管它使用 xampp 在本地运行良好。我需要一个 Python 路径包含在 cpanel 上的 PHP 代码中。

这是我的PHP代码:

public function forgot_password() {
     // Define email from PHP input
    $get_record = $this->request->getPost('query');
    $get_record = json_decode($get_record);
    $recipient_email = $get_record[0];  
    $register_model = new RegisterModel();
    $data_login['row']=$register_model->where('email', $recipient_email)->first();
    $password = $data_login['row']['password'];
    // Specify the path to the Python executable
    $pythonPath = 'C:\\Users\\omers\\AppData\\Local\\Programs\\Python\\Python39\\python.exe';

    // Path to Python script
    $pythonScript = 'var/scripts/send_email.py';

    // Execute the Python script using command-line arguments
    $command = "\"$pythonPath\" \"$pythonScript\" " . escapeshellarg($recipient_email) . " " . escapeshellarg($password);

    $output = shell_exec($command);
}

这是我的python脚本:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys  # Import the sys module to access command-line arguments

# Set email credentials
sender_email = "xxxxxxxxx"
sender_password = "xxxxxxxxx"

# Accept the recipient email and password as command-line arguments
if len(sys.argv) >= 3:
    recipient_email = sys.argv[1]
    recipient_password = sys.argv[2]
else:
    recipient_email = None
    recipient_password = None

if recipient_email:
    # Create the email message
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = recipient_email
    message["Subject"] = "Credentials for MS Thesis Management System https://xxxxxxxx.com"

# Email body with password and additional message
email_body = f"Hello, {recipient_email}\n\n" \
             f"Please visit https://xxxxxxx.com, login with your NU email, " \
             f"and the password shared in this email: {recipient_password}"
message.attach(MIMEText(email_body, "plain"))

# Connect to the SMTP server
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, sender_password)

# Send the email
server.sendmail(sender_email, recipient_email, message.as_string())

# Disconnect from the server
server.quit()

我对在我的 PHP CodeIgniter 应用程序中将什么添加为 cpanel 文件管理器目录上的 Python 路径感到困惑,我找不到要包含的 Python 路径,如下所示:

$pythonPath = 'C:\Users\omers\AppData\Local\Programs\Python\Python39\python.exe';

python php codeigniter cpanel

评论

0赞 Don't Panic 10/30/2023
你的问题很难理解,但我想你是在问 Python 在你的 CPanel 服务器中的什么位置?我搜索了“cpanel python path”,发现了很多可以尝试的东西(示例),你试过了吗?例如?无论如何,这似乎是您的托管服务提供商的问题,而不是 SO。/usr/bin/python

答: 暂无答案