-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
73 lines (62 loc) · 2.55 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import smtplib
import ssl
import re
from os import getenv
import google.generativeai as genai
from email.mime.text import MIMEText
def main():
# GEMINI GENERATION
genai.configure(api_key=getenv('API_KEY'))
model = genai.GenerativeModel('gemini-1.5-flash')
subject = "Thought For The Day"
response = model.generate_content(f"Prompt: Give me only single line output. \"{subject}\"",)
message = f"Subject: {subject}\n\n{response.text}"
sender, passkey, receivers = inputFromFile("info.txt")
msg = MIMEText(message)
msg['From'] = "Your Friendly Reminder"
error = send_mail(sender, passkey, *receivers, message = message)
if error:
print("Failed to send email to the following addresses:")
for address in error:
print(address)
else:
print("Sent all mail successfully")
def inputFromFile(fileLink):
file = open(fileLink, "r")
sender_mail = ""
sender_passkey = ""
match = []
for line in file:
if "sender" in line:
if sender_mail == "":
sender_mail = re.search(r'"([^"]*)"', line).group(1)
else:
raise Exception("More than one sender email provided.\n Read \"readme.py\" for proper use instructions.")
elif "passkey" in line:
if sender_passkey == "":
sender_passkey = re.search(r'"([^"]*)"', line).group(1)
else:
raise Exception("More than one passkey provided.\n Read \"readme.md\" file for proper use instructions.")
elif "receiver" in line:
match.append(re.search(r'"([^"]*)"', line).group(1))
file.close()
return sender_mail, sender_passkey, match
def send_mail(sender_email, sender_passkey, *receiver_emails, message) -> list:
context = ssl.create_default_context()
error = []
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
try:
server.login(sender_email, sender_passkey)
except Exception as e:
print(f"{e}\nIncorrect passkey or email. Set up correctly as instructed in the 'readme.md' file.")
return receiver_emails
for receiver_mail in receiver_emails:
try:
server.sendmail(sender_email, receiver_mail, message)
print(f"Email sent successfully to {receiver_mail}!")
except Exception as e:
print(f"{e}\nFailed to send email to {receiver_mail}:")
error.append(receiver_mail)
return error
if __name__ == "__main__":
main()