smtp 호스트로 메일 보내기 - golang

smtp 호스트로 메일 보내는 golang 소스

rfc5322를 만족하는 golang smtp 작성

golang의 "net/smtp" 라이브러리를 사용해서 메일을 gmail로 전송하다 보면 아래와 같은 에러메시지를 받게된다.

Sep  4 15:58:13 testemail postfix/smtp[53924]: 69F54C1466: to=<[email protected]>,
relay=gmail-smtp-in.l.google.com[173.194.174.27]:25,delay=1.7, delays=0.04/0.01/1/0.58, 
dsn=5.7.1, status=bounced (host gmail-smtp-in.l.google.com[173.194.174.27] 
said: 550-5.7.1 [XXX.XXX.XXX.XXX] Our system has detected that this message is not RFC 550-5.7.1 5322 compliant: 
550-5.7.1 'From' header is missing. 550-5.7.1 To reduce the amount of spam sent to Gmail, 
this message has been 550-5.7.1 blocked. Please visit 550-5.7.1  
https://support.google.com/mail/?p=RfcMessageNonCompliant 550 5.7.1  
and review RFC 5322 specifications for more information. kk12-20020a170903070c00b001c07bac13d0si6916397plb.383
- gsmtp (in reply to end of DATA command))

 

구글로 검색해서 만들어진 소스를 보면 RFC를 만족하게 않게 작성된 코드로 인해 발생된 케이스로 확인되어
아래와 같이 작성하면 rfc5322(https://datatracker.ietf.org/doc/html/rfc5322)를 만족하는 포멧으로 만들어진다.

 

package main

import (
	"net/smtp"
)

func main() {
	auth := smtp.PlainAuth("", "smtpauthID", "smtpauthPassword", "smtphost - mail.aaa.com")
	toEmail := "[email protected]"
	toArray := []string{toEmail}
	fromEmail := "[email protected]"

	fromHeader := "From: " + fromEmail + "\r\n"
	toHeader := "To: " + toEmail + "\r\n"
	headerSubject := "Subject: 테스트 전송용 메일\r\n"
	headerBlank := "\r\n"
	body := "이메일 전송 테스트\r\n"
	msg := []byte(fromHeader + toHeader + headerSubject + headerBlank + body)
	err := smtp.SendMail("mail.aaa.com:25", auth, fromEmail, toArray, msg)
	if err != nil {
		panic(err)
	}
}