hawkesley
Posts: 126
Joined: Tue Jan 12, 2016 10:16 am

email atachment

Tue Aug 13, 2019 9:46 am

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")


pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email atachment

Tue Aug 13, 2019 10:04 am

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.
Last edited by pcmanbob on Tue Aug 13, 2019 10:11 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: email atachment

Tue Aug 13, 2019 10:10 am

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.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

hawkesley
Posts: 126
Joined: Tue Jan 12, 2016 10:16 am

Re: email atachment

Tue Aug 13, 2019 10:26 am

Yes that is the problem so my question is how to add the variable latest contents to start_path?

pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email atachment

Tue Aug 13, 2019 10:47 am

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
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hawkesley
Posts: 126
Joined: Tue Jan 12, 2016 10:16 am

Re: email atachment

Tue Aug 13, 2019 11:06 am

Thanks for that the script now runs as intended.

Return to “Advanced users”