Skip to content

Commit ae3c7c7

Browse files
committed
Add image generator
1 parent 166db86 commit ae3c7c7

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

automate.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from scrape import scraper
22
from file_utils import read_csv, write_csv, read_file, write_file
33
from generate import _render_template, preprocess
4+
from image import pass_gen
45
from mail import sendmail
56
import json
67

@@ -12,17 +13,20 @@
1213
participants = read_csv()
1314
participants = preprocess(participants)
1415

15-
# Getting the list of mails to whom mails have already been sent
16+
# Getting the list of mails to whom mails have already been sent
1617
sent_mails = read_file()
1718

1819
# Looping over all participants
1920
for participant in participants:
2021
# Checking if the participant was sent a mail previously
2122
if participant['email'] not in sent_mails:
22-
# Generating a message from the template
23+
# Generating a message from the template
2324
msg = _render_template(participant['name'], participant['payment'])
25+
# Generating a custom image
26+
image_path = pass_gen(participant['name'], participant['email'], participant['phone'])
2427
# Sending the message to the participant via mail
25-
response = sendmail(to_email=participant['email'], msg=msg)
26-
if response['email_status'] == "Success":
28+
response = sendmail(to_email=participant['email'], message=msg, image_path=image_path)
29+
print(response)
30+
if response['email_status'] == "Success" and participant['payment'] == "paid":
2731
# if mail was sent successfully append the email to sentmails.txt
2832
write_file(participant['email'])

image.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from PIL import Image,ImageDraw,ImageFont
2+
import string, random
3+
4+
def pass_gen(name, email, phone):
5+
img = Image.open("templates/pass.png")
6+
draw = ImageDraw.Draw(img)
7+
font = ImageFont.truetype("templates/Quicksand-Bold.otf", 40)
8+
9+
s_path = "passes/" + email + ".png"
10+
if len(name) >= 18:
11+
name = name.split()
12+
name = name[0] + " " + name[1][0]
13+
14+
draw.text((200, 300), name.capitalize(), font=font, fill=(255, 255, 255))
15+
draw.text((650, 300), phone, font=font, fill=(255, 255, 255))
16+
draw.text((200, 500), email.lower(), font=font, fill=(255, 255, 255))
17+
18+
img.save(s_path)
19+
return s_path

mail.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
from email.mime.text import MIMEText
22
from email.mime.multipart import MIMEMultipart
3+
from email.mime.image import MIMEImage
34
import smtplib
45
from flask import render_template
56
import os
67
import sys
78

8-
def sendmail(to_email, msg):
9-
10-
from_email = ''
9+
def sendmail(to_email, message, image_path):
10+
11+
from_email = ""
1112
msg = MIMEMultipart('alternative')
1213
msg['Subject'] = "Python Automation Workstop"
1314
msg['From'] = from_email
1415
msg['To'] = to_email
15-
16-
content = msg
16+
17+
content = MIMEText(message, 'plain')
1718
msg.attach(content)
19+
20+
# This example assumes the image is in the given directory
21+
fp = open(image_path, 'rb')
22+
msgImage = MIMEImage(fp.read())
23+
fp.close()
24+
msg.attach(msgImage)
25+
1826
response = {}
1927
try:
2028
with smtplib.SMTP("smtp.gmail.com", 587) as s:

scrape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def scraper(url):
1010
data = []
1111

1212
# converting the html string to soup object
13-
soup = bs4.BeautifulSoup(html)
13+
soup = bs4.BeautifulSoup(html, "html.parser")
1414
li_tags = soup.select('li')
1515

1616
for i in range(len(li_tags)):

templates/Quicksand-Bold.otf

28.6 KB
Binary file not shown.

templates/pass.png

38.4 KB
Loading

0 commit comments

Comments
 (0)