|
1 | 1 | import yagmail
|
| 2 | +import datetime |
2 | 3 | import schedule
|
3 | 4 | import time
|
4 |
| -import datetime |
5 | 5 | import os
|
| 6 | +from typing import Optional |
6 | 7 |
|
7 |
| -# Input your information by chnaging string part from here--------------------- |
8 |
| - |
9 |
| -email = 'your_account@gmail.com' |
10 | 8 | # 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" |
13 | 12 | # 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 |
15 | 16 |
|
16 |
| -# Recipient's birthday information |
17 |
| -today = datetime.date.today() |
18 | 17 | birthday_list = {
|
19 | 18 | # Change John to recipient's name
|
20 | 19 | "John": {
|
21 | 20 | # Change john@example.com into real recipient's email
|
22 | 21 | "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 | + }, |
27 | 27 | "Jane": {
|
28 | 28 | "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() |
37 | 86 |
|
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! |
40 | 87 |
|
41 |
| -Best wishes, {your_name}""" |
42 |
| -# --------------------------------------------------------------------------^^^ |
| 88 | +if __name__ == "__main__": |
| 89 | + birthday_sender = BirthdaySender(your_name, your_email, password) |
43 | 90 |
|
| 91 | + schedule.every().day.at("07:00").do(birthday_sender.run) |
44 | 92 |
|
| 93 | + while True: |
| 94 | + schedule.run_pending() |
| 95 | + time.sleep(1) |
0 commit comments