You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postfix转发邮件至Python脚本时出现Permission denied错误求助

Postfix调用Python脚本时Permission Denied错误的解决方案

你好!从你提供的日志和脚本内容来看,问题出在文件路径权限和Postfix执行脚本的上下文上,我来帮你梳理具体的解决步骤:

问题分析

从日志里的报错IOError: [Errno 13] Permission denied: 'file.txt'可以明确两个核心问题:

  1. 你的Python脚本使用了相对路径打开file.txt,但Postfix执行脚本时的工作目录并不是脚本所在的/home/pythonscript,而是Postfix的系统运行目录(比如/var/spool/postfix),Postfix的运行用户(通常是postfix用户)没有权限在这个系统目录下创建文件。
  2. 就算路径指向正确,postfix用户大概率也没有/home/pythonscript目录的写入权限。

你的报错日志:

Apr 29 12:56:26 mail postfix/smtpd[7740]: connect from mail-wr1-x429.google.com[2a00:1450:4864:20::429]
Apr 29 12:56:26 mail postfix/smtpd[7740]: 84F503FE97: client=mail-wr1-x429.google.com[2a00:1450:4864:20::429]
Apr 29 12:56:26 mail postfix/cleanup[7747]: 84F503FE97: message-id=<CAHDgzXAk54JCjKKVesC2YR+oiD48uT0dE191znr_wd+t1jCp7A@mail.gmail.com>
Apr 29 12:56:26 mail postfix/qmgr[1231]: 84F503FE97: from=<example@gmail.com>, size=2513, nrcpt=2 (queue active)
Apr 29 12:56:26 mail postfix/smtpd[7740]: disconnect from mail-wr1-x429.google.com[2a00:1450:4864:20::429] ehlo=2 starttls=1 mail=1 rcpt=1 data=1 quit=1 commands=7
Apr 29 12:56:26 mail postfix/local[7749]: 84F503FE97: to=<exampleUser@mydomain.xyz>, relay=local, delay=0.05, delays=0.04/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Apr 29 12:56:26 mail postfix/local[7750]: 84F503FE97: to=<pythonscript@localhost>, relay=local, delay=0.08, delays=0.04/0.01/0/0.04, dsn=5.3.0, status=bounced (Command died with status 1: "/usr/bin/python /home/pythonscript/pythonscript.py". Command output: Traceback (most recent call last): File "/home/pythonscript/pythonscript.py", line 1, in <module> f = open("file.txt", "w") IOError: [Errno 13] Permission denied: 'file.txt' )
Apr 29 12:56:26 mail postfix/qmgr[1231]: 84F503FE97: removed
Apr 29 12:56:26 mail postfix/cleanup[7747]: 9693A41B8D: message-id=<20200429125626.9693A41B8D@mail.example.xyz>

你的Python脚本:

f = open("file.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

解决步骤

1. 修改脚本为绝对路径操作文件

把脚本里的相对路径改成绝对路径,确保不管Postfix在哪个目录执行脚本,都能定位到正确的文件位置:

# 使用绝对路径指向脚本所在目录的file.txt
f = open("/home/pythonscript/file.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

2. 确保Postfix用户拥有目录和文件的权限

Postfix默认以postfix用户身份执行外部脚本,你需要给这个用户赋予目标目录的写入权限:

  • 先确认Postfix的运行用户:
    ps aux | grep postfix
    
  • 修改目录权限:
    sudo chown postfix:postfix /home/pythonscript
    sudo chmod 755 /home/pythonscript
    
  • 如果已经存在file.txt,也要修改它的权限:
    sudo chown postfix:postfix /home/pythonscript/file.txt
    sudo chmod 644 /home/pythonscript/file.txt
    

3. (可选)更规范的做法:使用专用目录

为了安全和符合Linux目录规范,建议创建一个专门用于邮件脚本的目录(比如/var/log/mailscripts/),然后给postfix用户权限:

sudo mkdir /var/log/mailscripts
sudo chown postfix:postfix /var/log/mailscripts
sudo chmod 755 /var/log/mailscripts

然后修改脚本里的路径为/var/log/mailscripts/file.txt即可。

完成以上步骤后,重新发送测试邮件,应该就能正常执行脚本不会报权限错误了。

内容的提问来源于stack exchange,提问作者dynamicrange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:27:28