Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Unable to set a variable for a python script that sends an attachment from a security camera to my email address on my RPi3B+

$
0
0

this is my first post on stack overflow. I want to get into programming and I have tasked myself with starting with easy projects. My first project is to use my IR motion detecting sensor and a webcam both attached to my RPi3B+ to function as a security camera system (just for fun). I've got a lot of the code I needed to make it work from various websites. My problem now that I've encountered is that I don't know how to modify the name of the snapshot taken from the webcam to be a variable showing the date and time the shot was taken and then email the snapshot with that variable name to my email address. Below is example code that shows what I already kind of have and where I am stuck and need help. It is the part where it says "filename = " and "attachment = open()" where I am stuck. See below (didn't put my own code in because of the presence of a lot of personal data and I think this problem can be solved without including all of that).

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

from = "sender email address"

to = "receiver email address"

# instance of MIMEMultipart

data = MIMEMultipart()

# storing the senders email address  

data['From'] = from

# storing the receivers email address 

data['To'] = to

# storing the subject 

data['Subject'] = "Subject of the Mail"

# string to store the body of the mail

body = "Body-of-the-mail"

# attach the body with the msg instance

data.attach(MIMEText(body, 'plain'))

# open the file to be sent 

**filename = "File-name-with-extension"
attachment = open("Path of the file", "rb")**

# instance of MIMEBase and named as p

p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form

p.set_payload((attachment).read())

# encode into base64

encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# attach the instance 'p' to instance 'msg'

data.attach(p)

# creates SMTP session

s = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security

s.starttls()

# Authentication

s.login(from, "Password-of-the-sender")

# Converts the Multipart msg into a string

text = data.as_string()

# sending the mail

s.sendmail(from, to, text)

# terminating the session

s.quit()

Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>