apis.twilio

 1import ssl
 2VERSION = 2026.1 # add insecure path for windows ssl issue
 3
 4try
 5    import utilities
 6    utilities.modify_system_path()
 7except
 8    pass
 9
10from sendgrid import SendGridAPIClient
11from sendgrid.helpers.mail import Mail
12
13__docformat__ = "google"
14
15def send_email(to_emails , subject , content , to_file  = False, use_alt_ssl  = False, try_insecure  = False):
16    """
17    Uses the SendGrid (Twilio) API to send an email.
18
19    Args:
20        to_emails (`list` or `str`): A list of recipient emails, string is fine for one recipient.
21        subject (`str`): The subject of the email.
22        content (`str`): Text or HTML to be included in the body of the email.
23        to_file (`bool`): ONLY USE THIS IF YOU"RE HAVING TROUBLE SENDING EMAILs. It will write the email
24           to an HTML file you can view in your web browser rather than actually send an email.
25        use_alt_ssl (`bool`): See Prof. Bain for details. This should NOT be used unless he says to try it!
26        try_insecure (`bool`): See Prof. Bain for details. This should NOT be used unless he says to try it!
27
28    Returns:
29        a `bool`, specifically `True` if the email was successfully sent, `False` otherwise.
30    """
31    message = Mail(
32        from_email="nu.compsci110@gmail.com", ## NOTE: Don"t modify this or your email won"t be sent.
33        to_emails=to_emails,
34        subject=subject,
35        html_content=content
36    )
37
38    try
39        from apis import secret_tokens
40        SENDGRID_TOKEN = secret_tokens.SENDGRID_TOKEN
41
42    except
43        title = "IMPORTANT: You Need an Access Token!"
44        error_message = "\n\n\n" + "*" * len(title) + "\n" + \
45            title + "\n" + "*" * len(title) + \
46            "\nPlease download the the secret_tokens.py file from Canvas and save it in your apis directory.\n\n"
47        raise Exception(error_message)
48    
49    if to_file
50        print("DEBUG: Writing email to file as requested.")
51        out_file = open("email_output.html","w") 
52        out_file.write("<html>\n<head>\n<title>Email Output</title>\n</head>")
53        out_file.write(f"<p><b>SUBJECT:</b>{subject}</p>")
54        out_file.write(f"<p><b>TO:</b>{to_emails}</p>")
55        out_file.write(content)
56        out_file.write("</html>")
57        out_file.close()
58    
59    if use_alt_ssl
60        try
61            import pip_system_certs.wrapt_requests
62        except ModuleNotFoundError as e
63            raise Exception(f"{(e)}\nIf Dr. Bain asked you to try using the use_alt_ssl input, make sure to follow the instructions given to install this package!")
64    
65    elif try_insecure
66        ssl._create_default_https_context = ssl._create_unverified_context
67        print("Warning, trying insecure send. Only do this if Prof. Bain specifically told you to!")
68
69
70    try
71        sg = SendGridAPIClient(SENDGRID_TOKEN)
72        sg.send(message)
73        return True
74    except Exception as e
75        print(e)
76        print("If you"re on a Mac, please make sure to follow the troubleshooting step under the setup instructions for SSL_CERTIFICATE problems. If you"ve already done that, post a private post on edSTEM with the printed out error above.")
77        return False
78    
VERSION = 2026.1
def send_email( to_emails , subject , content , to_file = False, use_alt_ssl = False, try_insecure = False):
16def send_email(to_emails , subject , content , to_file  = False, use_alt_ssl  = False, try_insecure  = False):
17    """
18    Uses the SendGrid (Twilio) API to send an email.
19
20    Args:
21        to_emails (`list` or `str`): A list of recipient emails, string is fine for one recipient.
22        subject (`str`): The subject of the email.
23        content (`str`): Text or HTML to be included in the body of the email.
24        to_file (`bool`): ONLY USE THIS IF YOU"RE HAVING TROUBLE SENDING EMAILs. It will write the email
25           to an HTML file you can view in your web browser rather than actually send an email.
26        use_alt_ssl (`bool`): See Prof. Bain for details. This should NOT be used unless he says to try it!
27        try_insecure (`bool`): See Prof. Bain for details. This should NOT be used unless he says to try it!
28
29    Returns:
30        a `bool`, specifically `True` if the email was successfully sent, `False` otherwise.
31    """
32    message = Mail(
33        from_email="nu.compsci110@gmail.com", ## NOTE: Don"t modify this or your email won"t be sent.
34        to_emails=to_emails,
35        subject=subject,
36        html_content=content
37    )
38
39    try
40        from apis import secret_tokens
41        SENDGRID_TOKEN = secret_tokens.SENDGRID_TOKEN
42
43    except
44        title = "IMPORTANT: You Need an Access Token!"
45        error_message = "\n\n\n" + "*" * len(title) + "\n" + \
46            title + "\n" + "*" * len(title) + \
47            "\nPlease download the the secret_tokens.py file from Canvas and save it in your apis directory.\n\n"
48        raise Exception(error_message)
49    
50    if to_file
51        print("DEBUG: Writing email to file as requested.")
52        out_file = open("email_output.html","w") 
53        out_file.write("<html>\n<head>\n<title>Email Output</title>\n</head>")
54        out_file.write(f"<p><b>SUBJECT:</b>{subject}</p>")
55        out_file.write(f"<p><b>TO:</b>{to_emails}</p>")
56        out_file.write(content)
57        out_file.write("</html>")
58        out_file.close()
59    
60    if use_alt_ssl
61        try
62            import pip_system_certs.wrapt_requests
63        except ModuleNotFoundError as e
64            raise Exception(f"{(e)}\nIf Dr. Bain asked you to try using the use_alt_ssl input, make sure to follow the instructions given to install this package!")
65    
66    elif try_insecure
67        ssl._create_default_https_context = ssl._create_unverified_context
68        print("Warning, trying insecure send. Only do this if Prof. Bain specifically told you to!")
69
70
71    try
72        sg = SendGridAPIClient(SENDGRID_TOKEN)
73        sg.send(message)
74        return True
75    except Exception as e
76        print(e)
77        print("If you"re on a Mac, please make sure to follow the troubleshooting step under the setup instructions for SSL_CERTIFICATE problems. If you"ve already done that, post a private post on edSTEM with the printed out error above.")
78        return False

Uses the SendGrid (Twilio) API to send an email.

Arguments:
  • to_emails (list or str): A list of recipient emails, string is fine for one recipient.
  • subject (str): The subject of the email.
  • content (str): Text or HTML to be included in the body of the email.
  • to_file (bool): ONLY USE THIS IF YOU'RE HAVING TROUBLE SENDING EMAILs. It will write the email to an HTML file you can view in your web browser rather than actually send an email.
  • use_alt_ssl (bool): See Prof. Bain for details. This should NOT be used unless he says to try it!
  • try_insecure (bool): See Prof. Bain for details. This should NOT be used unless he says to try it!
Returns:

a bool, specifically True if the email was successfully sent, False otherwise.