0

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
  • did you test to send before editing this file with python? – obayhan Nov 30 '16 at 08:24
  • Yes I have. I have tested by manually entering the information without the code at all and it sends the email as it is supposed to. – Anthony Branda Nov 30 '16 at 08:25
  • And after writing to the file with python messages do not send. All it takes to become functional is to open the file manually with nano save it and close it again with no edits and messages send again as they should – Anthony Branda Nov 30 '16 at 08:27
  • maybe something wrong about encoding. Which python version do you use? – obayhan Nov 30 '16 at 08:30
  • I am using python 3.2.3 – Anthony Branda Nov 30 '16 at 08:30
  • Then the problem is in different place. May you upload somewhere both two file before and after python edited ? (please with dummy mail adress and password ) or may you post the difference as hex? – obayhan Nov 30 '16 at 08:34
  • there is the program and ill get the files before and after in just a minute. – Anthony Branda Nov 30 '16 at 08:48
  • may you try file.write("UseSTARTTLS=YES") without "\n"? – obayhan Nov 30 '16 at 08:51
  • Oh I'm sorry that was actually just a typo. As I said I am doing this on a raspberry pi so I quickly just retyped the code into the question. The \n is not in the program. – Anthony Branda Nov 30 '16 at 08:58
  • Ok well still not sure why my original program does not work. But for now my solution is this: after writing to the file I instruct the user to verify their email and password preform os.system("nano /etc/ssmtp/ssmtp.conf") and in comments in the ssmtp.conf I tell them to ctrl-o and ctrl-x to complete. And this sends the message as the program should. – Anthony Branda Nov 30 '16 at 09:27
  • This is a workaround. Not a solution. If you share the sample files before and after you save with nano maybe someone could help. Because obviously the file that you saved with python and saved by nano is different. – obayhan Nov 30 '16 at 09:31

0 Answers0