Skip to content

Commit 612bb96

Browse files
committed
Added main script
1 parent d19cfd1 commit 612bb96

File tree

1 file changed

+76
-25
lines changed

1 file changed

+76
-25
lines changed

gmail_birthday_sender/main.py

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,95 @@
11
import yagmail
2+
import datetime
23
import schedule
34
import time
4-
import datetime
55
import os
6+
from typing import Optional
67

7-
# Input your information by chnaging string part from here---------------------
8-
9-
email = 'your_account@gmail.com'
108
# used for email body (e.g. sincerely <myname>)
11-
your_name = 'your_real_name'
12-
recipient_name = 'John Doe'
9+
your_name = "your_real_name"
10+
# Input your information by chnaging string part from here---------------------
11+
your_email = "your_account@gmail.com"
1312
# Your application password can be used only after 2-step verification
14-
password = 'your_gmailpassword'
13+
password = "your_gmailpassword"
14+
15+
# Enter recipient information below
1516

16-
# Recipient's birthday information
17-
today = datetime.date.today()
1817
birthday_list = {
1918
# Change John to recipient's name
2019
"John": {
2120
# Change john@example.com into real recipient's email
2221
"email": "john@example.com",
23-
# Change Month and Day of a birthday
24-
"birthday": datetime.date(today.year, 12, 31),
25-
"attachment": "/path/to/john_card.pdf" # Enter absolute path to a file if needed
26-
},
22+
# Change Year, Month and Day of a birthday
23+
"birthday": datetime.date(1995, 12, 31),
24+
# Enter an absolute path to attachement file
25+
"attachment": "/path/to/john_card.pdf",
26+
},
2727
"Jane": {
2828
"email": "jane@example.com",
29-
"birthday": datetime.date(today.year, 8, 22),
30-
"attachment": None # 添付ファイルなし
31-
},
32-
# Add other recipient from here
33-
}
34-
# If you want to change the content, modify below
35-
subject = f"Happy birthday {recipient_name}!"
36-
body = f"""Dear {recipient_name},
29+
"birthday": datetime.date(2001, 8, 22),
30+
"attachment": None, # 添付ファイルなし
31+
}, # Add other recipient from a below line
32+
}
33+
# -----------------------------------------------------------------------------
34+
35+
36+
class BirthdaySender:
37+
"""This automatically sends birthday wishes via Gmail."""
38+
39+
def __init__(self, your_name, your_email, password) -> None:
40+
self.your_name = your_name
41+
self.sender_email = your_email
42+
self.birthday_list = birthday_list
43+
# for safer password storage
44+
yagmail.register(your_email, password)
45+
self.yag = yagmail.SMTP(your_email)
46+
47+
def send_email(
48+
self, name: str, to_email: str, attachment_path: Optional[str] = None
49+
) -> None:
50+
"""Include attachement to email file if it is needed"""
51+
52+
# If you want to change the content, modify below----------------------
53+
subject = f"Happy birthday {name}!"
54+
body = f"""Dear {name},
55+
56+
Wishing you a very happy birthday filled with love, laughter, and joy!
57+
May all your dreams and aspirations come true.
58+
Looking forward to seeing you soon! Have a fantastic birthday!
59+
60+
Best wishes, {self.your_name}"""
61+
# ---------------------------------------------------------------------
62+
63+
email_params = {"to": to_email, "subject": subject, "contents": body}
64+
65+
if attachment_path and os.path.exists(attachment_path):
66+
email_params["attachments"] = attachment_path
67+
print(f"{attachment_path} was included")
68+
69+
try:
70+
self.yag.send(**email_params)
71+
print(f"Sent to {name}")
72+
except Exception as e:
73+
print(f"Failed to send email to {name}, Error: {e}")
74+
75+
def send_email_if_birthday(self) -> None:
76+
"""Call send_email if today is birthday"""
77+
today = datetime.date.today()
78+
79+
for name, info in self.birthday_list.items():
80+
birthday = info["birthday"]
81+
if today.month == birthday.month and today.day == birthday.day:
82+
return self.send_email(name, info["email"], info["attachment"])
83+
84+
def run(self):
85+
return self.send_email_if_birthday()
3786

38-
Wishing you a very happy birthday filled with love, laughter, and joy! May all your dreams and aspirations come true.
39-
Looking forward to seeing you soon! Have a fantastic birthday!
4087

41-
Best wishes, {your_name}"""
42-
# --------------------------------------------------------------------------^^^
88+
if __name__ == "__main__":
89+
birthday_sender = BirthdaySender(your_name, your_email, password)
4390

91+
schedule.every().day.at("07:00").do(birthday_sender.run)
4492

93+
while True:
94+
schedule.run_pending()
95+
time.sleep(1)

0 commit comments

Comments
 (0)