Class SendMessage

java.lang.Object
edu.suffolk.litlab.efspserver.SendMessage

public class SendMessage extends Object
  • Method Details

    • create

      public static Optional<SendMessage> create()
      Creates a message sender, initializing internal variables with environment variables. The relevant env vars are:
      • `EMAIL_METHOD`
      • `SENDGRID_API_KEY` if `EMAIL_METHOD` is "sendgrid"
      • `SMTP_SERVER`, `SMTP_PORT`, `SMTP_ENABLE_AUTH`, `SMTP_USER`, and `SMTP_PASSWORD` if `EMAIL_METHOD` is "smtp"
      • `TWILIO_ACCOUNT_SID`, `TWILIO_AUTH_TOKEN`, and `TWILIO_SENDING_NUMBER` if `EMAIL_METHOD` is "twilio"
      Returns:
      a new SendMessage object to send your messages
    • sendEmail

      public int sendEmail(String from, String subject, String to, String messageTemplate, Map<String,Object> context) throws IOException
      Send an email, respecting method specified in environment variable EMAIL_METHOD ("smtp" or "sendgrid")
      Parameters:
      from - The email "from" address
      subject - The email subject
      to - The email recipient
      messageTemplate - A string with optional Jinja2 template variables. Will be rendered as plain text.
      context - A Map containing variables that will be substituted in the message_template
      Throws:
      IOException - If there's a network error talking to sendgrid
    • sendSmtpEmail

      public boolean sendSmtpEmail(String from, String subject, String to, String messageTemplate, Map<String,Object> context) throws IOException
      Send an email via plain SMTP
      Parameters:
      from - The email "from" address
      subject - The email subject
      to - The email recipient
      messageTemplate - A string with optional Jinja2 template variables. Will be rendered as plain text.
      context - A HashMap containing variables that will be substituted in the message_template
      Throws:
      IOException - If there's a network error
    • sendSendgridEmail

      public int sendSendgridEmail(String from, String subject, String to, String messageTemplate, Map<String,Object> context) throws IOException
      Send an email via Twilio Sendgrid API. Depends on the env variable SENDGRID_API_KEY. which should be a valid API key for Twilio SendGrid.
      Parameters:
      from - The email "from" address
      subject - The email subject
      to - The email recipient
      messageTemplate - A string with optional Jinja2 template variables. If the rendered template startes with "", will be rendered as `text/html`. Otherwise, will be rendered as plain text.
      context - A HashMap containing variables that will be substituted in the message_template
      Throws:
      IOException - If there's a network error talking to sendgrid
    • sendSms

      public String sendSms(String to, String messageTemplate, HashMap<String,Object> context)
      Send an SMS Message, currently using Twilio SMS API. Depends on env variables - TWILIO_ACCOUNT_SID. - TWILIO_AUTH_TOKEN, and - TWILIO_SENDING_NUMBER.
      Parameters:
      to - a valid phone number for SMS messaging, in ISO format.
      messageTemplate - A string with Jinja2 template variables. Will be rendered as plain text.
      context - a HashMap with variables that will be substituted into the message_template
    • isValidEmail

      public static boolean isValidEmail(String email)
    • isValidPhoneNumber

      public static boolean isValidPhoneNumber(String phoneNumber)
    • main

      public static void main(String[] args) throws IOException
      Main just for testing.
      Throws:
      IOException