0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go: Starttls でメールの送信

Posted at

こちらと同じことを Deno で行いました。
Python3: Starttls でメールの送信
hi-ho.ne.jp で試しました。

hi-ho.go
// ---------------------------------------------------------------
//
//	hi-ho.go
//
//					Jul/07/2020
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"net/smtp"
	"github.com/joho/godotenv"
)


func main() {

	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

err := godotenv.Load(".env")
    if err != nil {
        panic(err)
    }

	server := os.Getenv("SERVER")
	port := os.Getenv("PORT")
	usr := os.Getenv("USR")
	password := os.Getenv("PASSWORD")

	fmt.Fprintf (os.Stderr,server + "\n")
	fmt.Fprintf (os.Stderr,port + "\n")

	from := os.Getenv("FROM")
	to := os.Getenv("TO")

	auth := smtp.PlainAuth("", usr, password, server)

	msg := []byte("" +
		"From: " + from + "\n" +
		"To: " + to + "\n" +
		"Subject: Test from Hi-ho PM 20:02\n" +
		"\n" +
		"テスト\n" +
		"今晩は。\n" +
		"PM 20:02\n" +
	"")

	err = smtp.SendMail(server + ":" + port, auth, from, []string{to}, msg)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %v\n", err)
		return
	}

	fmt.Print("success\n")

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------
.env
SERVER = 'hi-ho.mose-mail.jp'
PORT = 587
USR = '****@hi-ho.ne.jp'
PASSWORD = '****'
FROM = '****@hi-ho.ne.jp'
TO = 'sample@example.com'

実行結果

$ go run hi-ho.go
*** 開始 ***
hi-ho.mose-mail.jp
587
success
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?