apis.twilio

 1try
 2    import utilities
 3    utilities.modify_system_path()
 4except
 5    pass
 6
 7from sendgrid import SendGridAPIClient
 8from sendgrid.helpers.mail import Mail
 9
10__docformat__ = "google"
11
12def send_email(to_emails , subject , content , to_file  = False):
13    """
14    Uses the SendGrid (Twilio) API to send an email.
15
16    Args:
17        to_emails (`list` or `str`): A list of recipient emails, string is fine for one recipient.
18        subject (`str`): The subject of the email.
19        content (`str`): Text or HTML to be included in the body of the email.
20        to_file (`str`): ONLY USE THIS IF YOU"RE HAVING TROUBLE SENDING EMAILs. It will write the email
21           to an HTML file you can view in your web browser rather than actully send an email.
22
23    Returns:
24        a `bool`, specifically `True` if the email was successfully sent, `False` otherwise.
25    """
26    message = Mail(
27        from_email="nu.compsci110@gmail.com", ## NOTE: Don"t modify this or your email won"t be sent.
28        to_emails=to_emails,
29        subject=subject,
30        html_content=content
31    )
32
33    try
34        from apis import secret_tokens
35        SENDGRID_TOKEN = secret_tokens.SENDGRID_TOKEN
36
37    except
38        title = "IMPORTANT: You Need an Access Token!"
39        error_message = "\n\n\n" + "*" * len(title) + "\n" + \
40            title + "\n" + "*" * len(title) + \
41            "\nPlease download the the secret_tokens.py file from Canvas and save it in your apis directory.\n\n"
42        raise Exception(error_message)
43    
44    if to_file
45        print("DEBUG: Writing email to file as requested.")
46        out_file = open("email_output.html","w") 
47        out_file.write("<html>\n<head>\n<title>Email Output</title>\n</head>")
48        out_file.write(f"<p><b>SUBJECT:</b>{subject}</p>")
49        out_file.write(f"<p><b>TO:</b>{to_emails}</p>")
50        out_file.write(content)
51        out_file.write("</html>")
52        out_file.close()
53
54    
55    try
56        sg = SendGridAPIClient(SENDGRID_TOKEN)
57        sg.send(message)
58        return True
59    except Exception as e
60        print(e)
61        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.")
62        return False
63    
def send_email(to_emails, subject, content, to_file = False):
13def send_email(to_emails , subject , content , to_file  = False):
14    """
15    Uses the SendGrid (Twilio) API to send an email.
16
17    Args:
18        to_emails (`list` or `str`): A list of recipient emails, string is fine for one recipient.
19        subject (`str`): The subject of the email.
20        content (`str`): Text or HTML to be included in the body of the email.
21        to_file (`str`): ONLY USE THIS IF YOU"RE HAVING TROUBLE SENDING EMAILs. It will write the email
22           to an HTML file you can view in your web browser rather than actully send an email.
23
24    Returns:
25        a `bool`, specifically `True` if the email was successfully sent, `False` otherwise.
26    """
27    message = Mail(
28        from_email="nu.compsci110@gmail.com", ## NOTE: Don"t modify this or your email won"t be sent.
29        to_emails=to_emails,
30        subject=subject,
31        html_content=content
32    )
33
34    try
35        from apis import secret_tokens
36        SENDGRID_TOKEN = secret_tokens.SENDGRID_TOKEN
37
38    except
39        title = "IMPORTANT: You Need an Access Token!"
40        error_message = "\n\n\n" + "*" * len(title) + "\n" + \
41            title + "\n" + "*" * len(title) + \
42            "\nPlease download the the secret_tokens.py file from Canvas and save it in your apis directory.\n\n"
43        raise Exception(error_message)
44    
45    if to_file
46        print("DEBUG: Writing email to file as requested.")
47        out_file = open("email_output.html","w") 
48        out_file.write("<html>\n<head>\n<title>Email Output</title>\n</head>")
49        out_file.write(f"<p><b>SUBJECT:</b>{subject}</p>")
50        out_file.write(f"<p><b>TO:</b>{to_emails}</p>")
51        out_file.write(content)
52        out_file.write("</html>")
53        out_file.close()
54
55    
56    try
57        sg = SendGridAPIClient(SENDGRID_TOKEN)
58        sg.send(message)
59        return True
60    except Exception as e
61        print(e)
62        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.")
63        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 (str): 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 actully send an email.
Returns:

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