Skip to main content

SMTP


SMTP

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
</head>

<body>
<table
border="1"
cellpadding="0"
cellspacing="0"
width="100%"
style="border-collapse: collapse; background-color:#F8FAFB;"
>
<tr>
<td>
<table border="1" cellpadding="0" cellspacing="0" width="600" style="margin: 0 auto;">
<tr>
<td height="40px"></td>
</tr>
<tr>
<td>{{.Something}}</td>
</tr>
<tr>
<td>{{.Something}}</td>
</tr>
<tr>
<td>{{.Something}}</td>
</tr>
<tr>
<td height="40px"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
warning
  • 이미지가 필요한 경우 svg 등을 직접 삽입하지 말고 https가 적용된 경로에 이미지를 업로드하고 그 경로를 사용합니다
  • 최대한 <table>, <tr>, <td> 태그를 사용하여 레이아웃을 구성합니다
  • <style>가 지원되지 않을 수 있습니다, 인라인 스타일을 사용하는 것이 좋습니다
  • https://validator.w3.org/#validate_by_input+with_options 에서 검사합니다

이메일 템플릿을 작성할 때는 border="1"로 설정하여 원하는 레이아웃을 먼저 구성합니다. 작업을 마친 후 border="0"으로 설정하여 테두리를 제거합니다.

  • <table>
    • border="<0|1>"
    • width="<px|%>"
  • <tr>
  • <td>
    • align="<left|right|center|justify|char>
    • valign="<top|middle|bottom|baseline>"
info

사용했던 버튼 예시입니다.

<table
border="0"
cellpadding="0"
cellspacing="0"
align="center"
style="
border-collapse: collapse;
border-radius: 6px;
background: #228be6;
"
>
<tr>
<td align="center">
<a
href="https://wiki.loliot.net"
style="
color: #ffffff;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
font-style: normal;
font-weight: 500;
width: 200px;
line-height: 48px;
display: inline-block;
text-decoration: none;
-webkit-text-size-adjust: none;
"
>Button</a
>
</td>
</tr>
</table>

net/smtp

package smtp

import (
"bytes"
"net/smtp"
"text/template"
)

var (
auth smtp.Auth
address string
from string
)

func Init() {
c := config.Config()

auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, c.SMTP.Host)
address = c.SMTP.Host + ":" + c.SMTP.Port
from = c.SMTP.Username
}

func Send(to, something string) error {
header := "To: " + to + "\r\n" +
"Subject: 제목\n" +
"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"

var body bytes.Buffer
t, _ := template.ParseFiles("template/smtp.html")
t.Execute(&body, struct {
Something string
}{
Something: something,
})

return smtp.SendMail(
address,
auth,
from,
[]string{to},
append(header, body.Bytes()...),
)
}
info

SMTP 프로토콜은 RFC 2921에 정의되어 있습니다. SMTP는 라인 단위로 커맨드, 데이터 등을 보내는 데, 라인을 구분하는 기호는 <CRLF>입니다. html로 내용을 보낼 때, 줄바꿈은 <br />을 사용하는 것이 원칙이지만 <LF>를 인식하는 경우도 있는 것 같습니다. 전송 또는 전송 내용에 오류가 있다면 줄바꿈을 바꿔가면서 테스트 해보시기 바랍니다.

Gmail

  • Google 계정 관리 -> 보안 -> Google에 로그인
    • 2 단계 인증
    • 앱 비밀번호
      • 앱 선택: 메일
      • 기기 선택: 기타

Email authentication