项目作者: mpdroog

项目描述 :
Send queued e-mail using Beanstalkd
高级语言: Go
项目地址: git://github.com/mpdroog/smtpw.git
创建时间: 2015-07-11T09:59:02Z
项目社区:https://github.com/mpdroog/smtpw

开源协议:BSD 2-Clause "Simplified" License

下载


SMTP Worker

Send queued e-mail in separate process through
a JSON abstraction.

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.

Why?

  • Security, SMTP credentials are isolated from the website
  • Simplicity, the website creates JSON and all SMTP logic is isolated here
  • Scalable, things need to go faster? Start a second..third..fourth.. worker!
  • Cleaner, no more ugly timeouts in the browser if trouble and no more lost emails/customers!

How?

  • Use Beanstalkd (http://kr.github.io/beanstalkd/) to add jobs in a queue.
  • One or more SMTPw-instances read the queue and try to send
  • Failure? Wait 5sec on SMTPw error or wait for Beanstalk deadline (if process got killed) and try again!

Config

  1. {
  2. "beanstalk": "127.0.0.1:11300", // Hostname:port to Beanstalkd
  3. "from": {
  4. "support": { // From in JSON matches to this from
  5. "user": "emailuser", // SMTP username
  6. "pass": "supersecretpass", // SMTP password
  7. "host": "smtp.yourcompany.com", // SMTP hostname
  8. "port": 1234, // SMTP port
  9. "from": "support@yourcompany.com", // From-address (used in mail header)
  10. "display": "RootDev", // Display-name added before From-address
  11. "bcc": [
  12. "alwayssendhere@yourcompany.com" // Send a secret copy (can be useful for monitoring/debugging)
  13. ],
  14. "allowBCC": false // true to allow the BCC-field to be appended by the Email-struct
  15. }
  16. }
  17. }

Be careful with Insecure=true as it will allow MITM (Man In The Middle Attacks)

Bounce sets the From-header to the bounce-address and
sets Reply-To for human replies.

Usage

  1. ./smtpw -h
  2. Usage of ./smtpw:
  3. -c="./config.json": Path to config.json
  4. -s=false: Delete e-mail on deverr
  5. -v=false: Verbose-mode
  • Path config.json, where to find the config.json file
  • Delete on deverr, delete and flush the e-mail content if
    JSON is invalid.
  • Verbose-mode, log what we’re doing

JSON

  1. type Email struct {
  2. From string // Key that MUST match From in config
  3. To []string
  4. BCC []string // Only used when allowBCC=true in config
  5. Subject string
  6. Html string
  7. Text string
  8. HtmlEmbed map[string]string // file.png => base64(bytes)
  9. Attachments map[string]string // file.png => base64(bytes)
  10. }

WARN: I like my parsers strict. Every HtmlEmbed key is scanned with
Html.contains(cid:key) and if not found it will throw an error.

WARN: Invalid supplied email-address will fail with errors like
mail: no angle-addr, please use a descent email RFC validator, i.e. for PHP https://github.com/iamcal/rfc822

Errors

panic: dial tcp 127.0.0.1:11300: connection refused
Install Beanstalkd and update config if Beanstalkd is running elsewhere?

x509: certificate signed by unknown authority
Your SMTP-server has a self-signed certificate? Time to get
a signed one!

Job never received by SMTPw?
Is the job sent to the email tube? As smtpw only listenes to this tube.

  1. telnet 127.0.0.1 11300
  2. use email
  3. peek-ready << should show your JSON here

Systemd

  1. # User + systemd
  2. useradd -r smtpw
  3. mkdir -p /home/smtpw
  4. vi /etc/systemd/system/smtpw@.service
  5. vi /etc/systemd/system/smtpw.target
  6. chmod 644 /etc/systemd/system/smtpw@.service
  7. chmod 644 /etc/systemd/system/smtpw.target
  8. systemctl daemon-reload
  9. systemctl enable smtpw@1
  10. systemctl enable smtpw@2
  11. systemctl start smtpw@1
  12. systemctl start smtpw@2

External deps