8000 add smtp sender sample code · c-ue/Golang_sample_code@9a6201a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a6201a

Browse files
committed
add smtp sender sample code
1 parent f350978 commit 9a6201a

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# Macos resource file
15+
.DS_Store

smtp/sender.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"log"
6+
"net/smtp"
7+
)
8+
9+
type loginAuth struct {
10+
username, password string
11+
}
12+
13+
func LoginAuth(username, password string) smtp.Auth {
14+
return &loginAuth{username, password}
15+
}
16+
17+
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
18+
return "LOGIN", []byte{}, nil
19+
}
20+
21+
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
22+
if more {
23+
switch string(fromServer) {
24+
case "Username:":
25+
return []byte(a.username), nil
26+
case "Password:":
27+
return []byte(a.password), nil
28+
default:
29+
return nil, errors.New("Unkown fromServer")
30+
}
31+
}
32+
return nil, nil
33+
}
34+
35+
func main() {
36+
// Choose auth method and set it up
37+
auth := LoginAuth("account", "password")
38+
39+
// Here we do it all: connect to our server, set up a message and send it
40+
to := []string{"john@gmail.com"}
41+
msg := []byte("From: \"account\" <account@gmail.com>\r\n" +
42+
"To: \"john\" <john@gmail.com>\r\n" +
43+
"Subject: Golang Test\r\n" +
44+
"\r\n" +
45+
"From Golang Example Code\r\n")
46+
err := smtp.SendMail("smtp.gmail.com:587", auth, "account@gmail.com", to, msg)
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
0