Categorygithub.com/CloudMile/gae_send_mail_api
modulepackage
0.0.0-20220317150204-f15359f0896f
Repository: https://github.com/cloudmile/gae_send_mail_api.git
Documentation: pkg.go.dev

# README

Mail Server on GAE

Abstract

Because of GCE couldn't be a mail smtp server[1]. For workaround, setup a mail server with API mode on GAE.

Setup GAE

Create a new project on GCP

  1. Choose App Engine.
  2. Choose language for development, we using Go.
  3. Choose deploy location[2].
  4. You can skip the tutorial.
  5. Refresh the page, and you can see the GAE main page.

Setup Sender

On GAE → Settings → Email senders → ADD Note, there are some restrictions for sender

  • Project Owner
  • Make project ID to be a domain name; for example, the project is hello-world-2018 you can use [email protected], this mail DON'T need to add into Email senders
  • More info[3]

Get Mail Server Project Source Code

Open Cloud Shell on GCP console check $GOPATH

$ echo $GOPATH
/home/<GCP_UESR>/gopath:/google/gopath

if $GOPATH not exist, create a new one

$ mkdir -p ~/gopath

get source code

$ go get -u github.com/CloudMile/gae_send_mail_api

this is a warning, skip it

package github.com/CloudMile/gae_send_mail_api: no Go files in /home/<GCP_UESR>/gopath/src/github.com/CloudMile/gae_send_mail_api

get http controller lib

$ go get -u github.com/gorilla/mux

cd to project

$ cd ~/gopath/src/github.com/CloudMile/gae_send_mail_api/

set up app.yaml

$ vim main/app.yaml

you can set up CUSTOM_TOKEN for enable auth check if you enable, you need to add curl -H 'Custom-Token: <YOUR_TOEKN>'

deploy

$ make deploy PROJECT_ID='<YOUR_PROJECT_ID>' FROM='mail@<YOUR_PROJECT_ID>.appspotmail.com'

Test or Use

cURL with multipart/form-data

$ curl -X POST \
-F "[email protected]" \
-F "[email protected],[email protected]" \
-F "[email protected],[email protected]" \
-F "subject=Send mail from GAE" \
-F "data=@./favicon.png" \
-F "body=upload file" \
"https://mail-dot-<YOUR_PROJECT_ID>.<REGION_ID>.r.appspot.com/send"

The <REGION_ID> is an abbreviated code that Google assigns based on the region you select when you create your app[4].

cURL with application/x-www-form-urlencoded

$ curl -X POST -d '[email protected]&subject=Send mail from GAE' "https://mail-dot-<YOUR_PROJECT_ID>.<REGION_ID>.r.appspot.com/send"

cURL with application/json

$ curl -X POST -H 'Content-Type: application/json' -d '{"to": "[email protected]", "subject": "Send mail from GAE"}' "https://mail-dot-<YOUR_PROJECT_ID>.<REGION_ID>.r.appspot.com/send"

ruby

$ gem install rest-client
require 'rest-client'
send_url = 'https://mail-dot-<YOUR_PROJECT_ID>.<REGION_ID>.r.appspot.com/send'
file = File.open('./static/favicon.png')
params = {
  to: '[email protected]',
  subject: 'Send Mail for Test',
  body: 'TESTTESTTESTTESTTESTTESTTEST',
  data: file
}
RestClient.post(send_url, params)

python

$ pip install requests
$ pip install requests-toolbelt
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
send_url = 'https://mail-dot-<YOUR_PROJECT_ID>.<REGION_ID>.r.appspot.com/send'
multipart_data = MultipartEncoder(
    fields={
            # a file upload field
            'data': ('favicon.png', open('./static/favicon.png', 'rb'), 'text/plain'),
            # plain text fields
            'to': '[email protected]',
            'subject': 'Send Mail for Test',
            'body': 'TESTTESTTESTTESTTESTTESTTEST',
            }
    )
response = requests.post(send_url, data=multipart_data, headers={'Content-Type': multipart_data.content_type})

Refs

# Packages

No description provided by the author
No description provided by the author