My python program is sort of a setup manager for using ssmtp
to send emails.
so prior to running the program I get the necessary installs:
sudo apt-get install ssmtp
sudo apt-get install mailutils
sudo apt-get install mpack
After installing those three things the next step in sending emails is to edit the file /etc/ssmtp/ssmtp.conf
and add your email address and password along with some other pieces of information:
root=postmaster
hostname=raspberrypi
AuthUser=emailaddress@gmail.com
AuthPass=password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
And this is where the complications start. I know this configuration works and I can test and prove it. but in my python code I open the file "/etc/ssmtp/ssmtp.conf" by doing:
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.
(My reason for doing this is for the program to act as an install manager of sorts where it asks the user to enter their email address and password and then it writes it in to the correct part of ssmtp.conf)
After writing to the file I close:
file.close()
With that being completed, I should be ready to send an email. In order to send an email you run this command:
echo "Hello Phone!" | mail myphonenumber@vtext.com
But this does not work. I get an error that says:
mail: cannot send message: Process exited with a non-zero status
In trying to figure out what was wrong I determined that if you open the file using:
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
And it sends the email to my phone no problem...
I do not understand why this doesn't work.
Thanks to all that answer in advance!
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 before running the program (I am leaving out all the comments):
root=postmaster
mailhub=mail
hostname=raspberrypi
ssmtp.conf after running the program:
root=postmaster
hostname=raspberrypi
AuthUser=myemailaddress@gmail.com
AuthPass=mypassword
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES