spike (2 days) - notification emails via python
Closed this issue · 2 comments
Check for this on ONSdigital github.
What is this
We need a reusable bit of software to send email notifications.
What to do
Look at the options for sending an email via python and make comparissons between them to be fed back to the team.
Ideally, we'd want an example or two but pure research is fine as well, we want to compare the options on:
- complexity
- dependencies - i.e, what external things (like mail servers) if any are needed
- reliability - i.e if external packages are involved, how much are they used? how long since they were updated? are there many open issues on the repository?
You'll probably need an SMTP server, there's some sort of AWS service for this, its worth investigating that and how/if we can utilise it via boto3 (or any alternative aws compliant package).
Accetpance Critiera
- Present the findings back to the team.
I'm not bothered how you present this information back: slides and a chat, a write up, example on a branch somewhere - whatever forum you're most comfortable with that gets the information accross is fine.
For this ticket I test different solution that we could use to send emails via python.
In this presentation (https://docs.google.com/presentation/d/1omD9T7ISZCqK9keZiacSWiEWpYJSptTetYLFun92Xns/edit?usp=sharing ) I covered the different solutions
- SMTPLib (SMTP) : Python's built-in SMTP library.
- Smtplib and others Python library for sending emails(Mime + Email).
- Boto3: AWS SDK for Python to interact with AWS services.
- Third-party packages: e.g., yagmail, sendgrid
Useful resources: https://realpython.com/python-send-email/
- Setting up a Local SMTP Server
Additional Notes: Secure SMTP Connection / Encryption ( SSL Library)
https://docs.python.org/3/library/ssl.html
Code Solutions:
I initially used the Python built in SMTP library to send email on local server ( Requires SMTP. Server)
This code is used to send Plain test emails only.
If we want to add HTML Content to email we can do like this
Side Note (Difference between HTML email and plain email ( a plain text email contains only text—no images, stylised fonts, or hyperlinks.)
(HTML, which stands for HyperText Markup Language, is a way to code a document (made out of ASCII text) that lets an HTML reader (such as a web browser) know how to render certain types of information.
HTML have everything plain text emails don’t have: colour, style, images, and sometimes multimedia.)
Code Example:
If you want to attach pdf or file to an email you can do this by using the Email and Mime library
SMTP Review:
Additionally AWS offers services to send email . Amazon SES and Boto3
Amazon SES is a cloud-based email service provider that can integrate into any application for high volume email automation.
AWS SES need to configured in AWS Console ( Website ) before we can embed that in our code
Note: Before you can send an email (. The email address needs to be verified
You can also send HTML email using the AWS like this
These are alternative solution available such as yagmail and sendgrid
Addtional Note: AWS Code solution need furture configration set up to fully send the content of the email to ONS email. During testing I could send an email to another email with subject and content however the recipient cannot view the content as they get this error "Your message wasn't delivered because the recipient's email provider rejected it."
I did some research and the issue is with ONS DMARC Policy and we can fix this issue if we verify the domain and update the DNS record to ONS domain's DNS configuration.