@@ -36,15 +36,44 @@ def create_email_body(repo_name, stats):
36
36
"""
37
37
return body
38
38
39
- def send_email (recipient_email , subject , body ):
40
- """Send email using SendGrid"""
39
+ # def send_email(recipient_email, subject, body):
40
+ # """Send email using SendGrid"""
41
+ # sg = SendGridAPIClient(os.environ['SENDGRID_API_KEY'])
42
+
43
+ # from_email = Email(os.environ['SENDER_EMAIL'])
44
+ # to_email = To(recipient_email)
45
+ # content = Content("text/plain", body)
46
+
47
+ # mail = Mail(from_email, to_email, subject, content)
48
+
49
+ # try:
50
+ # response = sg.client.mail.send.post(request_body=mail.get())
51
+ # print(f"Email sent successfully. Status code: {response.status_code}")
52
+ # except Exception as e:
53
+ # print(f"Error sending email: {e}")
54
+
55
+ def send_email (recipient_emails , subject , body ):
56
+ """Send email using SendGrid to multiple recipients"""
41
57
sg = SendGridAPIClient (os .environ ['SENDGRID_API_KEY' ])
42
58
43
59
from_email = Email (os .environ ['SENDER_EMAIL' ])
44
- to_email = To (recipient_email )
45
- content = Content ("text/plain" , body )
46
60
47
- mail = Mail (from_email , to_email , subject , content )
61
+ # Convert string to list if a single string is provided
62
+ if isinstance (recipient_emails , str ):
63
+ recipient_emails = [recipient_emails ]
64
+
65
+ # Create personalization object with multiple recipients
66
+ mail = Mail (
67
+ from_email = from_email ,
68
+ subject = subject ,
69
+ )
70
+
71
+ personalization = Personalization ()
72
+ for email in recipient_emails :
73
+ personalization .add_to (To (email ))
74
+
75
+ mail .add_personalization (personalization )
76
+ mail .add_content (Content ("text/plain" , body ))
48
77
49
78
try :
50
79
response = sg .client .mail .send .post (request_body = mail .get ())
0 commit comments