package main
import (
"crypto/tls""encoding/base64""fmt""io/ioutil""log""mime""net""net/mail""net/smtp""path/filepath"
)
const (
MIME = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
)
// Attachment represents an email attachment.type Attachment struct {
Filename string
Data []byte
}
type Message struct {
Attachments map[string]*Attachment
}
func(m *Message)attach(file string)error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
_, filename := filepath.Split(file)
m.Attachments[filename] = &Attachment{
Filename: filename,
Data: data,
}
returnnil
}
// SSL/TLS Email Examplefuncmain() {
m := &Message{}
m.Attachments = make(map[string]*Attachment)
// 이거 smtp 서버랑 주소 안 맞으면 거부됩니다. 네이버면 아이디@naver.com 그리고 mail.Address 첫번째 공백은 이름인데 없어도 됨
from := mail.Address{"", "보내는 메일 주소"}
to := mail.Address{"", "받는 메일 주소"}
subj := "This is the email subject 나여나"
body := "This is an example body.<br> With two lines. <br>내용이여.."// Setup headers
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subj
// Setup message
message := ""for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
fileman := []string{"1.png", "2.png", "3.pdf"}
// add attachmentsfor k := 0; k < len(fileman); k++ {
if err := m.attach(fileman[k]); err != nil {
log.Fatal(err)
}
}
boundary := "f46d043c813270fc6b04c2d223da"iflen(m.Attachments) > 0 {
message += "Content-Type: multipart/mixed; boundary=" + boundary + "\r\n"
message += "\r\n--" + boundary + "\r\n"
}
message += MIME + "\r\n" + body
iflen(m.Attachments) > 0 {
for _, attachment := range m.Attachments {
message += "\r\n\r\n--" + boundary + "\r\n"
ext := filepath.Ext(attachment.Filename)
mimetype := mime.TypeByExtension(ext)
if mimetype != "" {
mime := fmt.Sprintf("Content-Type: %s\r\n", mimetype)
message += mime
} else {
message += "Content-Type: application/octet-stream\r\n"
}
message += "Content-Transfer-Encoding: base64\r\n"
message += "Content-Disposition: attachment; filename=\"=?UTF-8?B?"
message += base64.StdEncoding.EncodeToString([]byte(attachment.Filename))
message += "?=\"\r\n\r\n"
b := make([]byte, base64.StdEncoding.EncodedLen(len(attachment.Data)))
base64.StdEncoding.Encode(b, attachment.Data)
message += string(b)
message += "\r\n--" + boundary
}
message += "--"
}
// Connect to the SMTP Server
servername := "smtp 주소:465"
host, _, _ := net.SplitHostPort(servername)
auth := smtp.PlainAuth("", "아이디", "패스워드", host)
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
// Here is the key, you need to call tls.Dial instead of smtp.Dial// for smtp servers running on 465 that require an ssl connection// from the very beginning (no starttls)
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
log.Panic(err)
}
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Panic(err)
}
// Authif err = c.Auth(auth); err != nil {
log.Panic(err)
}
// To && Fromif err = c.Mail(from.Address); err != nil {
log.Panic(err)
}
if err = c.Rcpt(to.Address); err != nil {
log.Panic(err)
}
// Data
w, err := c.Data()
if err != nil {
log.Panic(err)
}
_, err = w.Write([]byte(message))
if err != nil {
log.Panic(err)
}
err = w.Close()
if err != nil {
log.Panic(err)
}
c.Quit()
}
|
|
번 호
카테고리
제 목
이름
조회수
Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천 실행시간 : 0.05984 초 to webmaster... gogo sea. gogo sea.