Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SMTP pool to send emails #12

Open
ben-z opened this issue Jan 1, 2025 · 3 comments
Open

Use SMTP pool to send emails #12

ben-z opened this issue Jan 1, 2025 · 3 comments
Labels
good first issue Good for newcomers

Comments

@ben-z
Copy link
Member

ben-z commented Jan 1, 2025

Today, we were sending 558 emails at the same time after introducing the "active terms" feature. SMTP started throwing this error:

│ Error: 454 4.7.0 Too many login attempts, please try again later. For more information,
│ 4.7.0 go to
│ 4.7.0  https://support.google.com/mail/answer/7126229 00721157ae682-6f3e74459a1sm64557097b3.46 - gsmtp
│ 
│   with email_email.all_emails["o-zhang-05-expiring-soon"],
│   on common_emails.tf line 9, in resource "email_email" "all_emails":
│    9: resource "email_email" "all_emails" {

Only 188 out of 558 emails were sent in the first round. In the second round, slightly over 100 emails were sent.

It turns out we are creating a new SMTP connection for every email, which is not desirable:

https://stackoverflow.com/a/54299705/4527337

This ticket tracks using an SMTP pool to send emails.

@ben-z ben-z added the good first issue Good for newcomers label Jan 1, 2025
@ben-z ben-z changed the title Use pooled SMTP Use SMTP pool to send emails Jan 1, 2025
wato-github-automation bot pushed a commit to WATonomous/watcloud-website that referenced this issue Jan 1, 2025
## Description

This PR adds this project to the join page:
WATonomous/terraform-provider-email#12

## Checklist
- [x] I have read and understood the [WATcloud
Guidelines](https://cloud.watonomous.ca/docs/community-docs/watcloud/guidelines)
- [x] I have performed a self-review of my code
@ben-z
Copy link
Member Author

ben-z commented Jan 24, 2025

https://github.com/knadh/smtppool may be a useful project.

@ben-z
Copy link
Member Author

ben-z commented Jan 24, 2025

There appears to be a new object-oriented way to define resources: https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-resource-create

This makes it easy to persist an SMTP client for use multiple times.

@ben-z
Copy link
Member Author

ben-z commented Jan 24, 2025

The smtp.SendMail function is just a wrapper for working with an underlying client. We can use the client directly and persist it for multiple emails.

https://github.com/golang/go/blob/608acff8479640b00c85371d91280b64f5ec9594/src/net/smtp/smtp.go#L321-L376

We might need to manage concurrency so that smtp messages don't send interleaved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant