0

我的 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

但这不起作用。我收到一条错误消息:

邮件:无法发送消息:进程以非零状态退出

在试图找出问题所在时,我确定如果您使用以下命令打开文件:

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
4

0 回答 0