Skip to content
Snippets Groups Projects
Commit 190d439a authored by 이종현's avatar 이종현
Browse files

Update send_email.py

parent 67c8523f
No related branches found
No related tags found
No related merge requests found
import os
import smtplib import smtplib
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.image import MIMEImage from email.mime.image import MIMEImage
sender_email = "your_email@gmail.com" sender_email = "0819ljh@naver.com"
sender_password = "your_password" sender_password = "" //비밀번호는 생략하였습니다.
recipient_email = "recipient_email@example.com" recipient_email = "day305@naver.com"
smtp_server = "smtp.gmail.com" smtp_server = "smtp.naver.com"
smtp_port = 587 smtp_port = 587
image_dir = "path/to/your/image/directory"
image_files = [f for f in os.listdir(image_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
latest_image = max(image_files, key=lambda x: os.path.getmtime(os.path.join(image_dir, x)))
image_path = os.path.join(image_dir, latest_image)
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = sender_email msg['From'] = sender_email
msg['To'] = recipient_email msg['To'] = recipient_email
msg['Subject'] = "이메일 제목" msg['Subject'] = "OO지역에 흡연 감지됨됨"
body = "이메일 본문 내용" body = "OO지역에 흡연이 감지되었습니다. 지속적인흡연이감지되면 관리자는 확인부탁드리겠습니다."
msg.attach(MIMEText(body, 'plain')) msg.attach(MIMEText(body, 'plain'))
with open(image_path, "rb") as image_file: with open("/home/pi/cat.jpg", "rb") as image_file:
image = MIMEImage(image_file.read()) image = MIMEImage(image_file.read())
msg.attach(image) msg.attach(image)
...@@ -36,5 +27,3 @@ with smtplib.SMTP(smtp_server, smtp_port) as server: ...@@ -36,5 +27,3 @@ with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() server.starttls()
server.login(sender_email, sender_password) server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string()) server.sendmail(sender_email, recipient_email, msg.as_string())
print(f"최근 이미지 '{latest_image}'를 이메일로 전송했습니다.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment