Code Monkey home page Code Monkey logo

gomailer's Introduction

gomailer

A minimal example for sending emails with gmail in go

Instructions

  • Clone repo to $GOPATH/src/github.com/octohedron
  • Install dependencies
go get
  • Set env variables
export [email protected]
export email_password=hunter2
export [email protected]
  • Build & run program
$ go build && ./gomailer &
Serving email server in port 5555
  • Test it with curl
$ curl -X POST \
 -F '[email protected]' \
 -F 'name=example' \
 -F 'subject=ExampleSubject' \
 -F 'message=HelloWorld' \
  http://127.0.0.1:5555/sendemail
sent

Now you got the email server working, might need to add an nginx proxy, add your basic nginx configuration, then install and run certbot for SSL support

  • Install certbot with apt-get install python-certbot-nginx
  • Run certbot with certbot --nginx

Your new site.conf file should look like this

server {
    server_name email.example.com;
    location / {
            proxy_pass http://127.0.0.1:5555;
    }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/email.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/email.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = email.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80;
        server_name email.example.com;
    return 404; # managed by Certbot
}

Reload nginx with service nginx restart

Now you can send emails through gmail, for example, with javascript

var formData = new FormData();
formData.append("name", "example name");
formData.append("email", "example email");
formData.append("subject", "example subject");
formData.append("message", "Hello World");
// make the request, change the server address
fetch("https://email.example.com/sendemail", {
  method: "POST",
  body: formData
}).then(result => {
  result.text().then(result => {
    if (result == 'sent') {
      alert("Email sent successfully :)")
    } else {
      alert("There was a problem :)")
    }
  });
});

gomailer's People

Contributors

octohedron avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.