python文件.close()未关闭ssmtp.conf文件(我想?)

2024-10-05 14:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python程序是一种设置管理器,用于使用ssmtp发送电子邮件。你知道吗

因此,在运行程序之前,我会进行必要的安装:

sudo apt-get install ssmtp
sudo apt-get install mailutils
sudo apt-get install mpack

在安装了这三件事之后,发送电子邮件的下一步是编辑文件/etc/ssmtp/ssmtp.conf,并添加您的电子邮件地址和密码以及其他一些信息:

root=postmaster
hostname=raspberrypi
AuthUser=emailaddress@gmail.com
AuthPass=password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

这就是并发症的开始。我知道这个配置工作,我可以测试和证明它。但是在python代码中,我打开了文件“/etc/ssmtp”/ssmtp.conf文件“通过这样做:

file = open("/etc/ssmtp/ssmtp.conf", "w")

file.write("root=postmaster\n")
file.write("hostname=raspberrypi\n")
file.write()... and so on for the previously defined values.

(我这样做的原因是程序作为一个安装管理器,它要求用户输入他们的电子邮件地址和密码,然后将其写入到正确的部分。)ssmtp.conf文件)你知道吗

写入文件后,我关闭:

file.close()

完成后,我应该准备好发送电子邮件了。要发送电子邮件,请运行以下命令:

echo "Hello Phone!" | mail myphonenumber@vtext.com

但这行不通。我得到一个错误,上面写着:

mail: cannot send message: Process exited with a non-zero status

在试图找出问题所在的过程中,我确定如果您使用以下方法打开文件:

sudo nano /etc/ssmtp/ssmtp.conf
-ctrl-o to write the file
-ctrl-x to close the file
--repeat the command echo Hello Phone! | mail 1231231234@vtext.com

它把邮件发到我的手机没问题。。。你知道吗

我不明白为什么这样不行。你知道吗

感谢所有的答案提前!你知道吗

import os as os

os.system("apt-get update -y")

os.system("apt-get install ssmtp -y")
os.system("apt-get install mailutils -y")
os.system("apt-get install mpack -y")

os.system("clear")

email = input("Enter your gmail address: ")
password = input("Enter your gmail password: ")
phone = input("Enter the phone number to recieve emails to: ")

file = open("/etc/ssmtp/ssmtp.conf", "w")

file.write("root=postmaster\n")
file.write("hostname=raspberrypi\n")
file.write("AuthUser=" + email + "\n")
file.write("AuthPass=" + password + "\n")
file.write("FromLineOverride=YES\n")
file.write("mailhub=smtp.gmail.com:587\n")
file.write("UseSTARTTLS=YES")

file.close()

os.system("echo Hello Phone! | mail " + phone + "@vtext.com")

你知道吗ssmtp.conf文件在运行程序之前(我将省略所有注释):

root=postmaster
mailhub=mail
hostname=raspberrypi

你知道吗ssmtp.conf文件运行程序后:

root=postmaster
hostname=raspberrypi
AuthUser=myemailaddress@gmail.com
AuthPass=mypassword
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

Tags: install文件程序comgetos电子邮件conf