Page 1 of 1

email atachment

Posted: Tue Aug 13, 2019 9:46 am
by hawkesley
i want to send an email where the attachment will vary.
I am trying to use a variable to hold the file name and add that to directory path.
This fails with the error
Traceback (most recent call last):
File "/home/pi/email scripts/tony.sh", line 47, in <module>
attachment =open(filename,'rb')
TypeError: invalid file: PosixPath('/home/pi/Pictures/latest')
>>>
any suggestions appreciated.
This is the code in question

Code: Select all

path = "/home/pi/Pictures/"

files =os.listdir(path)
latest = files[0]
for key in files:
    if os.path.getctime(path+key) > os.path.getctime(path + latest):
        latest = key

print(latest)
filename = latest

start_path = Path('/home/pi/Pictures')
final_path = start_path /'filename'

filename= final_path
attachment  =open(filename,'rb')
This is the full script

Code: Select all

#!/bin/bash
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os
import os.path
from pathlib import Path

print("preparing to mail")

email_user = 'your email'
email_password = 'password'
email_send = 'send to email'

subject = 'send mail test'

print("preparing to mail")

msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject

body = 'Hi there, sending this email from Tony,sent from photo module!'
msg.attach(MIMEText(body,'plain'))

print("preparing to mail")

path = "/home/pi/Pictures/"

files =os.listdir(path)
latest = files[0]
for key in files:
    if os.path.getctime(path+key) > os.path.getctime(path + latest):
        latest = key

print(latest)
filename = latest

start_path = Path('/home/pi/Pictures')
final_path = start_path /'filename'

filename= final_path
attachment  =open(filename,'rb')

part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)

msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)

print("preparing to mail")

server.sendmail(email_user,email_send,text)
print("preparing to mail")
server.quit()

print("sending email from tony")


Re: email atachment

Posted: Tue Aug 13, 2019 10:04 am
by pcmanbob
Looking at the error message it would appear that your program can't find a file called latest which it is expecting to find in the directory
/home/pi/Pictures

So is your file called latest ? I suspect not.

Try adding a print statement before the error line and have it print the contents of the filename variable so you can see what it contains.

And you should not have #!/bin/bash as the first line in your program as this is a python program not a bash script.

Re: email atachment

Posted: Tue Aug 13, 2019 10:10 am
by scotty101
Firstly you can get the most recently updated file in a single line.

Code: Select all

latest = max(glob.iglob('Results\*.txt'), key=os.path.getctime)
Secondly look at this line of code

Code: Select all

final_path = start_path /'filename'
You are creating the final path to include the start_path and the string "filename" not the contents of the variable filename.
I suspect this will be causing some issues for you.

Re: email atachment

Posted: Tue Aug 13, 2019 10:26 am
by hawkesley
Yes that is the problem so my question is how to add the variable latest contents to start_path?

Re: email atachment

Posted: Tue Aug 13, 2019 10:47 am
by pcmanbob
Assuming the path will never change

filename = "/home/pi/Pictures/" + str(latest)

which would mean these lines are not needed.


start_path = Path('/home/pi/Pictures')
final_path = start_path /'filename'

filename= final_path

Re: email atachment

Posted: Tue Aug 13, 2019 11:06 am
by hawkesley
Thanks for that the script now runs as intended.