Skip to content

Gmail birthday sender #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions gmail_birthday_sender/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Gmail Birthday Sender

This script automatically sends birthday wishes via Gmail.
You just need to enter your information at beginning.

## Requirements

- Python 3

## Instructions

1. Create google account
2. Turn on [2-Step Verification](https://support.google.com/accounts/answer/185839)
3. Create [an application password](https://support.google.com/accounts/answer/185833#zippy=%2Cremove-app-passwords)

### Enter your information in `main.py`

- Enter your gmail account, your name and application password

```py
your_name = "your_real_name"
your_email = "[email protected]"
password = "your_gmailpassword"
```

- Enter recipient's information here

```py
# Change John to recipient's name
"John": {
# Change [email protected] into real recipient's email
"email": "[email protected]",
# Change Month and Day of a birthday
"birthday": datetime.date(1995, 12, 31),
```

(Example)
Name: John
Email: [email protected],
Year, Month and Day of a birthday: 1995, 12, 31

- Enter absolute path to a attachement file if you want

```py
"attachment": "/path/to/john_card.pdf"
},
```

If you don't need, write `None` instead

```py
"attachment": None
```

- If you want to change the subject and body of email, modify below string

```py
subject = f"Happy birthday {name}!"
body = f"""Dear {name},

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

Best wishes, {self.your_name}"""
```

### Run the script

After you're done installing Python and pip, run the following command from your terminal to install the requirements from the same folder (directory) of the project.

```bash
pip install -r requirements.txt
```

After satisfying all the requirements for the project, Open the terminal in the project folder and run

```bash
python dictionary.py
```

or

```bash
python3 dictionary.py
```

> [!IMPORTANT]
> Script should be run anytime to send birthday wishes automatically

## Output

![Example of output](example_of_birthday_sender.png)

## Disclaimers, if any

> [!WARNING]
> This script lacks strong security for your email
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions gmail_birthday_sender/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import yagmail
import datetime
import schedule
import time
import os
from typing import Optional

# Input your information by chnaging string part from here---------------------
# used for email body (e.g. sincerely <myname>)
your_name = "your_real_name"
your_email = "[email protected]"
# Your application password can be used only after 2-step verification
password = "your_gmailpassword"

# Enter recipient information below

birthday_list = {
# Change John to recipient's name
"John": {
# Change [email protected] into real recipient's email
"email": "[email protected]",
# Change Year, Month and Day of a birthday
"birthday": datetime.date(1995, 12, 31),
# Enter an absolute path to attachement file
"attachment": "/path/to/john_card.pdf",
},
"Jane": {
"email": "[email protected]",
"birthday": datetime.date(2001, 8, 22),
"attachment": None, # 添付ファイルなし
}, # Add other recipient from a below line
}
# -----------------------------------------------------------------------------


class BirthdaySender:
"""This automatically sends birthday wishes via Gmail."""

def __init__(self, your_name, your_email, password) -> None:
self.your_name = your_name
self.sender_email = your_email
self.birthday_list = birthday_list
# for safer password storage
yagmail.register(your_email, password)
self.yag = yagmail.SMTP(your_email)

def send_email(
self, name: str, to_email: str, attachment_path: Optional[str] = None
) -> None:
"""Include attachement to email file if it is needed"""

# If you want to change the content, modify below----------------------
subject = f"Happy birthday {name}!"
body = f"""Dear {name},

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

Best wishes, {self.your_name}"""
# ---------------------------------------------------------------------

email_params = {"to": to_email, "subject": subject, "contents": body}

if attachment_path and os.path.exists(attachment_path):
email_params["attachments"] = attachment_path
print(f"{attachment_path} was included")

try:
self.yag.send(**email_params)
print(f"Sent to {name}")
except Exception as e:
print(f"Failed to send email to {name}, Error: {e}")

def send_email_if_birthday(self) -> None:
"""Call send_email if today is birthday"""
today = datetime.date.today()

for name, info in self.birthday_list.items():
birthday = info["birthday"]
if today.month == birthday.month and today.day == birthday.day:
return self.send_email(name, info["email"], info["attachment"])

def run(self):
return self.send_email_if_birthday()


if __name__ == "__main__":
birthday_sender = BirthdaySender(your_name, your_email, password)

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

try:
while True:
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
print("Birthday sending stopped")
except Exception as e:
print(f"An error: {e}")
21 changes: 21 additions & 0 deletions gmail_birthday_sender/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cachetools==5.5.0
certifi==2024.8.30
charset-normalizer==3.3.2
cssselect==1.2.0
cssutils==2.11.1
flake8==7.1.1
idna==3.8
jaraco.classes==3.4.0
jaraco.context==6.0.1
jaraco.functools==4.0.2
keyring==25.4.1
lxml==5.3.0
mccabe==0.7.0
more-itertools==10.5.0
premailer==3.10.0
pycodestyle==2.12.1
pyflakes==3.2.0
requests==2.32.3
schedule==1.2.2
urllib3==2.2.2
yagmail==0.15.293
Loading