Class SendMessage
java.lang.Object
edu.suffolk.litlab.efsp.server.utils.SendMessage
-
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<SendMessage> create()Creates a message sender, initializing internal variables with environment variables.static booleanisValidEmail(String email) static booleanisValidPhoneNumber(String phoneNumber) static voidMain just for testing.intsendEmail(String from, String subject, String to, String messageTemplate, Map<String, Object> context) Send an email, respecting method specified in environment variable EMAIL_METHOD ("smtp" or "sendgrid")intsendSendgridEmail(String from, String subject, String to, String messageTemplate, Map<String, Object> context) Send an email via Twilio Sendgrid API.Send an SMS Message, currently using Twilio SMS API.booleansendSmtpEmail(String from, String subject, String to, String messageTemplate, Map<String, Object> context) Send an email via plain SMTP
-
Method Details
-
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 IOExceptionSend an email, respecting method specified in environment variable EMAIL_METHOD ("smtp" or "sendgrid")- Parameters:
from- The email "from" addresssubject- The email subjectto- The email recipientmessageTemplate- 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 IOExceptionSend an email via plain SMTP- Parameters:
from- The email "from" addresssubject- The email subjectto- The email recipientmessageTemplate- 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 IOExceptionSend 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" addresssubject- The email subjectto- The email recipientmessageTemplate- 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
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
-
isValidPhoneNumber
-
main
Main just for testing.- Throws:
IOException
-